acer

32490 Reputation

29 Badges

20 years, 9 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

You cannot use an indexed name as the parameter of a procedure. The parameter name has to be also of type symbol.

There are two kinds of subscripted name, an indexed name and a subliteral (which is an atomic identifier). The latter of those two can be of type symbol, but the former is only of type name and is not of type symbol.

The easy way to get that, in 2D Math entry mode, is to type it using Ctl-Shift-minus instead of Ctl-minus to get the underscore between the base name and the subscript. Eg. the key strokes   c Ctl-Shift-minus A  instead of c Ctl-minus A

You'll have to be careful to enter them the same way, consistently, wherever they are used in the procedure body.


restart:

f := proc (`#msub(mi("c"),mi("A"))`, `#msub(mi("c"),mi("B"))`) options operator, arrow; `#msub(mi("c"),mi("A"))`+`#msub(mi("c"),mi("B"))` end proc

proc (`#msub(mi("c"),mi("A"))`, `#msub(mi("c"),mi("B"))`) options operator, arrow; `#msub(mi("c"),mi("A"))`+`#msub(mi("c"),mi("B"))` end proc

f(x,y);

x+y

lprint(eval(f));

proc (`#msub(mi("c"),mi("A"))`, `#msub(mi("c"),mi("B"))`) options operator, arrow; `#msub(mi("c"),mi("A"))`+`#msub(mi("c"),mi("B"))` end proc

 


Download subliterals.mw

acer

@j.scott.elder The failure upon cut & paste to respect the literal subscript seems undesirable to me too. (I lprinted the pasted 2D input, or pasted in as 1D, getting the same misconversion.)

But it appears to work ok (in Maple 16.01 at least) if the typesetting level is set to "extended" rather than "standard".

You can change that in either of two ways:

1) From the main menubar, select Tools->Options->Display and change the dropdown menu "Typesetting level".

2) Execute the command

          interface(typesetting=extended):

Maple's fsolve seems to have trouble finding some of the roots (considering it ill-conditioned perhaps).

The following gets 85 complex-valued roots for the univariate eq2 (degree 85 in omega). You may want to experiment with Digits=10 and also recompute at some higher working precision (eg. Digits=50, 100,...), and try and check the accuracy. Note, though, that an individual root could be correct to several places and still show a very high forward error if used as a numeric value of omega for evaluation of eq2.

sols := [RootFinding:-Analytic((lhs-rhs)(eq2),
                                omega=-100000-100000*I..100000+100000*I)]:

nops(sols);

acer

You could use single left-quotes (aka name quotes) and have `&not` as a postfix function call, or you could use no quotes for that term (in which case the brackets are superfluous).

with(Logic):

Equivalent( a &implies b, b &or `&not`(a) );

                              true

Equivalent( a &implies b, b &or &not a );

                              true

acer

The first problem is that not-equal is <> rather than != in Maple.

The second problem is that you cannot assign to the formal parameter that way. Easiest is to just use a pair of locals.

Egcd := proc(a, b)
local A, B, temp;
   A,B := a,b;
   while B <> 0 do
      temp := B;
      B := A mod B;
      A := temp;
   end do;
   return A;  
end proc;

acer

This does not seem to be true in the case that q is itself a multiple of 3.

