DuncanA

703 Reputation

8 Badges

18 years, 142 days

MaplePrimes Activity


These are answers submitted by DuncanA

Both '.' and &* are non-commutative multiplication operators, but the evaluation rules are different.  With the dot (.) operator, evaluation is immediate.  &* is a neutral operator. A matrix multiplication expression containing &* remains unevaluated until a call to evalm is made.

A := LinearAlgebra:-RandomMatrix(2,2);
                                    [-50   45]
                               A := [        ]
                                    [-22  -81]
B := LinearAlgebra:-RandomMatrix(2,2);
                                    [50  -16]
                               B := [       ]
                                    [10   -9]
A . B;
                                [-2050   395]
                                [           ]
                                [-1910  1081]
A &* B;
                           [-50   45]    [50  -16]
                           [        ] &* [       ]
                           [-22  -81]    [10   -9]
evalm(%);
                                [-2050   395]
                                [           ]
                                [-1910  1081]

---

Duncan

The second loop does work but you don't see the output because the printlevel is set to 1 by default.  From Maple Help ?do: "The setting of printlevel causes the display of the results of all statements executed up to the level indicated by printlevel. By default, printlevel is initially set to 1. As a result, if nested loops are being used, it may be necessary to set printlevel to a higher value in order to see the results of all the statements in the loops."

Set printlevel := 2; and the output from the nested loop will be displayed.

---

Duncan

You have a portfolio of 28 stocks. I'm guessing that x(i) is the weight of stock i in the portfolio, i.e., the fraction of the investors wealth invested in stock i.  The sum of weights should add to 1.  In which case, the equation for V is wrong and should be


V := sum(x(i), i = 1 .. 28) = 1;

---

Duncan

The US matrix can be obtained by shifting one of the arguments to the delta function mod N.

N := 4:
delta := (a,b) -> `if`(a=b, 1, 0);  # Kronecker delta function
                    delta := (a, b) -> `if`(a = b, 1, 0)
Matrix(N, delta);
                                [1  0  0  0]
                                [          ]
                                [0  1  0  0]
                                [          ]
                                [0  0  1  0]
                                [          ]
                                [0  0  0  1]
new_delta := (a,b) -> delta((a) mod N,(b-1) mod N);
         new_delta := (a, b) -> delta(`mod`(a, N), `mod`(b - 1, N))
Matrix(N, new_delta);
                                [0  1  0  0]
                                [          ]
                                [0  0  1  0]
                                [          ]
                                [0  0  0  1]
                                [          ]
                                [1  0  0  0]

 

---

Duncan

To specify that x1,x2,y1,y2 are real use 'assume'.

For example,

assume(x1, 'real', x2, 'real', y1, 'real', y2, 'real');
A := Matrix([[1, 0, 0], [0, 1, 0], [0, 0, 1]]).exp(-I*(x1*x2+y1*y2));

When x1,x2,y1,y2 are assumed real, the complex conjugate sign over the products in the hermitian conjugate matrix disappear.

 

Also, if I understand you correctly, the upper shift matrices can be obtained using a circular shift of an identity matrix.  For example,

A := LinearAlgebra:-IdentityMatrix(4):
ArrayTools:-CircularShift(A, -1);
                                [0  1  0  0]
                                [          ]
                                [0  0  1  0]
                                [          ]
                                [0  0  0  1]
                                [          ]
                                [1  0  0  0]

---

Duncan

Based on the experiments I have conducted I believe the simplest option is to convert the data read in from Excel into a list of lists.

 

 

 

restart;

surveyData := ExcelTools:-Import("SurveyData.xls"):
L:=convert(surveyData, listlist);
         [[3.0, 29.5, 0.25], [3.0, 22.5, -0.25], [3.5, 43.0, 0.625]]
plots[surfdata](L, axes=boxed);


---

Duncan

