Element-wise operations: Difference between revisions

Content deleted Content added
Petelomax (talk | contribs)
m →‎{{header|Phix}}: added syntax colouring the hard way, phix/basics
Added AutoHotkey
Line 227: Line 227:
(2567686153161210000000000000000000000000000.00, 17069174130723200000000000000000000000000000000.00, 10555134955777600000000000000000000000000000000000000000000.00));
(2567686153161210000000000000000000000000000.00, 17069174130723200000000000000000000000000000000.00, 10555134955777600000000000000000000000000000000000000000000.00));
</pre>
</pre>

=={{header|AutoHotkey}}==
<lang AutoHotkey>ElementWise(M, operation, Val){
A := Obj_Copy(M),
for r, obj in A
for c, v in obj {
V := IsObject(Val) ? Val[r, c] : Val
switch, operation {
case "+": A[r, c] := A[r, c] + V
case "-": A[r, c] := A[r, c] - V
case "*": A[r, c] := A[r, c] * V
case "/": A[r, c] := A[r, c] / V
case "Mod": A[r, c] := Mod(A[r, c], V)
case "^": A[r, c] := A[r, c] ** V
}
}
return A
}</lang>
Examples:<lang AutoHotkey>
M := [[1, 2, 3]
, [4, 5, 6]
, [7, 8, 9]]
output := "M`t=`t" obj2str(M) "`n"
output .= "M + 2`t=`t" obj2str(ElementWise(M, "+", 2)) "`n"
output .= "M - 2`t=`t" obj2str(ElementWise(M, "-", 2)) "`n"
output .= "M * 2`t=`t" obj2str(ElementWise(M, "*", 2)) "`n"
output .= "M / 2`t=`t" obj2str(ElementWise(M, "/", 2)) "`n"
output .= "M Mod 2`t=`t" obj2str(ElementWise(M, "Mod", 2)) "`n"
output .= "M ^ 2`t=`t" obj2str(ElementWise(M, "^", 2)) "`n"
output .= "`n"
output .= "M + M`t=`t" obj2str(ElementWise(M, "+", M)) "`n"
output .= "M - M`t=`t" obj2str(ElementWise(M, "-", M)) "`n"
output .= "M * M`t=`t" obj2str(ElementWise(M, "*", M)) "`n"
output .= "M / M`t=`t" obj2str(ElementWise(M, "/", M)) "`n"
output .= "M Mod M`t=`t" obj2str(ElementWise(M, "Mod", M)) "`n"
output .= "M ^ M`t=`t" obj2str(ElementWise(M, "^", M)) "`n"
MsgBox % output
return

obj2str(A){
output := "["
for r, obj in A{
output .= "["
for c, v in obj
output .= v ", "
output := Trim(output, ", ") "], "
}
return output := Trim(output, ", ") "]"
}</lang>
{{out}}
<pre>M = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
M + 2 = [[3, 4, 5], [6, 7, 8], [9, 10, 11]]
M - 2 = [[-1, 0, 1], [2, 3, 4], [5, 6, 7]]
M * 2 = [[2, 4, 6], [8, 10, 12], [14, 16, 18]]
M / 2 = [[0.5, 1, 1.5], [2, 2.5, 3], [3.5, 4, 4.5]]
M Mod 2 = [[1, 0, 1], [0, 1, 0], [1, 0, 1]]
M ^ 2 = [[1, 4, 9], [16, 25, 36], [49, 64, 81]]

M + M = [[2, 4, 6], [8, 10, 12], [14, 16, 18]]
M - M = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
M * M = [[1, 4, 9], [16, 25, 36], [49, 64, 81]]
M / M = [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
M Mod M = [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
M ^ M = [[1, 4, 27], [256, 3125, 46656], [823543, 16777216, 387420489]]</pre>


=={{header|BBC BASIC}}==
=={{header|BBC BASIC}}==