pagan

5042 Reputation

23 Badges

13 years, 295 days

 

 

"A map that tried to pin down a sheep trail was just credible,

 but it was an optimistic map that tried to fix a the path made by the wind,

 or a path made across the grass by the shadow of flying birds."

                                                                 - _A Walk through H_, Peter Greenaway

 

MaplePrimes Activity


These are answers submitted by pagan

> algsubs(x^2=z,y=x^10-x^8);

                                      5    4
                                 y = z  - z

You don't need a procedure (or an arrow operator) for plotting. Maple can plot the expression like Int(...) too. The plot routines should even handle applying `evalf` for you automatically. But maybe you'd like to figure out the difference in behaviour you were seeing, regardless.

Is it possible that you are mixing up the formal parameters of procedure `f1` with the names at the higher (eg. global) level?

> restart:                                           

> blah:=sin(a*x):                                    

> f1:=(a,t)->evalf(Int(blah,x=0..t)):                

> f1(7,Pi/2);                                        

                             1.570796327
                            /
                           |
                           |             sin(a x) dx
                           |
                          /
                            0.

> evalf(Int(sin(7*x),x=0..Pi/2));
 
                                0.1428571429

> f1:=subs(BLAH=blah,(a,t)->evalf(Int(BLAH,x=0..t))):

> f1(7,Pi/2);                                        
                                 0.1428571429

> f1:=codegen[makeproc]('evalf'(Int(blah,x=0..t)),[a,t]):

> f1(7,Pi/2);                                            

                                 0.1428571429

The global `a` and `t` inside the expression `blah` are not the same as the formal parameters `a` and `t` of the first example of `f1` above. The first example is pretty much the same as doing

> f1:=(b,u)->evalf(Int(blah,x=0..u)):
           
> f1(7,Pi/2);                        

                             1.570796327
                            /
                           |
                           |             sin(a x) dx
                           |
                          /
                            0.

Does this get what you want?

> N := LinearAlgebra:-Dimension(BXFourier);

> shiftBXF := Vector[row](N, datatype = complex[8]);

> ArrayTools:-Copy(N/2, BXFourier, N/2-1, -1, shiftBXF, 0, 1);

> ArrayTools:-Copy(N/2, BXFourier, N/2, 1, shiftBXF, N-1, -1);

> listplot(abs(shiftBXF));

You can adjust it slightly, if you expect N might ever be odd.

One way, amongst many:

> res:=solve(sin((t-x)/2),t,AllSolutions);

                          x + 2 Pi _Z1

> eval(res,_Z1=0);

                               x

But maybe thats not so helpful, if the _Zn name changes n on you unpredictably and you don't know which _Zn name (ie. _Z4, _Z9, _Z1, etc) which which to  form that call. So, another way (for this simple example) might be:

> subsindets(res,`local`,0); # evalindets also works here

                               x

For more complicated examples you might need to handle the assumed parameters more carefully, subsituting for just those in which you're interested.

Maybe I read your question differently from others, but... are you just asking how to get S as a Matrix instead of a Vector?

# For A being an mxn Matrix, with S the Vector of singular values returned,
# then the DiagonalMatrix command can turn that S into a Matrix with the
# singular values along the main diagonal.

# eg.

LinearAlgebra:-DiagonalMatrix(S[1..min(m,n)],m,n);

Your posts indicates Maple 13, which had trouble with this. Maple 14.01 and Maple 15 may do better.

Maple 15:

> k1:=x->piecewise(x>=0 and x<=400, 155, x>400, 155+(x-400)*90):    
> k2:=x->piecewise(x>=0 and x<=200, 195-x, x>200, 555+(x-400)*90):

> solve(k1(x)=k2(x),x);                                           
                  RealRange(-infinity, Open(0)), 40, 3560/9

Maple 13.01:

> k1:=x->piecewise(x>=0 and x<=400, 155, x>400, 155+(x-400)*90):    
> k2:=x->piecewise(x>=0 and x<=200, 195-x, x>200, 555+(x-400)*90):

> solve(k1(x)=k2(x),x);
Warning, solutions may have been lost

> # You can try conversion to (and back from) Heaviside
> ans:=map(convert,[solve(convert(k1(x)=k2(x),Heaviside),x)],piecewise):

> # I doubt this is robust
> `union`(seq(`if`(type(T,piecewise),
>                  {map2(op,2,PiecewiseTools:-ToList(T))[]},{T}),T=ans))
> minus {undefined};
                                 {40, 3560/9}

You could also try combining/simplifiying k1(x)-k2(x) into a single piecewise, applying `solve` to each of its pieces and rejecting whichever solutions don't match the corresponding conditionals. That might get messier to code, in the presence of multiple solutions.

