acer

32485 Reputation

29 Badges

20 years, 7 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

It is not possible to have the entries get printed as m[i,j] with the same m as the variable to which the Matrix is assigned. In a related way, m[i,j] cannot be referenced for symbolic (unassigned, nonnumeric) i and j.

It is possible to use another symbol, however.

> m := Matrix(2,3,symbol=M);
                          [M[1, 1]    M[1, 2]    M[1, 3]]
                     m := [                             ]
                          [M[2, 1]    M[2, 2]    M[2, 3]]

> m[i,j];
Error, bad index into Matrix

There may be ways to use escaped local name m, or m rebound to a module export. But it's probably more effort than it's worth (and the created Matrix objects almost certainly couldn't get saved to .m or .mla and accessed in new sessions without encountering an infinite recursion. (I've tried..)

The now deprecated lowercase matrix has the property you describe, by default. That's a consequence of matrices having last_name_eval (which is a source of both good and evil).

> m := matrix(2,3):
> evalm(m);
                        [m[1, 1]    m[1, 2]    m[1, 3]]
                        [                             ]
                        [m[2, 1]    m[2, 2]    m[2, 3]]
 
> m[i,j];
                                    m[i, j]

acer

> a := Matrix(3,3,shape=symmetric):

> a[1,2]:=17:
> a[2,1];
                                      17

acer

This can be done quite easily with strings, by searching for longest substrings. The routine cat can be used to concatenate list entries into a string.

> F:=proc(x,b::posint,N::posint)
> local y,p:
>  if b>16 then error; end if;
>  y:=subs([10=A,11=B,12=C,13=D,14=E,15=F],
>          ListTools:-Reverse(
>    convert(floor(evalf[floor(N*ln(b)/ln(10))](x*b^N)),base,b))):
>  p:=nops(y)-N;
>  cat(op(y[1..p]),".",op(y[p+1..N]));
> end proc:
>
> S1 := F(sqrt(8),12,4500):
> S2 := F(sqrt(19),12,4500):

> StringTools:-LongestCommonSubString(S1,S2);
                                  "470B14568"

acer

Someone might post a more direct way, for your second example of SphericalY(1,1,phi,theta).

> expr := SphericalY(1,1,phi,theta):
> simplify(expand(combine(convert(expr,elementary)))) assuming phi::real;

                             1/2
                     -1/4 I 6    exp(theta I) | sin(phi) |
                     -------------------------------------
                                       1/2
                                     Pi

note: LegendreP does not automatically convert to elementary functions (as you seem to have suggested), although simplify can do that to it.

acer

While LinearAlgebra:-Equal (or the undocumented built-in, EqualEntries) are quite convenient for exact or symbolic Matrices, verify can also be used.

Careful comparison of floating-point Matrices or Vectors can be done using verify,Matrix along with verify,float. For example,

> M:=Matrix( 2, 2, [[0.3222, 0.5001], [1.0320, 0.9111]] ):
> S:=Matrix( 2, 2, [[0.3223, 0.5000], [1.03205, 0.911105]] ):

> verify( M, S, 'Matrix' );
                                     false
 
>  verify( M, S, 'Matrix(float(10^6))' );
                                     true
 
>  verify( M, S, 'Matrix(float(1,digits=5,test=2))' );
                                     false
 
>  verify( M, S, 'Matrix(float(10,digits=5,test=2))' );
                                     true

That last one tests that an absolute (test=2) entrywise comparison between M and S at 5 digits will have at most a difference of 10 units in last position (ulps). Which is true, since 0.50000 and 0.50010 vary by 10 ulps in an absolute comparison.

ps. People who write code often have their own distinctive style ("fist"). The variable name 'ee' is favoured by someone.

acer

You could also solve the characteristic polynomial of dFxdU, without having to convert to rationals.

solve(charpoly(dFxdU,lambda),lambda);
                                    (1/2)       /                      (1/2)\
                  rho u + (rho g pt)         1. \-1. rho u + (rho g pt)     /
u, u, u, u, u, u, -----------------------, - --------------------------------
                            rho                            rho               

BTW, you might wish to also consider using the more modern LinearAlgebra, VectorCalculus, and Matrix rather than linalg and array. It doesn't make things easier for your mixed symbolic+float dFxdU, but some aspects can be easier such as no need for evalm and no last_name_eval.

acer

An important difference is that you introduced floating-point values into dFxdU (eg, 0.5 rather than 1/2).

If I replace those three instances of 0.5 in the creation of dFxdU by the exact quanity 1/2 then I get these results,

> eigenvalues(dFxdU);
                      (1/2)                       (1/2)                  
    rho u + (rho g pt)         -rho u + (rho g pt)                       
    -----------------------, - ------------------------, u, u, u, u, u, u
              rho                        rho                             

If your matrix, with a mix of floats and multiple variables, has already been created then you can convert the floats to rationals like this,

> map(convert, dFxdU, rational);

ps. There are quite a few corners of Maple which have difficulty with the mix of multivariable expressions containing floating-point numbers. On the one hand, there are actually some poorly understood areas for such input. But there are also some tasks for which straightforward approaches can lead to decent answers (ie. for some such low order eigensystems, by computing the nullspace of the characteristic Matrix with lambda replaced by each explicit eigenvalue). There are parts of Maple where a practical approach is missing on the grounds that the general case is not possible or completely understood.

acer

It seems that your procedure p produces some nonnumeric results under evalhf (which is the numeric interpreter that plot uses by default).

