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]"
#