# Also not likely robust

> `union`(op(map(                                          
> proc(T)                                                  
>   local sol, z;                                          
>   sol:=solve(T[2],{x});                                  
>   {seq(`if`(evalb(subs(z,T[1]))=true,z,NULL), z in sol)};
> end proc,                                                
>     PiecewiseTools:-ToList(combine(k1(x)-k2(x))))));     

                             {x = 40, x = 3560/9}
> f:=sin(i*Pi/2)+cos(i*Pi/4)+i+A[i]*cos(i*6)+5*ln(5*r)*Pi+5+x:

> f1,f2:=selectremove(has,f,i):                               

> f1;

                      i Pi        i Pi
                  sin(----) + cos(----) + i + A[i] cos(6 i)
                       2           4

> f2;

                             5 + 5 ln(5 r) Pi + x

There is some funny business with dual-axis plots and the (programmatic, not right-click) GIF file format driver in the Standard interface. I wonder if you are seeing a related issue (with a different error message).

In this example below, it issues an error when called the second time, after setting plotdevice to GIF. But then the third attempt, using plots:-display(p1) actually produces the correct dual-axis gif format external file!

plotsetup('inline');

plotsetup();

       preplot = [], postplot = [], plotdevice = inline, 
       plotoutput = terminal, plotoptions = 

p3:=plots:-dualaxisplot(sin,sqrt):

plotsetup(gif,plotoutput=cat(kernelopts(homedir),"/zzz.gif"));

plots:-dualaxisplot(sin,sqrt): # this fails

Error, (in plots:-dualaxisplot) dual-axis plots are not available for the current interface or device

plots:-display(p3); # this works
> p1:=sin(x):                                        
> p2:=46:                                            

> seq(not(assigned(cat(p,i))),i=1..7);

                  false, false, true, true, true, true, true

> seq(`if`(assigned(cat(p,i)),NULL,cat(p,i)),i=1..7);

                              p3, p4, p5, p6, p7

Is this homework of some sort?

On the grounds of basic symmetry of your expression, you should be able to arrive yourself at a judgement as to whether that plot is corrrect near the point (x,y)=(0,0).

For your code attempt, that means whether the resulting plot is correct in what it shows as going on in the box x=[-0.5,0.5] and y=[-0.5,0.5].

So plot it again, for x=-0.01..0.01 and y=-0.01..0.01. Look for a direct discrepency with your earlier plot. For example, do the "connecting bits" occur in your second attempt in the same place as in the earlier plot? Repeat again. Now, do you think that they do actually connect like that, as some fixed range of the computation? Or do you think it could be an artifact of numerical computation problems inherent in the implicit solving?

Now look at what plots:-intersectplot() does, when viewed from above. Eg.

plots[intersectplot](1/4, x^2*y^2/(x^4+y^4),
                     x=-3..3,y=-3..3,axes=box,orientation=[0,0]);

Call intersectplot again as well, with narrower ranges.

Which do you prefer, the plot with a bit missing, or the plots with bits wrong?

[note: I am by no means suggesting that `intersectplot` is perfect, especially for this example.]

Easiest (to me) is to get rid of the `max` altogether. I don't know how to do that conceptual part in Maple. (I looked at the 3d case as a plot, and considered the symmetry...)

5! * 2^5 * Int(Int(Int(Int(Int(x^2,v=0..w),w=0..z),z=0..y),y=0..x),x=0..1):

CodeTools:-Usage( value(%) );
memory used=1.12MiB, alloc change=0.75MiB, cpu time=31.00ms, real time=31.00ms

                              160
                              ---
                               7 

I don't think you should need Maple for this step:

simplify( max(x^2,y^2,z^2,w^2) ) assuming w>0, w<z, z<y, y<x, x<1;

                                2
                               x 

The hard bit is figuring out the multiplicative factor, on account of the symmetries.

The parser gets to the code which constructs the module, and interprets I as sqrt(-1) before the interpreter runs it.

So, in the session in which you construct your module, you could call interface(imaginaryunit=ii) before the code that constructs the module.

I think I see what you intended. You invoked ModuleLoad(), which makes the `interface` call, inside the module's code. This would work for some actions (that you desired to occur even in the session which did not read the module from archive file) but not for what amounts to an adjustment in how the parser interprets the code up front.

restart;

interface(imaginaryunit=ii): # ..won't need this in new sessions where Q is read from archive
 
Q:=module()
 option package;
 export `*`, Qdef, Qrand, Qreal, Qimag, Qconj, Qnorm, Qinv, eval, LagP, M;
 local ModuleLoad;

 ModuleLoad:= proc()
  interface(imaginaryunit=ii):
 end proc;
 
 `*`:=proc(m,n)
  M(m,n);
 end proc;

