acer

32333 Reputation

29 Badges

19 years, 326 days
Ontario, Canada

Social Networks and Content at Maplesoft.com

MaplePrimes Activity


These are answers submitted by acer

The following is in Maple 16.01, but the effects may be similar back in Maple 13.

Here are some choices in setup, for pretty-printing of derivatives.

restart;

kernelopts(version);

`Maple 16.01, X86 64 LINUX, May 6 2012, Build ID 744592`

interface(typesetting=standard):

diff(y(t),t,t)-4*diff(y(t),t)+5*y(t);

diff(diff(y(t), t), t)-4*(diff(y(t), t))+5*y(t)

interface(typesetting=extended):
Typesetting:-Settings(useprime=false, usedot=false):

diff(y(t),t,t)-4*diff(y(t),t)+5*y(t);

diff(diff(y(t), t), t)-4*(diff(y(t), t))+5*y(t)

Typesetting[DisableTypesetRule]("diff"):

diff(y(t),t,t)-4*diff(y(t),t)+5*y(t);

diff(diff(y(t), t), t)-4*(diff(y(t), t))+5*y(t)

Typesetting[EnableTypesetRule]("diff"):

Typesetting:-Settings(useprime=true, usedot=false, prime=t):

diff(y(t),t,t)-4*diff(y(t),t)+5*y(t);

diff(diff(y(t), t), t)-4*(diff(y(t), t))+5*y(t)

Typesetting:-Settings(useprime=false, usedot=true, dot=t):

diff(y(t),t,t)-4*diff(y(t),t)+5*y(t);

diff(diff(y(t), t), t)-4*(diff(y(t), t))+5*y(t)

 


Download diff_typesetting.mw

The different behavior in the following two examples makes me wonder whether/how the runtime scoping/evaluation is importantly different here, according to whether the inner procedure is "declared and assigned locally".

restart;

proc()
  local a := 0, b := 1;
  local f:=proc(x::uneval) print(x); x := b end proc;
  f(a);
end proc();
                               a
                               1

proc()
  local a := 0, b := 1;
  proc(x::uneval) print(x); x := b end proc(a);
end proc();
                               0
Error, (in unknown) illegal use of a formal parameter

Interestingly, the following "works" in Maple 10.03 but produces the error in Maple 11.00.

proc()
  local a,b;
   a := 0; b := 1;
   proc(x::uneval) print(x); x := b end proc(a);
end proc();

I also see the following item in the help page with topic updates,Maple11,compatibility

   Using the uneval or evaln parameter modifiers in procedures with
   optional or keyword parameters sometimes produced unpredicatable
   results in obscure cases. As such, the use of these modifiers has
   been improved in Maple 11, with clearly defined behavior. Some
   user-written procedures that use the troublesome combinations may
   need to have their parameter declarations slightly reorganized.

Your example does not use optional or keyword parameters, but I wonder whether these changes might have somehow affected parameter/lexical evaluation/scoping rules otherwise. (It might even be a "fix".)

[edited] I sent an email to a Maplesoft kernel developer and he confirmed that the emitted error was not expected. I will submit a bug report with details.

I'll add another variant. Experimentation shows that the behaviour can occur even when:
 - The argument passed to the inner procedure is not a local.
 - The local variable (of the outer procedure) is not assigned a value at the outer level.
 - The local variable (of the outer procedure) is referenced in the inner procedure, but not involved in the statement involving the problematic parameter.

restart;

c:=0:
proc()
  global c;
  local b;
  proc(x::uneval)
    print(x);
    #b;
    x:=1;
  end proc(c);
end proc();
                               c
                               1

restart;

c:=0:
proc()
  global c;
  local b;
  proc(x::uneval)
    print(x);
    b;
    x:=1;
  end proc(c);
end proc();
                               0
Error, (in unknown) illegal use of a formal parameter

So, it is the parameter modifier uneval (on a required parameter) being short-circuited during execution of a call to a nested anonymous procedure that happens also to reference a local from the outer level.

