acer

32510 Reputation

29 Badges

20 years, 12 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

One of the advantages of ArrayInterpolation is that is can be used to compute interpolatory results for each input value in an Array in just a single call. If you have many values at which to interpolate, and if the input values do not depend upon each other, then this can improve performance as the problem size increases.

In your example the values at which to interpolate are the first column of Matrix `A`, and they are all known in advance of the need to interpolate. So you can bring a vectorized call to ArrayInterpolation outside of your do-loop.

InterProc1 := proc(N)
uses CurveFitting;
local i, A, variable1, variable2;
     A := Matrix(N, 4);
     variable1 := evalf([0, 100]);
     variable2 := evalf([12, 20]);
     for i from 1 to N do
          A(i, 1) := evalf(3+i);
          A(i, 2) := 2*i+A(i, 1);
          A(i, 3) := A(i, 2)-1;
          A(i, 4) := ArrayInterpolation(variable1, variable2, A(i, 1));
     end do;
     A;
end proc:

ans1 := CodeTools:-Usage( InterProc1(1000) ):
memory used=17.20MiB, alloc change=24.00MiB, cpu time=220.00ms, real time=228.00ms

InterProc1b := proc(N)
uses CurveFitting;
local i, A, variable1, variable2;
     A := Matrix(N, 4);
     variable1 := evalf([0, 100]);
     variable2 := evalf([12, 20]);
     for i from 1 to N do
          A(i, 1) := evalf(3+i);
          A(i, 2) := 2*i+A(i, 1);
          A(i, 3) := A(i, 2)-1;
     end do;
     A[1..N, 4] := ArrayInterpolation(variable1, variable2, A(1..N, 1));
     A;
end proc:

ans1b := CodeTools:-Usage( InterProc1b(1000) ):
memory used=459.64KiB, alloc change=0 bytes, cpu time=0ns, real time=11.00ms

The two results are the same.

LinearAlgebra:-Norm(ans1b-ans1);
                               0.

Some additional refinements might squeeze a bit more speed out of it (which might only be apparent for much larger sizes such as N=10^4 here). And perhaps more still could be done, reusing Vector workspaces and using ArrayTools:-Alias. But the subsequent improvements seem relatively minor compared to the improvement of making just a single call to ArrayInterpolation.

InterProc2 := proc(N)
uses CurveFitting;
local V1, i, A, variable1, variable2;
     A := Matrix(N, 4, datatype=float[8]);
     variable1 := Vector([0, 100], datatype=float[8]);
     variable2 := Vector([12, 20], datatype=float[8]);
     V1:=Vector(N, i->3+i, datatype=float[8]);
     A[1..N, 1] := V1;
     A[1..N, 2] := Vector(N, i -> 2*i+A[i,1], datatype=float[8]);
     A[1..N, 3] := Vector(N, i -> A[i,2]-1, datatype=float[8]);
     A[1..N, 4] := ArrayInterpolation(variable1, variable2, V1);
     A;
end proc:

ans2 := CodeTools:-Usage( InterProc2(1000) ):
memory used=171.74KiB, alloc change=0 bytes, cpu time=0ns, real time=7.00ms

LinearAlgebra:-Norm(ans2-ans1);
                               0.

acer

Student:-Calculus1:-Roots(cosh(C+cosh(C))/cosh(C)-2, C = -3 .. 3, numeric);

                  [-2.383208606, 0.3230741020]

acer

Your call to `animate` is passing A(tranz) which evaluates before `tranz` gets any numeric value. So `plot3d` is seeing just the valueless symbol `tranz`, and cannot produce a plot. That's what the error message is about.

This is a common usage mistake. There are several ways to correct for it. One easy way is to use so-called "uneval" quotes (single right-quotes) to delay the evaluation of the call A(tranz).

restart:

with(plots):
with(plottools):

A :=tranz-> plot3d(-2/sqrt(x^2+y^2)+5/sqrt((x-1.6)^2+y^2),
                   x = -5 .. 5, y = -5 .. 5,
                   view = [-5 .. 5, -5 .. 5, -5 .. 5],
                   transparency = tranz):
B := sphere([0, 0, 0], 2*(1/10), color = magenta, style = patchnogrid):
C := sphere([1.6, 0, 0], 5*(1/10), color = green, style = patchnogrid):
E := plot3d(0, x = -5 .. 5, y = -5 .. 5, view = [-5 .. 5, -5 .. 5, -5 .. 5],
            style = wireframe, shading = zgrayscale):

animate(display, ['A'(tranz), B, C, E, scaling = constrained,
                  view = [-5 .. 5, -5 .. 5, -5 .. 5],
                  axes = normal], tranz = 0.1 .. 0.9);

acer

rsolve( {x(n+2)-5*x(n+1)+6*x(n)=4*n+1, x(0)=1, x(1)=2}, x(n) );

                         n   7         3  n
                     -4 2  + - + 2 n + - 3 
                             2         2   