> evalhf(p(0.02));
                              Float(undefined)

Try this, as a workaround,

plot(t -> evalf[Digits](p(t)), 0.0 .. 0.1);

acer

One way to look at it:
> rsolve(Q(k,x)=Q(k-1,x)+1,Q(n,x));
                                  Q(0, x) + n
 
> eval(%, Q(0,x)=x);
                                     x + n

acer

This one can also be done using int, without changing variables. (A matter of taste, whether figuring out the intersections is easier or not, for this example.)

> int(piecewise(x<1,2*x,2/x)-piecewise(x<1,1/x,x),x=1/sqrt(2)..sqrt(2));
                                   1      
                                   - ln(2)
                                   2      

acer

1) The command with should not be used inside a proc or a module. The help-page for with says that. I quote:

- The with command is effective only at the top level, and
  intended primarily for interactive use. Because with operates
  by using lexical scoping, it does not work inside the bodies
  of procedures, module definitions, or within statements.
  See the examples at the end of this help topic.

2) You have saved the module Phys_Quant_ex_0 with the global binding for diffindice. Rebinding diffindice interactively by issuing with(Math_Tools_ex) at the top-level in a new session will not affect the name diffindice that was saved in the module. (That is an intended part of the design of modules, and lots of stuff depends on that desirable behaviour.)

Either look at the help-page for use, or utilize the syntax Math_Tools_ex:-diffindice inside the body of module Phys_Quant_ex_0.

acer

Eigenvectors returns both the eigenvalues and the eigenvectors. So your assignments to (and use of) vec[1] and vec[2] are misguided.
> with(LinearAlgebra):
> M := Matrix(2, 2, [[r, r^2], [r^2, 1/r]]):
> vals,vecs := Eigenvectors(M):

> simplify(M.vecs-vecs.DiagonalMatrix(vals));
                                   [0    0]
                                   [      ]
                                   [0    0]

> simplify(vals[1]*vecs[1..-1,1] - M.vecs[1..-1,1]);
                                      [0]
                                      [ ]
                                      [0]

acer

It wasn't clear to me whether you wanted 1000 decimal digits or 1000 dozenal digits.

> F:=proc(x,b::posint,N::posint)
> local y,p:
> if b>16 then error; end if;
> y:=subs([10=A,11=B,12=C,13=D,14=E,15=F],
>         ListTools:-Reverse(
>   convert(floor(evalf[floor(N*ln(b)/ln(10))](x*b^N)),base,b))):
> p:=nops(y)-N;
> cat(op(y[1..p]),".",op(y[p+1..N]));
> end proc:

> # simple test case
> ans := F(sqrt(31793),12,60);
     ans := 12A.38075AB57660B4819B0B955BAA84365B892465A102072B7A53109A398

> Digits:=floor(60*ln(12)/ln(10)):

> convert(ans,decimal,12);
       178.3059168956543629146978561044118852999755964655120186918299562

> evalf(sqrt(31793)); # should be same as above
       178.3059168956543629146978561044118852999755964655120186918299562

> Digits:=10: # want F to work independent of top-level Digits setting

> # now, the posted question

> F(sqrt(2),12,1000);
1.4B79170A07B85737704B085486853504563650B559B8B79A401387B342380A998A173A951\
    303434821B55419A068816958B64282342A358A8947369B97237B9B04B656A072334932\
    8A219013A8B21AB42844A5758BA27B3A14317B17B28A4354B796260136269A55A79598A\
    4619BA2352A310A3373251B0598676B4537681A191A6901560B13362953A3B373054251\
    593693051410425656527080871A620766432B006383A272876409AB560250154713653\
    46AA731A9248B86B009972A5059115A10537765A3727300B71615798551101BB025B5A1\
    19781083699746484A9A0A5807960910B945AB250B74A6594723624594035156BB3A6A9\
    6559A453899500B6BB8811032B2332A74BB8070401B50A8A15BA2096184636714AB8894\
    749356151A36BA8AA424B6511A6AA35635A55848B5B4A9953B96478B317223B62700B28\
    4559B59A0AA34B6724497A247B53B8256881993B18A90A575862342586554334ABAB283\
    AA091186977782BB99734B16373A27B60A882935333325A1167A98A42B053831634948A\
    444A7572A993929440A412296997B297AA1810B79145B39974988B968B6731343532269\
    14236833678A02694B9A563B1017B953268692960AB384B15488A8B26808164413967B0\
    11056BA0A08BBAB3022935A1B6A096AA9A044836568294400477129BAA8048102482911\
    2B5

> length(%); # including the period
                                     1001

acer

Your data file is all ones and zeroes, so I presume you want that interpreted as a data set, say as discrete measurements of a square wave.

If that's right, then you could read it into Matrix using ImportMatrix, convert it to a Vector, and then run DiscreteTransforms:-FourierTransform on that. How you then wish to plot the frequency information is unclear, as you haven't mentioned anything about the sampling rate or time.

You can compare such DFT results with the continuous equivalent by searching  in google for  Fourier transform of a square  wave. (Eg, here, or the graphs here.)

I wonder, would it be useful for Maple to have something like Matlab's fftshift function?

acer

Could you use tensor[create] instead?

tensor[entermetric] eventually calls readline(), which calls iolib(2,..). And iolib is't checking IsWorksheetInterface() to acertain which interface is being used. So I don't see any way to trick it into behaving like it does in the commandline interface.

acer

First 299 300 301 302 303 304 305 Last Page 301 of 337