I figure that you don't want to have to pick off op(3,..) of a or numer(a), since that relies on visual inspection, and so would be done rather "by hand". I'd say the same thing about using applyop, which requires you to figure out visually which operand matters.

Having said that, here is something more programmatic. 

restart;

a := rho^(epsilon-1)*a__01^(-k)*(N__E2/Omega+N__E1*mu)*(K+1)/(mu);

rho^(epsilon-1)*a__01^(-k)*(N__E2/Omega+N__E1*mu)*(K+1)/mu

b:=rho^(epsilon-1)*a__01^(-k)*(N__E2/(Omega*mu)+N__E1)*(K+1);

rho^(epsilon-1)*a__01^(-k)*(N__E2/(Omega*mu)+N__E1)*(K+1)

type(denom(a), '`*`'(name)); # We're going to rely on this.

type(numer(a),`*`); # We're going to rely on this.

true

true

# Split multiplicands of numer(a) according to whether they are
# polynomial in any of the names in the product denom(a).

if type(denom(a), '`*`'(name)) and type(numer(a),`*`) then
  p1,p2 := selectremove(u->ormap(uu->degree(u,uu)>0,{op(denom(a))}),[op(numer(a))]);
  if nops(p1)>0 then
    res := expand(p1[1]/denom(a)) * `*`(op([2..],p1)) * `*`(op(p2));
  end if;
end if:
res;

rho^(epsilon-1)*a__01^(-k)*(N__E2/(Omega*mu)+N__E1)*(K+1)

 


Download div.mw

There are two kind of thing which pretty-print with subscripts. One is an indexed name, and the other is called a literal subscript.