You can check that,

eval(sol,n=0);

                               1

eval(sol,n=1);

                               2

simplify( eval(sol,n=k+2)-5*eval(sol,n=k+1)+6*eval(sol,n=k) );

                            1 + 4 k

acer

You could compare the output from these various calls.

restart:
with(plots):

lambda1:=(S+sqrt(S^2+4*alpha))/(2):
lambda2:=(S-sqrt(S^2+4*alpha))/(2):

plot3d({lambda1,lambda2}, alpha=-5..5, S=-10..10);

isolate(lambda1=lambda2,alpha);

plot3d({lambda1,lambda2}, alpha=max(-1/4*S^2,-5)..5, S=-10..10);

plot3d({lambda1,lambda2}, alpha=-1/4*S^2..5, S=-10..10,
       view=[-5..5,-10..10,-10..10]);

contourplot({lambda1,lambda2}, S=-10..10, alpha=-5..5,
            contours=50, grid=[100,100]);

plottools:-transform((x,y)->[y,x])(contourplot({lambda1,lambda2},
                                                alpha=max(-1/4*S^2,-5)..5,
                                                S=-10..10, contours=50));

acer

restart:

f := x -> x^3:
g := x -> x^3:

addressof(eval(f));
                      18446744074339582038

addressof(eval(g));
                      18446744074339582126

evalb( f = g );
                             false

evalb( eval(f) = eval(g) );
                              true

is( f = g );
                             false

is( eval(f) = eval(g) );
                              true

restart:

f := x -> x^3:
g := t -> t^3:

addressof(eval(f));
                      18446744074339582038

addressof(eval(g));
                      18446744074339582126

evalb( f = g );
                             false

evalb( eval(f) = eval(g) );
                              true

is( f = g );
                             false

is( eval(f) = eval(g) );
                              true

acer

Use the currentdir command to find out where the write is being attempted. Eg, just issue

currentdir();

If that shows some location where you really shouldn't be trying to write your own files (eg. "C:/Program Files/Maple 17" then use that same command to set a more prudent location that is safer from the risk of inadvertantly clobbering one of Maple's own installed files.

For example,

currentdir(kernelopts(':-homedir'));

The location of your Maple installation is not a good place to be writing files. If you are fortunate then it will have been set write-protected by the installation process.

acer

It may happen that you want to expand the `sin` call (in the sense of using only the sum identity) but do not have at hand the terms to be temporarily frozen (via the double `subs`).

One can make a further distinction, according to which behaviour is wanted when the argument to `sin` is not a sum. It might be desired that the `sin` term be left alone, or it might be desired that it be handled in the usual `expand` manner.

Just for fun, here are two ideas. (It might be that applyrule alternatively could be used for part of it. I'd be surprised if they couldn't be improved or robustified.)

> restart:   
                                         
> expandsinsum := x -> evalindets(x,specfunc(`+`,sin),
>    q->sin(op([1,1],q))*cos(op([1,2..-1],q))
>       +cos(op([1,1],q))*sin(op([1,2..-1],q))):

> expandsinsum( 2*sin(x+Pi/4) );

                                   1/2           1/2
                           sin(x) 2    + cos(x) 2

> expandsinsum( 2*sin(2*x+Pi/4) );

                                   1/2             1/2
                         sin(2 x) 2    + cos(2 x) 2

> expandsinsum( 2*sin(2*x+4*k) );

                   2 sin(2 x) cos(4 k) + 2 cos(2 x) sin(4 k)

> expandsinsum( 2*sin(4*k) ); # not a sin of a sum; does nothing

                               2 sin(4 k)

> restart:                                                     

> expandtrigsum := x -> eval(expand( x,                        
>    op(map(op@op,indets(x,'specfunc(`+`,{sin,cos,tan})'))) )):

> expandtrigsum( 2*sin(x+Pi/4) );                              

                                   1/2           1/2
                           sin(x) 2    + cos(x) 2

> expandtrigsum( 2*sin(2*x+Pi/4) );                            

                                   1/2             1/2
                         sin(2 x) 2    + cos(2 x) 2

> expandtrigsum( 2*sin(2*x+4*k) );                             

                   2 sin(2 x) cos(4 k) + 2 cos(2 x) sin(4 k)

> expandtrigsum( 2*sin(4*k) ); # not trig of a sum; behaves like expand

                                      3
                      16 sin(k) cos(k)  - 8 sin(k) cos(k)

It's possible, then, to craft your own procedures which can produce a variety of more general behaviour.

acer

You wrote, "actually, my expectation is simple, just find back the original matrix from eigenvector and eigenvalue."

Your Matrix happens to be diagonaliable, and there are three linearly independent eigenvectors associated with its three eigenvalues. The 3x3 Matrix of its eigenvectors (as columns, like the `Eigenvectors` command returns) is invertible.

The eigen-solving, or the reconstruction, can be done for this example either as an exact symbolic or as an approximate floating-point computation.