The read command ?read  provides a simple way to share maple code between worksheets. If the base model is exported to a Maple Input file (.mpl) it can be read into other worksheets by a call to read.  To export the base model, open the base model worksheet and from the menu system select File->Export As.  This will open the "Export As" dialog box.  Choose "Maple Input (.mpl)" from the "Files of type" menu and save with an appropriate filename.  A call to 'read <filename>;' in the other worksheets will import the base model into those worksheets.

 ?savelib  or a shared kernel  ?worksheet/reference/kernelmodes may be alternatives to using read.

---

Duncan

Use the plot command to plot two lists of co-ordinate data, use solve to find coefficients of the equation, and use plots[display] to display two functions on the same plot.

 



 

restart;

x := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]:

y := [3.7, 9.39, 23.09, 58.60, 153.41, 409.43, 1103.63, 2988.98, 8112.08, 22036.47]:

Plot two lists of co-ordinates using the plot command.

plot(x, y);

 

Use the solve command to find the coefficients given two points from the list of co-ordinates.

eqs := [ seq ( a*x[i] + exp(b*x[i]) = y[i], i in [ 1, 2 ] ) ];
solve( eqs, [ a, b ] );

 

(1)

f := x -> .5*x + exp(.8*x);
g := x -> .7*x + exp(.9*x);

 

(2)

unassign('x');
p1 := plot(f(x), x = 1 .. 10,color=blue):
p2 := plot(g(x), x = 1 .. 10,color=green):

Display two separate functions on the same graph using the plots[display] command.

plots[display]([p1, p2]);

 

 

 



Download exp_plot.mw

 

---

Duncan

If the execution of S2 is traced it can be seen that S2 is being called recursively, but never exits

trace(S2);
                                     S2
