DJKeenan

442 Reputation

9 Badges

17 years, 187 days

MaplePrimes Activity


These are replies submitted by DJKeenan

In similar situations, what I have done is export the plots (here p12 and p0) and then combine them using an image manipulation program (I use The Gimp; Photoshop, etc., would also work). I often export&manipulate anyway, e.g. to position the caption or legend within the plot, to include a small version of one plot within another, etc.
Yet
p2:= plot(x^2, x=0..1):
plots:-display(p2,color='green');
still displays the plot with a red line. So presumably the color is already “defined locally in the … plot structure” even if it is not specified. Which is my aside question: why can't display overide this?
Yet
p2:= plot(x^2, x=0..1):
plots:-display(p2,color='green');
still displays the plot with a red line. So presumably the color is already “defined locally in the … plot structure” even if it is not specified. Which is my aside question: why can't display overide this?
I also have 11.02/Windows. You are only displaying p0, not the Matrix—i.e. you must have made an error when copying/entering the code from my comment.
I also have 11.02/Windows. You are only displaying p0, not the Matrix—i.e. you must have made an error when copying/entering the code from my comment.
IR4:= proc(n::posint) 
local i, M; 
M:= Matrix(n,n); 
(ArrayTools:-Alias(M, [n*n]))[[seq(n+(n-1)*i, i=0..n-1)]]:= -1; 
M 
end proc:
This takes advantage of scatter-point indexing of Vectors. Is there a way to do the scatter-point indexing directly on M?—and thus avoid using Alias.
IR4:= proc(n::posint) 
local i, M; 
M:= Matrix(n,n); 
(ArrayTools:-Alias(M, [n*n]))[[seq(n+(n-1)*i, i=0..n-1)]]:= -1; 
M 
end proc:
This takes advantage of scatter-point indexing of Vectors. Is there a way to do the scatter-point indexing directly on M?—and thus avoid using Alias.
Kind thanks for the explanation—I had erroneously understood that things were all solved.
The relative speed of KroneckerProd2 and KroneckerProd3 depends on the Matrices. For example, if
X:= Matrix(11, 29, [seq(.1*rand(), i=1..11*29)]):
Y:= Matrix(23, 19, [seq(.1*rand(), i=1..23*19)]):
then KroneckerProd3(X,Y) takes about 3.3 times as long as KroneckerProd2(X,Y).
Here is my attempt (from yesterday) to get the result on an element-by-element basis, but without looping.
KroneckerProd3:= proc(A::Matrix, B::Matrix) 
local a, b; 
description "Returns the Kronecker product of A and B";
uses LinearAlgebra;
a:= Dimension(A); b:= Dimension(B); 
Matrix(a[1]*b[1], a[2]*b[2], (i,j)-> A[1+iquo(i-1,b[1]), 1+iquo(j-1,b[2])]*B[1+irem(i-1,b[1]), 1+irem(j-1,b[2])]) 
end proc:
This is, though, slower than acer's quadruple loop.
As a long-term solution, perhaps it might be worth considering a full implementation of the Risch algorithm. This came up in another thread. A full Risch implementation would strengthen Maple's integration/DE capabilities; it would also be nice for users to know when an elementary antiderivative did not exist. It would take lots of work, but would be valuable.
My version above was just to illustrate a modification using Concatenate. A more direct way to write the procedure is this.
KroneckerProd2:= proc(A::Matrix, B::Matrix)
 local a, b, i, j;
 description "Returns the Kronecker product of A and B";
 uses ArrayTools, LinearAlgebra;
 a:= Dimension(A); b:= Dimension(B);
 if member(0,[a,b]) then Matrix(a[1]*b[1],a[2]*b[2]) 
 else Concatenate(1, seq(Concatenate(2, seq(A[i,j]*B, j=1..a[2])), i=1..a[1]))
 end if; 
end proc:
I prefer this version, although it can take slightly more time. But none of these versions are production quality. In particular, none of them handle Matrix options properly.
Mariner, kind thanks. I see that kronprod uses the linalg package, and returns an array (not an Array, or Matrix). The approach used is broadly similar to mine: use scalarmultiply and then turn the resultant matrices into a single array. linalg includes augment and stackmatrix functions, to get the single array. ArrayTools has Concatenation; I had misread the Help for that and thought it accepted only two Matrices (the Help is written clearly, the fault was mine). AHHH! So my code simplifies to the following.
KroneckerProd:= proc(A::Matrix, B::Matrix)
 local C, i;
 uses ArrayTools,LinearAlgebra;
 description "Returns the Kronecker product of A and B";
 C:= map(curry(ScalarMultiply, B), A);
 Concatenate(1, seq(Concatenate(2, op(convert(C[i,1..-1], list))), i=1..Dimension(A)[1])) 
end proc;
And of course Robert's code is production quality, and handles cases that the above code does not.
I prefer an applicative style, and so prefer the version that I came up with. We discussed this before, and to some extent it is a matter of taste. On the other hand, if you ever want to prove a program's correctness, an applicative style makes things greatly easier.
I agree that it would be nice for Maple to include Kronecker product natively. In the meantime, the following might do.
KroneckerProd:= proc(A::Matrix, B::Matrix) 
 local a, b, C, Catenate, i; 
 uses LinearAlgebra;
 description "Returns the Kronecker product of A and B"; 
 Catenate:= (d, A, B)-> `if`(LinearAlgebra:-Dimension(A)[d]=0, B, ArrayTools:-Concatenate(d, A, B)); 
 a:= Dimension(A); b:= Dimension(B); 
 C:= map(curry(ScalarMultiply,B), A);
 foldl(curry(Catenate,1), Matrix(0,a[2]*b[2]), seq(foldl(curry(Catenate,2), Matrix(b[1],0), op(convert(C[i,1..-1], list))), i=1..a[1])) 
end proc:
The local function Catenate should not be needed; it is used only because there is a bug in Concatenate, when the dimension is zero.
1 2 3 4 5 6 7 Last Page 3 of 13