The common factors I(n) and Z(q) may be removed from the two quantities being tested for equality. (The posted question and the uploaded worksheet disagree: one has Ic(n) as a multiplicative term and the other has it as an addititive term. But it doesn't matter which is intended, In both case those common terms may just be removed.)

restart:

X:=1/2*add(  cos(nwt - qptheta - (n-q)*(i-1)*2*Pi/3)
       + cos(nwt + qptheta - (n+q)*(i-1)*2*Pi/3), i=1..3):

# We are given n-q is an integer multiple of 3.
XX:=combine(expand(eval(X, n=q+3*k))) assuming k::integer;

         3                      1                   
   XX := - cos(nwt - qptheta) + - cos(nwt + qptheta)
         2                      2                   

        1    /  8                     \   1    /  4                     \
      + - cos|- - Pi q + nwt + qptheta| + - cos|- - Pi q + nwt + qptheta|
        2    \  3                     /   2    \  3                     /

Y:=3/2*cos(nwt-qptheta):

# We are interested in whether XX=Y, ie. whether XX-Y=0.
Z:=combine(expand(XX-Y));

              1                      1    /  8                     \
         Z := - cos(nwt + qptheta) + - cos|- - Pi q + nwt + qptheta|
              2                      2    \  3                     /

              1    /  4                     \
            + - cos|- - Pi q + nwt + qptheta|
              2    \  3                     /

expand(eval(Z, q=3*j+2)) assuming j::integer;

                                      0

expand(eval(Z, q=3*j+1)) assuming j::integer;

                                      0

expand(eval(Z, q=3*j)) assuming j::integer;

              3                         3                      
              - cos(nwt) cos(qptheta) - - sin(nwt) sin(qptheta)
              2                         2                      

This means that when q (and also n, due to the given properties) is an integer multiple of 3 the two expressions XX and Y are not equal for all nwt and qpdata.

acer

m:=Matrix([[1,2],[3,4]]);

                                      [1  2]
                                 m := [    ]
                                      [3  4]

T:=a.m;

                                       /[1  2]\
                              T := a . |[    ]|
                                       \[3  4]/

eval(T,a=3);

                                   [3   6]
                                   [     ]
                                   [9  12]

eval(T,a=Matrix([[0,-1],[-1,0]]));

                                  [-3  -4]
                                  [      ]
                                  [-1  -2]

acer

One possible approach is to construct a procedure `f` which would build the final 6x6 Matrix by calling `f(M)`, using explicit formulas in terms of all the entries in input 9x9 Matrix `M`.

You might construct such a re-usable procedure by doing the process on a general symbolic 9x9 Matrix and then using `unapply`.

restart:

with(LinearAlgebra):

# These steps are just for making re-usable procedure `f`.
# By making a re-usable procedure `f` you thus avoid the need to
# apply all these steps to each of your many 9x9 input Matrices.
R := Matrix(9,symbol=A):
R := ColumnOperation(R,[1,-1],1)[1..-1,1..-2]:
R :=    RowOperation(R,[1,-1],1)[1..-2,1..-1]:
R := ColumnOperation(R,[1,-1],1)[1..-1,1..-2]:
R :=    RowOperation(R,[1,-1],1)[1..-2,1..-1]:
R := ColumnOperation(R,[1,-1],1)[1..-1,1..-2]:
R :=    RowOperation(R,[1,-1],1)[1..-2,1..-1]:

# Now we can construct a procedure which has all the formulas
# for constructing the final result. Enter eval(f) to see its body.
f:=unapply(R,A):

M:=RandomMatrix(9);

                  [ 88   52  -33  -95   20   25  -22   57   27]
                  [                                           ]
                  [-82  -13  -68  -20  -61   94   45   27    8]
                  [                                           ]
                  [-70   82  -67  -25  -48   12  -81  -93   69]
                  [                                           ]
                  [ 41   72   22   51   77   -2  -38  -76   99]
                  [                                           ]
             M := [ 91   42   14   76    9   50  -18  -72   29]
                  [                                           ]
                  [ 29   18   16  -44   31   10   87   -2   44]
                  [                                           ]
                  [ 70  -59    9   24  -50  -16   33  -32   92]
                  [                                           ]
                  [-32   12   99   65  -80   -9  -98  -74  -31]
                  [                                           ]
                  [ -1  -62   60   86   43  -50  -77   -4   67]

f(M);

                       [  63  -57  135   80  -67  -50]
                       [                             ]
                       [  -2  -13  -68  -20  -61   94]
                       [                             ]
                       [-175   82  -67  -25  -48   12]
                       [                             ]
                       [  26   72   22   51   77   -2]
                       [                             ]
                       [  30   42   14   76    9   50]
                       [                             ]
                       [ 158   18   16  -44   31   10]

acer

P[0] is an indexed name, but it is not distinct from the name P. So when you assign (the eval result) to P you are clobbering P[0]'s earlier value.

Once you assign a Vector to P then Maple produces an error when you subsequently invoke P[0], because at that point P[0] is a indexed reference to the nonexistent 0th entry of Vector P. P[0] is no longer an assigned name.

One way to handle the issue is to use an entirely different name for either P[0] or P. Another related way is to turn P[0] into an "atomic identifier", which is a fancy term that basically means "distinct name".

The 2D Math for the atomic identifier gets displayed as the subscripted P[0], just like P[0] does in the Standard GUI. You can enter the atomic identifier for P[0] in at least three ways. 1) Using the subliteral item in the Layout palette, 2) Typing the usual P_0 while in 2D Math input mode and then applying the right-click context menu item 2-D Math -> Convert To -> Atomic Identifier to that input, and 3) entering it as the keystrokes P Ctl-_ 0 instead of the usual P _ 0. On my keyboard I get Ctl-_ as Ctl-Shift-minus.

acer

Try that last one as,

b[6..9,1]:=j[[6,5,4,3],1];