S2(p,2,2);
{--> enter S2, args = p, 2, 2
{--> enter S2, args = p, 2, `+`(2, `-`(m))
{--> enter S2, args = p, 2, `+`(2, `-`(`*`(2, `*`(m))))
{--> enter S2, args = p, 2, `+`(2, `-`(`*`(3, `*`(m))))
{--> enter S2, args = p, 2, `+`(2, `-`(`*`(4, `*`(m))))
{--> enter S2, args = p, 2, `+`(2, `-`(`*`(5, `*`(m))))
{--> enter S2, args = p, 2, `+`(2, `-`(`*`(6, `*`(m))))
   :
   :
   :
{--> enter S2, args = p, 2, `+`(2, `-`(`*`(2407, `*`(m))))
{--> enter S2, args = p, 2, `+`(2, `-`(`*`(2408, `*`(m))))
{--> enter S2, args = p, 2, `+`(2, `-`(`*`(2409, `*`(m))))

Using add in place of sum gives the correct answer

trace(S3);
                                     S3
S3(p,2,2);
{--> enter S3, args = p, 2, 2
{--> enter S3, args = p, 2, 0
                                      1
<-- exit S3 (now in S3) = 1}
                                  p (1 - p)
<-- exit S3 (now at top level) = `*`(p, `*`(`+`(1, `-`(p))))}
                                  p (1 - p)
expand(%);
                                        2
                                   p - p

where

S3 := (p, k, n)-> piecewise(0 <= n and n < k, 1, n < 0, 0, add(add(p^y*(1-p)^(m-y)*S3(p, k, n-m), m = y+1 .. y+k-1), y = 1 .. n-k+1));

I don't have a good explanation why sum doesn't work apart from saying that sum is designed for symbolic summation.

---

Duncan

The plot will appear if the multiplication dots in the expression are replaced by asterisks.



restart:

with(DEtools):

dfieldplot(diff(y(x),x) = 2*x - 2*x*y(x),y(x),x=-2..2,y=-3..3);

 

 



Download dfieldplot.mw

---

Duncan

There is a parametric plot ?plot,parametric.  But if you want to use the actual x[i], y[i] values these can be plotted as separate vectors. Increasing the value of 'n' below will give a smoother plot.



 

restart;

a := 2: b := 1:
plot([a*sin(t), b*cos(t), t=-Pi..Pi], scaling=constrained);

 

n := 10:

a := 2: b := 1:
d := 2 * Pi / n:

t := [seq(-Pi + d*i, i = 0 .. n)]:

x := Vector[row](n+1, i -> a*sin(t[i])):

y := Vector[row](n+1, i -> b*cos(t[i])):

plot(x, y, scaling=constrained);

 

 

 



Download plot,parametric.mw

 

---

Duncan

Use the Copy command in the LinearAlgebra package.



 

A := Matrix(3,3,1);

(1)

C:=LinearAlgebra:-Copy(A);

(2)

C[2,2]:=3;

(3)

C;

(4)

A;

(5)

 

 



Download LinearAlgebra,Cop.mw

 

The ArrayTools package also has a Copy command that performs a similar function.

---

Duncan

According to the MapleNet documentation it is possible to specify a callback in the html file, i.e., "a JavaScript function to invoke when the Maplet Shutdown command is invoked." See Listing 6 and Table 4 on http://www.maplesoft.com/documentation_center/maplenet/Publisher/MapleNet%20HTML-4-1.html

---

Duncan

There should be no semi-colon after 'test:=proc(alpha, beta)'. There should be a comma between 'lambda2' and 'Delta' in the local variable declaration.

 

test:=proc(alpha, beta)
local  itype::integer, lambda1, lambda2, Delta;

Delta := alpha^2-beta;
lambda12 := sqrt(sqrt(beta)-alpha);
lambda22 := sqrt(sqrt(beta)+alpha);
lambda13 := sqrt(-alpha+sqrt(alpha^2-beta));
lambda23 := sqrt(alpha+sqrt(alpha^2-beta));
lambda14 := sqrt(-alpha);
lambda24 := sqrt(-alpha);
lambda15 := sqrt(alpha);
lambda25 := sqrt(alpha);
lambda16 := sqrt(alpha-sqrt(alpha^2-beta));
lambda26 := sqrt(alpha+sqrt(alpha^2-beta));
lambda17 := sqrt(-alpha+sqrt(alpha^2-beta));
lambda27 := sqrt(-alpha-sqrt(alpha^2-beta));
lambda18 := 0.;
lambda28 := sqrt(-2.0*alpha);
lambda19 := 0.;
lambda29 := sqrt(2.0*alpha);
lambda1 := piecewise(alpha = 0. and beta = 0., 0., `and`(beta > 0., Delta < 0.), lambda12, beta < 0., lambda13, `and`(alpha < 0., Delta = 0.), lambda14, `and`(alpha > 0., Delta = 0.), lambda15, `and`(`and`(beta > 0., alpha > 0.), Delta > 0.), lambda16, `and`(`and`(beta > 0., alpha < 0.), Delta > 0.), lambda17, alpha < 0., lambda18, lambda19);
lambda2 := piecewise(alpha = 0. and beta = 0., 0., `and`(beta > 0., Delta < 0.), lambda22, beta < 0., lambda23, `and`(alpha < 0., Delta = 0.), lambda24, `and`(alpha > 0., Delta = 0.), lambda25, `and`(`and`(beta > 0., alpha > 0.), Delta > 0.), lambda26, `and`(`and`(beta > 0., alpha < 0.), Delta > 0.), lambda27, alpha < 0., lambda28, lambda29);
itype := piecewise(alpha = 0. and beta = 0., 1, `and`(beta > 0., Delta < 0.), 2, beta < 0., 3, `and`(alpha < 0., Delta = 0.), 4, `and`(alpha > 0., Delta = 0.), 5, `and`(`and`(beta > 0., alpha > 0.), Delta > 0.), 6, `and`(`and`(beta > 0., alpha < 0.), Delta > 0.), 7, alpha < 0., 8, 9);
return lambda1, lambda2, itype;
end proc;

 

---

Duncan

For n=60000 I get an unhandled win32 exception in mserver.exe [4504] and a "Kernel connection has been lost." message from Maple.  I'm using Maple 12.

---

Duncan

3 4 5 6 7 Page 5 of 7