It sounds as if you have inadvertantly used the wrong one, at some point. Or perhaps you have not been consistent in which one you used. (It's hard to tell which is the problem, without your complete worksheet.)

An indexed name is something like l[1] , l[3] . In both 1D plaintext and 2D Input mode you can just type those in as you see them here. But in 2D Input mode you can also enter those using the keystrokes such l Ctl-Shift-underscore 3, in which case the input is automatically turned into the pretty-printed, subscripted display. (In OSX on the Mac the keystrokes are Cmd-Shift-underscore I believe.) The underlying expression is still the indexed name.

A literal subscripted name has the underlying structure l__3 with two underscores. In both 1D plaintext and 2D Input mode you can just type those in as you see them here, and in 2D Input mode that automatically gets turned into the pretty-printed, subscripted display.

Download subscripts.mw

Those are name quotes.

You can convert to name, or use nprintf.

convert(x, name);
nprintf("%a", x);

If you are wanting this in order to concatenate multiple items as names, then note that nprintf can often do all those steps in one command.

[edited] note that this operation affects the printing. For example `1/2` will not pretty-print the same way that 1/2 does. In your related recent Questions that aspect turned out to be important to you.

I deliberately avoided mentioning symbol above. I figured that indexed names would just add confusion here.

I suspect that the OP only asked this Question so that he could use || to concatenate many items to a name. I also suspect that that's not the right approach anyway, since pretty-printing of the math terms might in fact be wanted.

The nprintf command is more suitable and straightforward than cat, for such concatenation (to a name).

restart;

Sol1:=1/4: Sol2:=1/5: Sol3:=1/7:

nprintf("Solution : A=%a, B=%a, C=%a.",Sol1,Sol2,Sol3);

`Solution : A=1/4, B=1/5, C=1/7.`

 


Download nprintf.mw

If you don't need the 1/2 pretty-printed as 2D Math then I suggest you go with Rouben's straightforward suggestion.

But if you want the 1/2 as an exact value and typeset in 2D Math then you could do it as follows. (There first example displays in the Maple's Standard GUI with the desired space after the word "for". That space is missing below due to a bug in the Mapleprimes inline renderer.)

ss1:=-2:  ss2:=1/2:  ss3:=-5:

 

print(`The 3 equations in A, B, C for`*s=ss1,s=ss2,`and `*cat(s,`=`,ss3,` :`));

`The 3 equations in A, B, C for`*s = -2, s = 1/2, `and `*`s=-5 :`

 

Or if you prefer an upright font for the words,

 

Typesetting:-mn(`The 3 equations in A, B, C for`)*s=ss1, s=ss2,
Typesetting:-mn(`and `)*cat(s,`=`,ss3,` :`);

Typesetting:-mn(`The 3 equations in A, B, C for`)*s = -2, s = 1/2, Typesetting:-mn(`and `)*`s=-5 :`

 


Download sent.mw

You could Explore a function call of the parameter.

restart;

m:=Matrix(1..10,1..10,(i,j)->(j/10.0)^(i)):

Explore((A->plots:-pointplot(m[A,1..10],[.1,.2,.3,.4,.5,.6,.7,.8,.9,1]))(a),
        parameters=[a=1..10]);
which is the same as,
restart;

m:=Matrix(1..10,1..10,(i,j)->(j/10.0)^(i)):

F:=A->plots:-pointplot(m[A,1..10],[.1,.2,.3,.4,.5,.6,.7,.8,.9,1]):

Explore(F(a), parameters=[a=1..10]);

It may well be that global optimization (using DirectSerach say) is the best way to go.

But I thought that I'd show that, at least for the given example, it is possible to find "all" the roots of the derivative system. Note that I use the output=numeric and method=RS options to RootFinding:-Isolate, and it may be that instead allowing an interval output could even encapsulate some small ranges within which a global minimum attains. On a fast machine it found 14 roots in about 15 minutes, and on a slightly slower machine the same in about 30 minutes. Of course that may be prohibitively long, for you. But it may be faster than other attempts.

It isn't clear to me how many digits are to be allowed in a solution. I mean, what is a very much better solution is only possible within a very restricted range of difference in some variable way out beyond the 15th decimal place?

And (for fun, mostly), I just added in some goofing around with Optimization:-Minimizepoly_opt.mw

First I clicked on the Download link on the webpage linked in the Question. That prompted me to download a file named adty_v1_0.tar.gz which  I saved to a new folder named wkptest located in my home directory.

Note my "home directory" is also the string that Maple returns when I issue the Maple command kernelopts(homedir) .

Then I unpacked that adty_v1_0.tar.gz file to the same folder (directory). (I use Linux, which has a utility tar for unpacking such files. On MS-Windows you may have to use something else. It's possible that modern MS-Windows will know how to extract the files by right-click.)

Two files were unpacked:

  exam_wkptest.mws
  wkptest_cpc

The first file, exam_wkptest.mws is a worksheet whose first few lines were commands to create a new Maple Library archive, and adjust libname . Below I'll show you how I changed those lines.

The second file wkptest_cpc contains the Maple source code for the wkptest package. NOTE that this source file also contains a call to savelib at its end. So DON'T use the Maple command read on it if you don't first make correct edits (as shown below) to adjust libname appropriately.

These are the lines that I removed from the exam_wkptest.mws file.

libname:=`i:\\ptest`,libname;
march(create,libname[1],100);
read`i:\\ptest\\wkptest_cpc`;

I replaced those lines in exam_wkptest.mws with the following:

restart:
sourcefolder:=cat(kernelopts('homedir'),"/wkptest");
installfolder:=cat(kernelopts('homedir'),"/maple/toolbox/wkptest/lib");
FileTools:-MakeDirectory(installfolder, 'recurse'=true);
libraryfile:=cat(installfolder,"/wkptest.mla");
try
  FileTools:-Remove(libraryfile);
catch:
end try:
LibraryTools:-Create(libraryfile);
libname:=libraryfile,libname;
read cat(sourcefolder,"/wkptest_cpc");

Note that the output of the Mapel command cat(kernelopts('homedir'),"/wkptest") matches the folder into which I unpacked the adty_v1_0.tar.gz file.

Those lines above, which build the Maple Library archive, only need to be executed once. I suggest saving a copy of the exam_wkptest.mws which does not contain them. (In Maple 18 at least, such a revised .mw/.mws file will need to be Closed and re-Opened in the GUI, for the examples to run.)

By creating the .mla Maple Library archive in an appropriate folder (directory) under cat(kernelopts('homedir'),"/maple/toolbox") there will be no need to adjust libname in future sessions in order to load and access the wkptest package.

After doing the above steps the package loaded file with the with(wkptest) command, and the examples in the .mws worksheet executed OK.

Note to others: The files in question are the intellectual property of that website. Please do not upload them to Mapleprimes without explicit permission of the authors.

Last week or so _Maxim_ mentioned wrapping calls involving large Arrays in try..catch, presumably so that an error could be rethrown with a terse (or no) message string.

I noticed that in Maple 18(?) the error message arising from an invalid call to say InageTools:-Embed would elide the large Array/Matrix from the formatted error string.

So sometime between Maple 18 and Maple 2017 something has changed. If I'm right then it would be worthwhile pinning down the version where it changed, perhaps figure out if it were accidental or deliberate regression, and (in either case) submitting a bug report.

 

Using two calls to ArrayTools:-BlockCopy this can be done very quickly.

For N=10^4 and P=5 my machine does it so quickly that the elapsed time is reported as 0 seconds, because the time is below the timer threshold. Memory allocation increase is not measurably more than the memory footprint of the result Matrix. Garbage collection effects are also too small to measure (except in kilobytes, ie. tiny).

The only caveat is that the datatype of A and B must match.  But that allows for the case where neither has a specified datatype. (And mixed float[8] and complex[8] could be accomodated with minor changes).

In the attachment below I show it running for unspecified datatype (ie. `anything`) as well as float[8]. For interest I compare with Tom's two methods.

If you need to re-use Matrix C from some earlier use then it'd be a trivial change to edit the procedure F so that C is passed in as an argument. Of course you could then add boilerplate to verify that dimensions match, etc.

restart;

F:=proc(a::Matrix,b::Matrix,N::posint,P::posint)
  local C, dta, dtb;
  dta:=rtable_options(a,':-datatype');
  dtb:=rtable_options(b,':-datatype');
  if dta<>dtb then error "expecting matrices with same datatype";
  end if;
  C:=Matrix(N,2*P,':-datatype'=dta):
  ArrayTools:-BlockCopy(A,0,N,N,P,C,0,2*N,N,P);
  ArrayTools:-BlockCopy(B,0,N,N,P,C,N,2*N,N,P);
  C;
end proc:

n:=4;
p:=5;

4

5

A:=LinearAlgebra:-RandomMatrix(n,p,generator=1..10);

_rtable[18446883882722325438]

B:=LinearAlgebra:-RandomMatrix(n,p,generator=-10..-1);

_rtable[18446883882722328438]

F(A,B,n,p);

_rtable[18446883882722327598]

nr:=1000:
nc:=5:
A:=LinearAlgebra:-RandomMatrix(nr, nc):
B:=LinearAlgebra:-RandomMatrix(nr, nc):

M:=CodeTools:-Usage( Matrix
                                        ( [ seq
                                            ( [A[..,k], B[..,k]][],
                                              k=1..op([1,2],A)
                                            )
                                          ]
                                        )
                                     ):

memory used=30.86MiB, alloc change=1.00MiB, cpu time=195.00ms, real time=196.00ms, gc time=0ns

P:=CodeTools:-Usage( Matrix
                                        ( ListTools:-Interleave
                                          ( [LinearAlgebra:-Column(A,[1..op([1,2],A)])],
                                            [LinearAlgebra:-Column(B,[1..op([1,2],B)])]
                                          )
                                        )
                                     ):

memory used=29.74MiB, alloc change=0 bytes, cpu time=192.00ms, real time=193.00ms, gc time=12.00ms

Q:=CodeTools:-Usage( F(A,B,nr,nc) ):

memory used=83.35KiB, alloc change=0 bytes, cpu time=0ns, real time=0ns, gc time=0ns

LinearAlgebra:-Norm(P-M);

0

LinearAlgebra:-Norm(P-Q);

0

A:=LinearAlgebra:-RandomMatrix(nr, nc, datatype=float[8]):
B:=LinearAlgebra:-RandomMatrix(nr, nc, datatype=float[8]):

M:=CodeTools:-Usage( Matrix
                                        ( [ seq
                                            ( [A[..,k], B[..,k]][],
                                              k=1..op([1,2],A)
                                            )
                                          ]
                                        )
                                     ):

memory used=30.04MiB, alloc change=0 bytes, cpu time=189.00ms, real time=191.00ms, gc time=8.00ms

P:=CodeTools:-Usage( Matrix
                                        ( ListTools:-Interleave
                                          ( [LinearAlgebra:-Column(A,[1..op([1,2],A)])],
                                            [LinearAlgebra:-Column(B,[1..op([1,2],B)])]
                                          )
                                        )
                                     ):

memory used=30.04MiB, alloc change=0 bytes, cpu time=196.00ms, real time=194.00ms, gc time=12.00ms

Q:=CodeTools:-Usage( F(A,B,nr,nc) ):

memory used=83.43KiB, alloc change=0 bytes, cpu time=0ns, real time=0ns, gc time=0ns

LinearAlgebra:-Norm(P-M);

0.

LinearAlgebra:-Norm(P-Q);

0.

 


Download interleavecolumns.mw

From that website you should be able to right-click and Save-As from your webbrowser, and then use File-Open in Maple's GUI.

But if that is a problem for you, here's that example opened directly from within Maple (done in two related ways).
 

restart;

kernelopts(version);

`Maple 2016.2, X86 64 LINUX, Jan 13 2017, Build ID 1194701`

str:=Import("http://www.yorku.ca/marko/ComPhys/maple6-files/SpecialRel.mws"):

# Write it to a file.
# Here I'm writing it out to "/tmp/SpecialRel.mws" on Linux.
# On MS-Windows you could use an appropriate location, say
# "C:/TEMP/SpecialRel.mws" or what have you.

filename := "/tmp/SpecialRel.mws";
FileTools:-Text:-WriteFile(filename, str);

"/tmp/SpecialRel.mws"

93552

# Open that saved .mws file in a new Standard GUI tab.

Worksheet:-DisplayFile(filename);

# Optionally, open it immediately in a new tab of the Standard GUI.

streamcall(INTERFACE_WORKSHEET(':-display', ':-text'=str));

 

 

Download import_mws.mw

A MathContainer component is a very poor choice for storing numeric data. That is borne out by the absurdity of having to paste in a whole Matrix (or set its `expression` to the whole Matrix), and the inability to efficiently extract entries.

A DataTable component is much more suitable to storing a Matrix or Vector. A DataTable is tied directly to an actual Matrix/Vector so programmatic extraction and update is as usual, and efficient. The data is stored in the document and available after restart or close/re-open. 

@Earl 

It is a common misunderstanding about the solve command that it somehow requires you to solve for all variables, or is otherwise weirdly picky about which.

A related misunderstanding is that when solve returns NULL it means that there is no solution at all for the supplied variables. But it can mean that there is no solution for the solving-variables that allows the remaining variables to be unrestricted.

The essence is in whether solve can return solutions in which all the variables are either free or defined in terms of those that are free.

In your example you cannot solve only for xp, since x and tp are otherwise restricted. And you cannot solve for just xpx, and tp because then t and v are still restricted. Even if you evaluate those particular expressions PrimeTime and PrimeLength beforehand at the special values of x and tp then solve would still need to get at least one of t or v as a solving variable, because t and v are restricted by each other.

So if you want to utilize solve for your example then you have to include at least one of t or v in the set of solving-variables (or pass the parametric option so that restrictions can be handled and expressed via piecewise, but that has limited support for inequalites and non-polynomial expressions).

Another command for this kind of thing (often overlooked) is eliminate. For your example it can return a formula for xp (and other solving-variable subsets) as well as the set of restrictions on the remaining variables.

When the second component of the list returned by eliminate is the empty set then there are no restrictions on the remaining variables, which means that the particular call to eliminate would also have succeeded with solve.

See attached: SpecialRelativity_1.mw

First 189 190 191 192 193 194 195 Last Page 191 of 336