acer

I believe that the menu item that you've cited is available similarly to the rest of the Standard GUI's main menubar, and not from the Help window's menubar. The choices should be available by navigating the menubar Tools->Help Database->....  It is possible that the "Tools" item appearing in the main menubar (on Windows, Linux) is accessible in some different way on OSX. (Eg, on OSX Tools->Options is just Preferences, if I recall...)

I have had success using the command INTERFACE_HELP for managing my own personal .hdb Help database files programmatically. I prefer it, usually, over the makehelp command. For example, using the following code I can repeatedly replace and update a particular help item, in a personal .hdb file, after editing the original worksheet. It deletes the topic, and then adds it back.

restart:
worksheetlocation:=cat(kernelopts('homedir'),
                       "/Documents/hsvdisc2d.mw"):
wkstext:=Worksheet:-ReadFile(worksheetlocation):
wksstring:=Worksheet:-ToString(wkstext):
libname:=cat(kernelopts('homedir'),
             "/maple/toolbox/temp/lib"),libname:
try
streamcall(INTERFACE_HELP('delete',
               'topic'="hsvdisc3d",
               'library'=cat(kernelopts('homedir'),
                             "/maple/toolbox/temp/lib/maple.hdb")));
catch:
end try:
streamcall(INTERFACE_HELP('insert',
               'topic'="hsvdisc3d",
               'text'='TEXT'(wksstring),
               'library'=cat(kernelopts('homedir'),
                             "/maple/toolbox/temp/lib/maple.hdb")));

It's a matter of taste, but I prefer to use scripts like this for rebuilding my project's .mla and .hdb files. This is useful for when I forget the details, and can be less error prone than doing things manually. I keep the scripting Maple code either in plaintext .mpl or in .mw 1D notation Worksheets.

acer

You can create an HSV color disc in both 2D and 3D plots, but the Standard GUI seems to initially respond much better to single density plots as 3D plots.

Here's a 3D version, with axes supressed. And some simple animations that use it as a background. (Note, I inline here a jpg, which appears as nice as did the plot in the GUI. I had trouble exporting it as GIF -- see below.)

restart:
with(ImageTools):
N:=360:
p:=Array(1..N,1..N,1..3,(i,j,k)->
         `if`(k=1,(i-1),`if`(k=2,(j-1)/N,1)),
         datatype=float[8],order=C_order):
scaledp:=Preview(ImageTools:-HSVtoRGB(p),50): #faster, maybe
#scaledp:=Preview(ImageTools:-HSVtoRGB(p)): #no-scaling doesn't help gif export
with(plots):with(plottools):
T:=transform((x,y,z)->[y*cos(x*evalf(2*Pi/360)),
                       y*sin(x*evalf(2*Pi/360)),
                       z]):
hsvdisc:=T(scaledp):
display(hsvdisc,axes=none,view=0..1);

animate(pointplot3d,[[[360*t/(8*Pi)*cos(t),
                       360*t/(8*Pi)*sin(t),
                       1]],
                     color=black,symbolsize=15,symbol=solidcircle],
        background=hsvdisc, t=0..8*Pi, frames=100, axes=none,
        view=[-360..360,-360..360,0..2]);

animate(spacecurve,[[360*T/(8*Pi)*cos(T),
                     360*T/(8*Pi)*sin(T),
                     1],T=0..t,
                     color=black,thickness=3,numpoints=150],
        background=hsvdisc, t=0..8*Pi, frames=100, axes=none,
        view=[-360..360,-360..360,0..2]);

Exporting a 2D or 3D densityplot to GIF image file format seems to give a patterned blotchy result. The result with JPEG or BMP looks ok, much smother color gradation. I tried programmatic `plotsetup` export, as well as right-click export from the plot after manually resizing it much larger, in Maple 15 and 16. Maybe I missed something obvious (like grid size??). If you want exported animated GIF, then I guess this would have to be sorted out.

acer

If you want it fast then you could compute the random values up front, using Statistics:-Sample and then access them one at a time, as you need them, from that resulting Array/Vector.

It will take memory, however, to store all the precomputed values up front. It is also possible to take a middle road -- by precomputing in smaller batches. In recent Maple a float[8] Vector can be (memory efficiently) re-used repeatedly as the container into which one periodically computes batches of the random values.

Another way to precompute a sample of floats from the uniform distribution on [0,1) is with the `rtable` constructor, or more conveniently by accessing that though the LinearAlgebra package's commands `RandomVector` and `RandomMatrix`.

