2016 Update
This commit is contained in:
parent
948b86eafa
commit
dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions
|
|
@ -1,3 +1,18 @@
|
|||
Similar to [[Matrix multiplication]] and [[Matrix transposition]], the task is to implement basic element-wise matrix-matrix and scalar-matrix operations, which can be referred to in other, higher-order tasks. Implement addition, subtraction, multiplication, division and exponentiation.
|
||||
This task is similar to:
|
||||
::* [[Matrix multiplication]]
|
||||
::* [[Matrix transposition]]
|
||||
|
||||
|
||||
;Task:
|
||||
Implement basic element-wise matrix-matrix and scalar-matrix operations, which can be referred to in other, higher-order tasks.
|
||||
|
||||
Implement:
|
||||
:::* addition
|
||||
:::* subtraction
|
||||
:::* multiplication
|
||||
:::* division
|
||||
:::* exponentiation
|
||||
|
||||
<br>
|
||||
Extend the task if necessary to include additional basic operations, which should not require their own specialised task.
|
||||
<br><br>
|
||||
|
|
|
|||
|
|
@ -0,0 +1,59 @@
|
|||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.BiFunction;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public class ElementWiseOp {
|
||||
static final Map<String, BiFunction<Double, Double, Double>> OPERATIONS = new HashMap<String, BiFunction<Double, Double, Double>>() {
|
||||
{
|
||||
put("add", (a, b) -> a + b);
|
||||
put("sub", (a, b) -> a - b);
|
||||
put("mul", (a, b) -> a * b);
|
||||
put("div", (a, b) -> a / b);
|
||||
put("pow", (a, b) -> Math.pow(a, b));
|
||||
put("mod", (a, b) -> a % b);
|
||||
}
|
||||
};
|
||||
public static Double[][] scalarOp(String op, Double[][] matr, Double scalar) {
|
||||
BiFunction<Double, Double, Double> operation = OPERATIONS.getOrDefault(op, (a, b) -> a);
|
||||
Double[][] result = new Double[matr.length][matr[0].length];
|
||||
for (int i = 0; i < matr.length; i++) {
|
||||
for (int j = 0; j < matr[i].length; j++) {
|
||||
result[i][j] = operation.apply(matr[i][j], scalar);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static Double[][] matrOp(String op, Double[][] matr, Double[][] scalar) {
|
||||
BiFunction<Double, Double, Double> operation = OPERATIONS.getOrDefault(op, (a, b) -> a);
|
||||
Double[][] result = new Double[matr.length][Stream.of(matr).mapToInt(a -> a.length).max().getAsInt()];
|
||||
for (int i = 0; i < matr.length; i++) {
|
||||
for (int j = 0; j < matr[i].length; j++) {
|
||||
result[i][j] = operation.apply(matr[i][j], scalar[i % scalar.length][j
|
||||
% scalar[i % scalar.length].length]);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public static void printMatrix(Double[][] matr) {
|
||||
Stream.of(matr).map(Arrays::toString).forEach(System.out::println);
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
printMatrix(scalarOp("mul", new Double[][] {
|
||||
{ 1.0, 2.0, 3.0 },
|
||||
{ 4.0, 5.0, 6.0 },
|
||||
{ 7.0, 8.0, 9.0 }
|
||||
}, 3.0));
|
||||
|
||||
printMatrix(matrOp("div", new Double[][] {
|
||||
{ 1.0, 2.0, 3.0 },
|
||||
{ 4.0, 5.0, 6.0 },
|
||||
{ 7.0, 8.0, 9.0 }
|
||||
}, new Double[][] {
|
||||
{ 1.0, 2.0},
|
||||
{ 3.0, 4.0}
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,10 @@ my @a =
|
|||
[7,8,9];
|
||||
|
||||
sub msay(@x) {
|
||||
.perl.say for @x;
|
||||
for @x -> @row {
|
||||
print ' ', $_%1 ?? $_.nude.join('/') !! $_ for @row;
|
||||
say '';
|
||||
}
|
||||
say '';
|
||||
}
|
||||
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
sub infix:<M+> (\l,\r) { l <<+>> r }
|
||||
|
||||
msay @a M+ @a;
|
||||
msay @a M+ [1,2,3];
|
||||
msay @a M+ 2;
|
||||
|
|
@ -1,32 +1,31 @@
|
|||
/*REXX program multiplies two matrixes together, shows matrixes & result*/
|
||||
/*REXX program multiplies two matrixes together, displays the matrixes and the result.*/
|
||||
m=(1 2 3) (4 5 6) (7 8 9)
|
||||
w=words(m); do k=1; if k*k>=w then leave; end /*k*/; rows=k; cols=k
|
||||
|
||||
w=words(m); do k=1; if k*k>=w then leave; end /*k*/; rows=k; cols=k
|
||||
call showMat M, 'M matrix'
|
||||
answer=matAdd(m, 2 ); call showMat answer, 'M matrix, added 2'
|
||||
answer=matSub(m, 7 ); call showMat answer, 'M matrix, subtracted 7'
|
||||
answer=matMul(m, 2.5); call showMat answer, 'M matrix, multiplied by 2½'
|
||||
answer=matPow(m, 3 ); call showMat answer, 'M matrix, cubed'
|
||||
answer=matDiv(m, 4 ); call showMat answer, 'M matrix, divided by 4'
|
||||
answer=matIdv(m, 2 ); call showMat answer, 'M matrix, integer halved'
|
||||
answer=matMod(m, 3 ); call showMat answer, 'M matrix, modulus 3'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SHOWMAT subroutine──────────────────*/
|
||||
showMat: parse arg @, hdr; say
|
||||
L=0; do j=1 for w; L=max(L,length(word(@,j))); end
|
||||
say center(hdr, max(length(hdr)+4, cols*(L+1)+4), "─")
|
||||
n=0
|
||||
do r =1 for rows; _=
|
||||
do c=1 for cols; n=n+1; _=_ right(word(@,n),L); end; say _
|
||||
end
|
||||
return
|
||||
/*──────────────────────────────────one-liner subroutines───────────────*/
|
||||
matAdd: arg @,#; call mat#; do j=1 for w; !.j=!.j+#; end; return mat@()
|
||||
matSub: arg @,#; call mat#; do j=1 for w; !.j=!.j-#; end; return mat@()
|
||||
matMul: arg @,#; call mat#; do j=1 for w; !.j=!.j*#; end; return mat@()
|
||||
matDiv: arg @,#; call mat#; do j=1 for w; !.j=!.j/#; end; return mat@()
|
||||
matIdv: arg @,#; call mat#; do j=1 for w; !.j=!.j%#; end; return mat@()
|
||||
matPow: arg @,#; call mat#; do j=1 for w; !.j=!.j**#; end; return mat@()
|
||||
matMod: arg @,#; call mat#; do j=1 for w; !.j=!.j//#; end; return mat@()
|
||||
mat#: w=words(@); do j=1 for w; !.j=word(@,j); end; return
|
||||
mat@: @=!.1; do j=2 to w; @=@ !.j; end; return @
|
||||
answer=matAdd(m, 2 ); call showMat answer, 'M matrix, added 2'
|
||||
answer=matSub(m, 7 ); call showMat answer, 'M matrix, subtracted 7'
|
||||
answer=matMul(m, 2.5); call showMat answer, 'M matrix, multiplied by 2½'
|
||||
answer=matPow(m, 3 ); call showMat answer, 'M matrix, cubed'
|
||||
answer=matDiv(m, 4 ); call showMat answer, 'M matrix, divided by 4'
|
||||
answer=matIdv(m, 2 ); call showMat answer, 'M matrix, integer halved'
|
||||
answer=matMod(m, 3 ); call showMat answer, 'M matrix, modulus 3'
|
||||
exit /*stick a fork in it, we're all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
matAdd: parse arg @,#; call mat#; do j=1 for w; !.j=!.j+#; end; return mat@()
|
||||
matSub: parse arg @,#; call mat#; do j=1 for w; !.j=!.j-#; end; return mat@()
|
||||
matMul: parse arg @,#; call mat#; do j=1 for w; !.j=!.j*#; end; return mat@()
|
||||
matDiv: parse arg @,#; call mat#; do j=1 for w; !.j=!.j/#; end; return mat@()
|
||||
matIdv: parse arg @,#; call mat#; do j=1 for w; !.j=!.j%#; end; return mat@()
|
||||
matPow: parse arg @,#; call mat#; do j=1 for w; !.j=!.j**#; end; return mat@()
|
||||
matMod: parse arg @,#; call mat#; do j=1 for w; !.j=!.j//#; end; return mat@()
|
||||
mat#: w=words(@); do j=1 for w; !.j=word(@,j); end; return
|
||||
mat@: @=!.1; do j=2 to w; @=@ !.j; end; return @
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
showMat: parse arg @, hdr; L=0; say
|
||||
do j=1 for w; L=max(L,length(word(@,j))); end
|
||||
say center(hdr, max(length(hdr)+4, cols*(L+1)+4), "─")
|
||||
n=0
|
||||
do r =1 for rows; _=
|
||||
do c=1 for cols; n=n+1; _=_ right(word(@,n),L); end; say _
|
||||
end
|
||||
return
|
||||
|
|
|
|||
|
|
@ -1,27 +1,26 @@
|
|||
/*REXX program multiplies two matrixes together, shows matrixes & result*/
|
||||
/*REXX program multiplies two matrixes together, displays the matrixes and the result. */
|
||||
m=(1 2 3) (4 5 6) (7 8 9)
|
||||
w=words(m); do k=1; if k*k>=w then leave; end /*k*/; rows=k; cols=k
|
||||
|
||||
w=words(m); do k=1; if k*k>=w then leave; end /*k*/; rows=k; cols=k
|
||||
call showMat M, 'M matrix'
|
||||
ans=matOp(m, '+2' ); call showMat ans, 'M matrix, added 2'
|
||||
ans=matOp(m, '-7' ); call showMat ans, 'M matrix, subtracted 7'
|
||||
ans=matOp(m, '*2.5' ); call showMat ans, 'M matrix, multiplied by 2½'
|
||||
ans=matOp(m, '**3' ); call showMat ans, 'M matrix, cubed'
|
||||
ans=matOp(m, '/4' ); call showMat ans, 'M matrix, divided by 4'
|
||||
ans=matOp(m, '%2' ); call showMat ans, 'M matrix, integer halved'
|
||||
ans=matOp(m, '//3' ); call showMat ans, 'M matrix, modulus 3'
|
||||
ans=matOp(m, '*3-1' ); call showMat ans, 'M matrix, tripled, less one'
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*──────────────────────────────────SHOWMAT subroutine──────────────────*/
|
||||
showMat: parse arg @, hdr; say
|
||||
L=0; do j=1 for w; L=max(L,length(word(@,j))); end
|
||||
say; say center(hdr,max(length(hdr)+4,cols*(L+1)+4),"─")
|
||||
n=0
|
||||
do r =1 for rows; _=
|
||||
do c=1 for cols; n=n+1; _=_ right(word(@,n),L); end; say _
|
||||
end
|
||||
return
|
||||
/*──────────────────────────────────one-liner subroutines───────────────*/
|
||||
matOp: arg @,#;call mat#; do j=1 for w; interpret '!.'j"=!."j #;end; return mat@()
|
||||
mat#: w=words(@); do j=1 for w; !.j=word(@,j); end; return
|
||||
mat@: @=!.1; do j=2 to w; @=@ !.j; end; return @
|
||||
ans=matOp(m, '+2' ); call showMat ans, "M matrix, added 2"
|
||||
ans=matOp(m, '-7' ); call showMat ans, "M matrix, subtracted 7"
|
||||
ans=matOp(m, '*2.5' ); call showMat ans, "M matrix, multiplied by 2½"
|
||||
ans=matOp(m, '**3' ); call showMat ans, "M matrix, cubed"
|
||||
ans=matOp(m, '/4' ); call showMat ans, "M matrix, divided by 4"
|
||||
ans=matOp(m, '%2' ); call showMat ans, "M matrix, integer halved"
|
||||
ans=matOp(m, '//3' ); call showMat ans, "M matrix, modulus 3"
|
||||
ans=matOp(m, '*3-1' ); call showMat ans, "M matrix, tripled, less one"
|
||||
exit /*stick a fork in it, we"re all done. */
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
matOp: parse arg @,#; call mat#; do j=1 for w; interpret '!.'j"=!."j #;end; return mat@()
|
||||
mat#: w=words(@); do j=1 for w; !.j=word(@,j); end; return
|
||||
mat@: @=!.1; do j=2 to w; @=@ !.j; end; return @
|
||||
/*──────────────────────────────────────────────────────────────────────────────────────*/
|
||||
showMat: parse arg @, hdr; say
|
||||
L=0; do j=1 for w; L=max(L,length(word(@,j))); end
|
||||
say; say center(hdr,max(length(hdr)+4,cols*(L+1)+4),"─")
|
||||
n=0
|
||||
do r =1 for rows; _=
|
||||
do c=1 for cols; n=n+1; _=_ right(word(@,n),L); end; say _
|
||||
end
|
||||
return
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue