- D1 Meta Docs - Denizen Script -
Home Page / Pi to one million places / Contact mcmonkey / Donate / Paste Scripts / Denizen Help /
You are browsing as a guest.
Login | Register








The script repo is an archive of historical scripts. For modern scripts, or to post your own, please use the Scripts forum section.





Staff Pick: Matrices


By waterquarks
Created: 2018/11/06 19:43:21 UTC-08:00 (6 years and 46 days ago)
Edited: 2018/11/14 18:53:25 UTC-08:00 (6 years and 38 days ago)
Likes: 0

Staff pick as of: 2018/11/06 19:46:08 UTC-08:00 (6 years and 46 days ago)
Denizen Version: 1.0.3
Script Version: 1.0
Description:

Matrices for Denizen!

This script contains a thoroughly-featured matrix object that supports addition, scalation, multiplication, powering and more!

Do "/matrix" in game to see a demonstration of it in action.

A basic understanding of custom object scripts is needed in order to work with this script, check https://one.denizenscript.com/haste/16447 to learn about them :D



Download script | View raw script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
12400

Matrix:
  type: custom
  debug: false
  values: li@
  tags:
    value:
      # Usage: <custom@Matrix.value[%row%|%column%]>
      # Returns the value of the Matrix in the position specified.
      - define row "<c.value.as_list.get[1]>"
      - define column "<c.value.as_list.get[2]>"
      - determine "<c.this.values.get[<def[row]>].split_by[,].get[<def[column]>]>"
    rows:
      # Returns the number of rows in the Matrix.
      - determine "<c.this.values.size||0>"
    columns:
      # Returns the number of columns in the Matrix.
      - determine "<c.this.values.get[1].split_by[,].size||0>"
    row:
      # Usage: <custom@Matrix.row[%row%]>
      # Returns the values contained in the specified row, in a li@a|b|c format.
      - determine "<c.this.values.get[<c.value>].split_by[,]||li@>"
    column:
      # Usage: <custom@Matrix.column[%column%]>
      # Returns the values contained in the specified column, in a li@a|b|c format.
      - define column li@
      - foreach "<c.this.values>":
        - define row "<def[value].split_by[,]>"
        - define column "<def[column].include[<def[row].get[<c.value>]>]>"
      - determine "<def[column]||li@>"
    add:
      # Usage: <custom@Matrix.add[%matrix%]>
      # Adds two matrices by adding the corresponding entries together.
      - define matrix "<c.value>"
      - repeat "<c.this.rows>":
        - define row "<def[value]>"
        - repeat "<c.this.columns>":
          - define column "<def[value]>"
          - define value "<c.this.value[<def[row]>|<def[column]>]>"
          - define value "<def[value].add[<def[matrix].value[<def[row]>|<def[column]>]>]>"
          - adjust "<entry[m].result||<c.this>>" "value:<def[row]>|<def[column]>|<def[value]>" "save:m"
      - determine "<entry[m].result>"
    scale:
      # Usage: <custom@Matrix.scale[%scalar%]>
      # Returns the product of a matrix and a scalar value.
      - define scalar "<c.value>"
      - repeat "<c.this.rows>":
        - define row "<def[value]>"
        - repeat "<c.this.columns>":
          - define column "<def[value]>"
          - define value "<c.this.value[<def[row]>|<def[column]>].mul[<def[scalar]>]>"
          - adjust "<entry[m].result||<c.this>>" "value:<def[row]>|<def[column]>|<def[value]>" "save:m"
      - determine "<entry[m].result>"
    mul:
      # Usage: <custom@Matrix.mul[%matrix%]>
      # Returns the product of two matrices with entries in a field.
      # The number of columns of the first matrix must be equal
      # to the number of rows of the second one.
      - define matrix "<c.value>"
      - define aux li@
      - repeat "<c.this.rows>":
        - define row "<def[value]>"
        - repeat "<def[matrix].columns>":
          - define column "<def[value]>"
          - define r "0"
          - foreach "<c.this.row[<def[row]>]>":
            - define i "<def[loop_index]>"
            - define r "<def[r].add[<def[value].mul[<def[matrix].column[<def[column]>].get[<def[i]>]>]>]>"
              #- adjust "<entry[m].result||<c.this>>" "value:<def[row]>|<def[column]>|<def[r]>" "save:m"
          - define aux "<def[aux]><def[r]><t[<def[matrix].columns.matches[<def[column]>]>]:||,>"
        - define aux "<def[aux].include[|]>"
      - adjust "<c.this>" "values:<def[aux]>" "save:m"
      - determine "<entry[m].result>"
    power:
      # Usage: <custom@Matrix.power[%power%]>
      # Returns the product of a matrix power.
      - define power "<c.value.sub[1]>"
      - define matrix "<c.this>"
      - repeat "<def[power]>":
        - define matrix "<def[matrix].mul[<c.this>]>"
      - determine "<def[matrix]>"
  mechanisms:
    value:
      # Adjusts the value in a certain position of a matrix.
      # Usage: adjust <custom@Matrix> value:%row%|%column%|%value%
      - define row "<c.value.as_list.get[1]>"
      - define column "<c.value.as_list.get[2]>"
      - define value "<c.value.as_list.get[3]>"
      - define newRow "<c.this.row[<def[row]>]>"
      - define newRow "<def[newRow].set[<def[value]>].at[<def[column]>].separated_by[,]>"
      - define values "<c.this.values.set[<def[newRow]>].at[<def[row]>]>"
      - adjust "<c.this>" "values:<def[values]>" "save:matrix"
      - determine "<entry[matrix].result>"

MatrixTest:
  type: command
  debug: false
  usage: /matrix
  description: See what can the script do in a test matrix!
  name: matrix
  script:
    - define matrix "custom@matrix[values=li@0.6,2.4,0.5|0.5,0,0|0,0.6,0]"
    - narrate "<def[matrix].power[5]>"
    #    - define matrix "custom@matrix[values=li@1,2,3|4,5,6]"
    #    - announce "<def[matrix].rows>"
    #    # Narrates "2".
    #    - announce "<def[matrix].columns>"
    #    # Narrates "3".
    #    - announce "<def[matrix].values>"
    #    # Narrates "li@1,2,3|4,5,6"
    #    - announce "<def[matrix].value[2|3]>"
    #    # Narrates "6".
    #    - define matrixB "custom@matrix[values=li@4,5,6|7,8,9]"
    #    - announce "<def[matrix].add[<def[matrixB]>]>"
    #    # Narrates "custom@MATRIX[values=li@5,7,9|11,13,15]"
    #    - announce "<def[matrix].scale[3]>"
    #    # Narrates "custom@MATRIX[values=li@3,6,9|12,15,18]"
    #    - define matrixC "custom@matrix[values=li@1,2,3|4,5,6|7,8,9]"
    #    - announce "<def[matrix].mul[<def[matrixC]>]>"
    #    # Narrates "custom@MATRIX[values=li@30,36,42|66,81,96]"
    #    - announce "<def[matrixC].power[2]>"
    #    # Narrates "custom@MATRIX[values=li@30,36,42|66,81,96|102|126|150]"
    #






View History