restart:

with(LinearAlgebra):

A := `<|>`(`<,>`(1, 2,3), `<,>`(2, 3, 0), `<,>`(2, 0, 0));

                                [1  2  2]
                                [       ]
                           A := [2  3  0]
                                [       ]
                                [3  0  0]

v, EigenVector1:= Eigenvectors(A):

map(radnormal,EigenVector1.DiagonalMatrix(v).EigenVector1^(-1));

                              [1  2  2]
                              [       ]
                              [2  3  0]
                              [       ]
                              [3  0  0]

v, EigenVector1:= Eigenvectors(evalf(A)):

map(simplify@fnormal,EigenVector1.DiagonalMatrix(v).EigenVector1^(-1));

               [1.000000000  2.000000000  2.000000000]
               [                                     ]
               [         2.  3.000000000           0.]
               [                                     ]
               [3.000000000           0.           0.]

Note that not all Matrices have a full set of linearly independent eigenvectors which can form such an invertible Matrix. But even if that Matrix of eigenvectors is not invertible then the vectorized definition of eigenvalues & eigenvectors (A.x=lambda.x) can still hold. Ie, the following is very similar to the above, but both sides are not right-multiplied by the inverse of `EigenVector1` Matrix.

v, EigenVector1:= Eigenvectors(evalf(A)):

map( simplify@fnormal, A.EigenVector1 - EigenVector1.DiagonalMatrix(v) );

                            [0.  0.  0.]
                            [          ]
                            [0.  0.  0.]
                            [          ]
                            [0.  0.  0.]

v, EigenVector1:= Eigenvectors(A):

map( radnormal, A.EigenVector1 - EigenVector1.DiagonalMatrix(v) );

                              [0  0  0]
                              [       ]
                              [0  0  0]
                              [       ]
                              [0  0  0]

acer

By using cylindrical coordinates one can easily construct either a whole cone or a partial cone having a round base.

R, H := 3.2, 4.7;

plot3d(R*(H-h)/H, th=-Pi..Pi, h=0..H, coords=cylindrical);

plot3d(R*(H-h)/H, th=-3*Pi/2..0, h=0..H, coords=cylindrical, view=[-5..5,-5..5,0..H]);

Other options such as the 'view',etc, may also be supplied.

In contrast, using plottools:-cone produces only a full cone, while using the default rectangular coordinates (with trig functions) produces a structure which renders with straight edges on the polygonal primitives around its base.

acer

An alternate way might be,

f := x1^2/((x2^3)*(x3^2)):

subsindets(f, name^negint, q->1/cat(op(1,q),b)^op(2,q));

                           2    3    2
                         x1  x2b  x3b 

acer

There are various ways to get it. It's not clear whether your expression will always be so simple, or whether it might have some symbolic coefficient, whether it is always a ratio of multivariable polynomials, etc. How simple you can make it will depend on how restricted is the nature of the expression.

ee:=x^2*y^3/z^7:

(t->zip(`^`,t,map2(degree,ee,t)))([indets(ee,And(name,Non(constant)))[]]);

                          [ 2   3  1 ]
                          [x , y , --]
                          [         7]
                          [        z ]

[op(ee)];

                          [ 2   3  1 ]
                          [x , y , --]
                          [         7]
                          [        z ]

[indets(ee,And(name,Non(constant))^anything)[]];

                          [ 2   3  1 ]
                          [x , y , --]
                          [         7]
                          [        z ]

As just one simple example of how these behave for a slightly more general expression,

ee:=Pi^3*x^2*y^3/z^7:

(t->zip(`^`,t,map2(degree,ee,t)))([indets(ee,And(name,Non(constant)))[]]);

                          [ 2   3  1 ]
                          [x , y , --]
                          [         7]
                          [        z ]

[op(ee)];

                       [  3   2   3  1 ]
                       [Pi , x , y , --]
                       [              7]
                       [             z ]

[indets(ee,And(name,Non(constant))^anything)[]];

                          [ 2   3  1 ]
                          [x , y , --]
                          [         7]
                          [        z ]

So you may be able to figure out whether `op` alone is enough.

acer

If you used the syntax,

(exp(x)-1)(1-exp(-x))

then you probably wanted this instead,

(exp(x)-1)*(1-exp(-x))

I don't know whether either of these will get you what you want for more constrained examples, but it might give you some ideas.

restart:

s:={A>E,F>Z,F<P,Z>E,P<A}:

sort([s[]],(a,b)->coulditbe(op(2,a)<op(1,b) or op(1,a)<op(2,b))=true) assuming op(s);

                    [E < A, E < Z, Z < F, F < P, P < A]

sort([s[]],(a,b)->is(op(2,a)<op(1,b) or op(1,a)<op(2,b))=true) assuming op(s);

                    [E < A, E < Z, Z < F, F < P, P < A]

acer

First 244 245 246 247 248 249 250 Last Page 246 of 337