V:=LinearAlgebra:-RandomVector[row](3,generator=0.0..1.0,
                                 outputoptions=[datatype=float[8]]);

       V := [0.223811939491137, 0.585267750979777, 0.340385726666133]

X:=Statistics:-RandomVariable('Uniform'(0,1)):

S:=Statistics:-Sample(X,3);

       S := [0.751267059305653, 0.255095115459269, 0.505957051665142]

Statistics:-Sample(X,S):  # re-using S
S;

          [0.699076722656686, 0.890903252535798, 0.959291425205444]

You can compare with calling GenerateFloat() or rand() repeatedly. Note that `randomize` usually only needs to be called once, upon restart. The following is on 64bit Maple 16.01 for Windows 7.

restart:
randomize():
st := time():
X:=Statistics:-RandomVariable('Uniform'(0,1)):
S:=Statistics:-Sample(X,5000000):
for i from 1 by 1 to 5000000 do
S[i];
end do:
time() - st;

                                    2.792

restart:
randomize():
with(RandomTools[MersenneTwister]):
st := time():
for i from 1 by 1 to 5000000 do
GenerateFloat();
end do:
time() - st;

                                   11.170

restart:
randomize():
st := time():
for i from 1 by 1 to 5000000 do
rand()/(1000000000000.);
end do:
time() - st;

                                   29.063

Statistics:-Sample and the rtable constructors also respect the `randomize()` command.

acer

I can't tell from your phrasing whether you want to rearrange the entries of each row or of each column. If I've misinterpreted what you want then sorry; it's a trivial change to the code below to get the other effect.

If M is the original Matrix, then you can rearrange all the rows in one call, using rtable (Matrix/Vector/Array) indexing.

Eg,

M:=Matrix(7,7,(i,j)->i*11);

                           [11  11  11  11  11  11  11]
                           [                          ]
                           [22  22  22  22  22  22  22]
                           [                          ]
                           [33  33  33  33  33  33  33]
                           [                          ]
                      M := [44  44  44  44  44  44  44]
                           [                          ]
                           [55  55  55  55  55  55  55]
                           [                          ]
                           [66  66  66  66  66  66  66]
                           [                          ]
                           [77  77  77  77  77  77  77]

M[[4,6,7,2,3,1,5],1..-1];

                        [44  44  44  44  44  44  44]
                        [                          ]
                        [66  66  66  66  66  66  66]
                        [                          ]
                        [77  77  77  77  77  77  77]
                        [                          ]
                        [22  22  22  22  22  22  22]
                        [                          ]
                        [33  33  33  33  33  33  33]
                        [                          ]
                        [11  11  11  11  11  11  11]
                        [                          ]
                        [55  55  55  55  55  55  55]

 

See the subsection "Selecting Elements: Extracting Subblocks" of the rtable_indexing help-page.

acer

When you write that you want a rational polynomial approximation do you have in mind something like P(x)+S(x)/T(x) where P(x), S(x), and T(x) are each univariate polynomials in x with double precision floating-point coefficients (but possibly rewritten in Horner`s form)?

By 4 ulps do you mean in lp=`last place` hardware double precision (ie. in a scheme with 16 decimal digits)?

You can make do by considering only 0..Pi/4.

Is this close enough (when run compiled with Compiler:-Compile, or converted using CodeGeneration[C]), and do you need Maple code that generates it? I used numapprox:-minimax while computing it.

tanfun := proc(x::float) option inline;
  (0.9639674457083826e-34+(-0.1403264094762631e-30+(0.3402219607050792e-28
  +(.4427849600763185+(-0.9428452458811255e-1+(-0.2340000962851197e-2
  +(0.4982686721516939e-3+(-0.9260046118558648e-4+(0.1971789821665368e-4
  +(-0.4012888100718023e-5+(0.8544837764235923e-6+(-0.1784319759347114e-6
  +(0.3799383266698791e-7+(-0.7999539350949548e-8+(0.1703131269569653e-8
  +(-0.3594642906853935e-9+(0.7644190510188491e-10+(-0.1609484525183886e-10
  +(0.3390517969012429e-11+(-0.6966640403796986e-12+(0.1384318962492760e-12
  +(-0.2506934227750501e-13+(0.3905859966369083e-14+(-0.4494272751264981e-15
  +0.3027062135360529e-16*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)*x)
  *x)*x)*x)*x)*x)*x)*x)*x)*x)*x)/(1.328354880228956+(-.2828535737643377
  +(-.5383619549801358+.1146362355221901*x)*x)*x)+x end proc:

acer

First 262 263 264 265 266 267 268 Last Page 264 of 337