define(M,'multilinear','flat','identity'=1,
M(a::nonunit(freeof({I,J,K}))*b::identical~({I,J,K}),c::algebraic)=a*M(b,c),
M(a::algebraic,c::nonunit(freeof({I,J,K}))*d::identical~({I,J,K}))=c*M(a,d),
M(a::freeof({I,J,K}),b::algebraic)=a*b,
M(a::algebraic,b::freeof({I,J,K}))=a*b,
M(I,I)=-1, M(I,J)=K,  M(I,K)=-J,
M(J,I)=-K, M(J,J)=-1, M(J,K)=I,
M(K,I)=J,  M(K,J)=-I, M(K,K)=-1);

end module:

with(Q):

M(a+b*x*I + c*x^2*J, d*I + x*K);

                                  2        2          3  
       d a I + x a K - b x d - b x  J - c x  d K + c x  I

If you savelib that module to a personal Library archive, then you shouldn't need to make the separate `interface` call in subsequent sessions which load it from archive. It would get done by the ModuleLoad.

Are you saying that you want `a` (or maybe `q`) to be updated whenever you move the slider? If that's the situation, then you can put code into the slider component. Right-click the slider and select "Component Properties". Click on "Action When Value Changes". In the pop-up window, you can write the code you want.

For example, if you have previously assigned a:=q^2 you could have the code be

q := DocumentTools:-GetProperty('Slider0','value');

Or, if you don't need `q` to get the actual value, but only care about what `a` becomes

a := eval( q^2, q=DocumentTools:-GetProperty('Slider0','value') );

Here's that second way: Download answer.mw

It's not a question of the different level of evaluation (ie. whether inside a procedure, or not).

It's a question of the binding of the operator at the time when the procedure is created.

> restart:
         
> m:=module() option package: export `*`;
> `*`:=proc(x,y) whatever; end proc:     
> end module:

> firstproc:=proc(s,t)
>   s * t;
> end proc:

> with(m);
                                      [*]

> secondproc:=proc(s,t)
>   s * t;
> end proc:

> firstproc(a, b);
                                      a b

> secondproc(a, b);
                                   whatever

A Library routine such as LinearAlgebra:-MatrixMatrixMultiply is created (and saved to archive) with the (usual) global :-`*` binding. Similar effects occur for kernel builtins.\

It's the same reason why the exported Units:-Standard:-`*` doesn't get called every time multiplication occurs inside Maple's stock Library procedures. In this next example, the only time it works inside any of these procedure calls is when the proc is defined after `*` is rebound due to `with` (although of course we could get the same effect by `uses` or `use` inside that proc defn).

> restart:    
                                     
> myMMM:=proc(m, n)                                
>   m[1,1] * n[1,1];                               
> end proc:   
                                     
> myMMM(Matrix([[Unit(N)]]),Matrix([[Unit(s^2)]]));

                                       [ 2]
                                   [N] [s ]

> with(Units:-Standard):    
                                                   
> myMMM(Matrix([[Unit(N)]]),Matrix([[Unit(s^2)]]));

                                       [ 2]
                                   [N] [s ]

> myMMM:=proc(m, n)                                
>   m[1,1] * n[1,1];                               
> end proc:              
                          
> myMMM(Matrix([[Unit(N)]]),Matrix([[Unit(s^2)]]));

                                    [m kg]

> LinearAlgebra:-MatrixMatrixMultiply(Matrix([[Unit(N)]]),Matrix([[Unit(s^2)]]));

                                  [    [ 2]]
                                  [[N] [s ]]

When you cut and paste products, you're re-executing them just as if you'd typed them in. So it's natural that you could see your exported `*` being used (if you'd with'd your package, say).

For some builtins, it is possible to actually unprotect and replace them (rather than merely export and rebind). This can actually be used to enhance and replace the prefix operator `*`, but it doesn't work for using the infix operator `*`.

See the ?module,package help page. But be careful about running that help page's Example without first attending to libname! (Hmmm, not a good help page Example... I ended up with MyPackage.m in a toolbox `lib` subfolder...)

Helloworld := module()
option package;
export proc1, proc2;
  proc1:=proc(x) sin(x); end proc;
  proc2:=proc(x) cos(x); end proc;
end module:

with(Helloworld);

                         [proc1, proc2]

proc1(2.2), sin(2.2);

                   0.8084964038, 0.8084964038

It's interesting that the (1D code) MyPackage example on ?module,package works, since the white space looks missing for the export and local statements (after the statement key word).

First 11 12 13 14 15 16 17 Last Page 13 of 48