MaplePrimes Questions

As an exercise to gain familiarity with the maple programming platform, I am attempting to repackage a geometric algebra program written for Maple V as a module. I have attached file NewGlyph.mpl containing the initialization code for a module to support the GA routines in the package. The text file was created in the Maple code editor (Maple 2018) and loaded by reading it into a worksheet (configured for maple input instead of 2D math). I have two questions: one: how to get the GAinit procedure to execute completely, and two: to understand the puzzling warnings produced by the code editor.  The behaviour I observe when the module is loaded into a worksheet is: NewGlyph:= a maple id.  At this point the load=setup procedure has registered type blade, but not executed the remaining procedures in the setup routine. When I execute with(NewGlyph) the exported procedures are displayed properly.  However, I still have to execute the GAversion procedure and
the GA_addmetric procedure manually to properly initialize the metric table in the module. It would appear that the module load process permits the execution of the addtype procedure but not the output of the version procedure or the metric table constructor. Is there a way to autoexecute these procedures when the NewGlyph module is invoked by the with(NewGlyph) command? Are there any criteria for the kinds of processes that can be included using option load=setup? My second question concerns the compiler warning raised by the code editor.  It warns that "These names were used as global names but were not declared: blade, sig. Also "These local variable were assigned a value but not otherwise used: cleanup.

NewGlyph := module ()
description "Basic Geometric Algebra Functions";
local setup, cleanup;
global e;
export GAversion,GAinit, GAadd_metric,GAmetric,GAmv;
option package, load = setup, unload = cleanup;

setup := proc ()
TypeTools:-AddType(`blade`, proc (a) type(a, indexed) and op(0, a) = 'e' end proc);
GAversion();
GAinit();
end proc;
setup();
cleanup:=proc()
TypeTools:-RemoveType(`blade`);
end proc;

GAinit := proc()
global `&@`, `&.`, `&^`, `&x`, `&s` , `&/`, `&l`, `&v`;
protect('`&@`','`&.`', '`&^`', '`&x`', '`&s`', '`&/`', '`&l`', '`&v`');
GAadd_metric('default', signum, -infinity, infinity);
GAadd_metric('SA', proc () 1 end proc, 1, 3);
GAadd_metric('STA', proc (i) `if`(i = 0, 1, -1) end proc, 0, 3);
GAadd_metric('MSTA', proc (i) `if`(modp(i, 4) = 0, 1, -1) end proc, 0, infinity);
GAadd_metric('Homogeneous', proc () 1 end proc, 0, infinity);
GAadd_metric('Conformal', proc (i) `if`(i = -1, -1, 1) end proc, -1, infinity);
GAmetric('default');
end proc:

GAversion := proc ()
print(`GA Package`);
print(`Version 2.0`);
print(`Written by: Mark Ashdown, maja1@mrao.cam.ac.uk`);
print(`Extended by: Alan Macdonald, macdonal@luther.edu`);
print(`Re-Packaged for Maple 2018 by: Ian McCreath`);
end proc;

GAadd_metric := proc (fred::name, signature::procedure, minind::{integer, infinity}, maxind::{integer, infinity})
global MetricTable;
unprotect('MetricTable');
MetricTable['fred'][sig] := eval(signature);
if maxind < minind then
ERROR(`The minimum index is greater than the maximum index`)
end if;
MetricTable['fred'][min] := minind;
MetricTable['fred'][max] := maxind;
protect('MetricTable');
print(`The GA package knows about metric ` . fred . `.`)
end proc:

GAmetric := proc (a::name)
global Metric, Sig, MetricTable, MinIndex, MaxIndex;
if member([a], [indices(MetricTable)]) then
unprotect('Metric', 'Sig', 'MinIndex', 'MaxIndex');
Metric := a;
Sig := MetricTable[Metric][sig];
MinIndex := MetricTable[Metric][min];
MaxIndex := MetricTable[Metric][max];
protect('Metric', 'Sig', 'MinIndex', 'MaxIndex');
print(`Metric is now set to: ` . Metric)
else
ERROR(`The metric ` . a . ` is unknown to the GA package`)
end if
end proc:

# Creates multivector with name x of with grade or set of grades g with
# indices ind
GAmv := proc(x::name,g::{nonnegint,set(nonnegint)},ind::set(integer))
local i;
if g::set then
add(procname(x,i,ind), i=g)
elif g > nops(ind) then
ERROR(`grade too large for number of indices.`)
elif g=0 then
x[NULL]
else
add(x[op(i)]*e[op(i)], i=combinat:-choose(sort([op(ind)]),g))
end if
end:

end module;

Download NewGlyph.txt

 

 

restart:
ode:=-diff(y(x),x,x)+(1/4)*y(x)-4*exp(-x)=0:
init:=y(0)=0,y(1)=0:
exact:=dsolve({ode,init},y(x)):
N:=8:
trial:=sum(c[i]*x^i,i=1..N):
residual:=lhs(subs(y(x)=trial,ode)):
R:=simplify(residual):
sys:=[]:
for i from 1 to N  do sys:=[op(sys),int(R*x^i,x=0..1)=0];    
od:
sys:
vars:=seq(c[i],i=1..N):
A,b:=LinearAlgebra[GenerateMatrix](sys,[vars]):
sol:=solve({op(sys)},{vars}):
assign(sol):
trial:

# if i am plotting the graph exact and trial i am getting the error, separately if i am plotting the graph of exact i am getting the plot, similarly exact - trial error
trial_fun:=unapply(trial,x):
exact_fun:=unapply(rhs(exact),x):
evalf(trial_fun(1)-exact_fun(1)):
plot(exact,trial,x=0..1):
Warning, unable to evaluate the function to numeric values in the region; see the plotting command's help page to ensure the calling sequence is correct
plot(trial,x=0..1,legend['trial']):
Warning, unable to evaluate the function to numeric values in the region; see the plotting command's help page to ensure the calling sequence is correct



#error calculation

plot(exact-trial,x=0..1):
Warning, unable to evaluate the function to numeric values in the region; see the plotting command's help page to ensure the calling sequence is correct


restart:

restart:

Eqn:=-diff(y(x),x,x)+(1/4)*y(x)-4*exp(-x)=0:
BCs:=y(0)=0,y(1)=0:
dsolve({Eqn,BCs},y(x)):
assign(%):y:=unapply(y(x),x):
plot(y(x),x=0..1);
 

 

When running this code in Maple 13 and 16 this works just fine,

GenMs:= (k::posint)-> assign(m||(1..2*k) =~ seq([dx||i, dy||i][], i= 1..k)):
GenMs(k), m || (1 .. 2*k);
gln := evalDG(l1 &s n1+`&s`(l2, n2)+sum('epsilon || i'*(cat(m, 2*i-1) &s cat(m, 2*i-1)+cat(m, 2*i) &s cat(m, 2*i)), i = 1 .. k));

But running the same code in Maple 2018 I get the following error:

Error, (in DifferentialGeometry:-Tensor:-SymmetrizeIndices) expected 1st argument to be a tensor. Received: `m2*i-1`^2

I don't know if it's related to Maple 2018 screwing with my tensor formatting:
https://gyazo.com/88eaeceda2e36cf411df44dbc4aa3ab6

(compare https://gyazo.com/b33396131165d66bbcf16e45d20cc579)

For whatever reason I just noticed that my inline &t's have been turned into `&t` but that's a separate issue.

I am learning how to do parsing in Maple. 

I want to check that a user supplied an expression with correct argument to y(*) from some complicated expression. So I need to find all instanced of y() to check that its agument is only y(x) and nothing else.  For example, given this 

restart;
expr:=y(x)^2+x+y(x)+2*1/y(z)+sin(x)+sin(y(x))+y+f(z)/Int(sin(y(x)),x)+y(x,y,z);

I need to obtain all these y(anything), like this

No matter where they show up in the expression. The above is just some made up example. The actual input will be a differential equation, and I want to check that the dependent variable y(x) has only x as its argument.

So I did the following

candidates:=convert(select(has,expr,y),list);

The problem now, is how to scan this list and check that each entry in it, the "y" in there has form y(x) and nothing else, so I can reject or accept the input. For example, the first one above is Ok, so the second one, but the third is not, since it function of z and not x. #4 is OK, #5 is not OK, since it has y without (x), and the last one is no OK, since it has 3 arguments, and so on.

I am not good in pattern matching in Maple. do I need to check match() for this? or patmatch()? If given single expresion like y(x), then I can handle it. I do something like

expr:=y(x);
if type(expr,'function') and nops(expr)=1 and op(0,expr)=y and op(1,expr)=x then
   print("OK");
else
   print("not ok");
fi;

But when the expression gets more complicated, like 1/y(z), then I need to check other things, and things gets complicated quickly. I think pattern matching is needed? or is there a better approach to do this that works in general? 

How does Maple do it internally? When I type

   dsolve(diff(y(x),x)+x+y(z)=0,y(x));

Error, (in dsolve) found the indeterminate function y with different arguments {y(x), y(z)}

dsolve(diff(y(x),x)+x+sin(y(z))=0,y(x));

Error, (in dsolve) found the indeterminate function y with different arguments {y(x), y(z)}

dsolve(diff(y(x),x)+x+y()=0,y(x));

So I need to do the same thing as Maple does. I looked at dsolve() code, but did not understand it how or where it does the parsing. 

Hello,

I am asking the students to write down the integral  (1/3)*int(10*cos(2*Pi*t/3),t=-1..1) without calculating it.  Both Maple TA in student answer, and Maple in my algorithm evaluate the integral. I just want the formula to be displayed not evaluated. How can I do that.  Thanks in advance.

I am using this proc by Carl Love posted here

https://www.mapleprimes.com/questions/211401-How-Do-I-Print-Text-Followed-By-Math

TSprintf:= proc() 
   local e;
   uses T= Typesetting; 
   T:-mrow(seq(`if`(e::string, T:-mn(e), T:-Typeset(T:-EV(e))), e= [args])) 
end proc:

when I do 

         TSprintf("Solving ", diff(y(x),x)=x);   

it works fine and it prints on the screen as expected. 

The problem is that inside a proc, if I use an error() later on, the message do not show up

foo:=proc()
   TSprintf("Solving ", _passed);   
   error "opps"
end proc;

And now when I call it like this 

  foo(diff(y(x),x)=x);
      Error, (in foo) opps   # Where the message "solving...." gone?? it does not show on the screen

I only see the "opps" and never see the message.  If I remove error(), then it shows up. But with standard printf, both show up

foo:=proc()   
   printf("Solving %a", _passed); 
   error "opps";
end proc;


foo(diff(y(x),x)=x);
    Solving diff(y(x),x) = x  # printf message shows OK
    Error, (in foo) opps      # from error 
 

Why TSprintf message do not show up if there is an error() after it?

Hello all,

     When using maple's differential equation solvers (dsolve or pdsolve), solutions will often have undetermined constants (or functions) _C1, _F2(x), _F5(t,x), etc. These are automatically generated and could potentially cause name collisions. I've found that passing names as part of the solver's ranking prevents this. For instance

D[1,1](y)(t,x) + y(t,x) = 0:
pdsolve(%);

would return y(t,x) = _F1(x)*cos(t) + _F2(x)*sin(t). However, if _F1(x,z) is defined elsewhere, then passing

D[1,1](y)(t,x) + y(t,x) = 0:
pdsolve(%, {y(t,x), _F1(x,z), _F2(x,z)});

instead returns y(t,x) = _F3(x)*cos(t) + _F4(x)*sin(t), preventing a collision.

However, it seems that PDEtools:-Solve doesn't follow this same convention. Indeed, calling PDEtools:-Solve(%, {y(t,x), _F1(x,z), _F2(x,z)}) on the above system returns y(t,x) = _F1(x)*cos(t) + _F2(x)*sin(t).

In all fairness, this is only a problem since my "colliding" functions are actually different functions: they differ in their arguments. When the arguments are the same (for instance, passing {y(t,x), _F1(x), _F2(x)} to Solve) then no collision occurs.

Nevertheless, having two different functions with the same name is causing problems for my code (later invocations of Solve with both _F1(x) and _F1(x,z) causes an "found functions with same name" error).

Is there any trick to avoid this behavior?

Thanks!

I am learning to use module().  If one has a local private proc() inside  a module, then on calling this local proc from inside the module itself, does one need to call it using module_name:-local_proc() or is it safe  to just call it using local_proc()?

i.e. will Maple always look to resolve this name inside the module first, before looking outside? What order Maple uses to resolve names? 

Here is an example

restart;
private_proc:= proc()
    print("Opps, should not be calling this, global copy");
end proc;

foo :=module()
    local private_proc;
    export public_proc;

    private_proc:=proc()
        print("inside private");
    end proc;

   public_proc:= proc()
        private_proc(); #will this always call foo:-private_proc() and not
                        #any other global proc with that name?                 
   end proc;

end module;

foo:-public_proc();

gives "inside private". So it seems to work without having to use foo:-private_proc(). But I thought to ask if the above will always work like this. 

Hi guys, 

I always thought matrices (Matrix) and 2D arrays (Array) where different objects in Maple.

In the attached file you will find a few examples of build-in functions which prove that Maple doesn't always check correctly the type of the arguments.

Beyond the questions set in the attached file, is there some tacit and unspoken rule in Maple which considers that Matrix and 2D Array have to be treated equally ?


Thanks for your answers

Matrix_or_Array.mw

For the most part, when I have come across bugs (or what appears to be bugs from my naive view, maybe it is left unevaluated for some means of diplomacy that I am not privy to) they are very difficult or impossible for me to find a way to get around.

But I have found alot recently that I was able to fix very quickly using functions that are already inbuilt in maple. Most have to do  with evaluating functions of trancendental arguements or composites of trancendental functions, but still, it leaves me feeling a little disturbed just how readily it was resolved.

I don't know exactly what to say, perhaps a 'dispatch' needed to be included in an inbuilt procedure to inform the 'engine' that a user has come across a bug of some kind at that address, so that maplesoft can keep the process of applying patches in house, avoiding having to enable the evolution/personal development the inept user that clearly stumbled upon the bug by sheer coincidence whist entering formula and observing output in a relatively sub human cognitive state?

  I want to calculate values of constants a[1] and b[1] by NonlinearFit model for two equations mentioned in below

(1/a[1]*x*t)*(1+a[1]*x^(2)*t+2*b[1]*x)    and  (1/b[1]*x*t)*(5*x*t+20*b[1]*a[1]*x*t^2+2*b[1]*x)

I know for the nonlinearfit model we need a data. But in my case, I do not have any data. So maybe data can be obtained by the numerical method, but how I do not know 

 

i want to write the code for picewise polynomial :

if i take h=10 ,interval [0,1] takes the values j =0,0.1,0.2,     so on up to 1 and the polynomial it should take 

(x-xj+1)/0.1,when x<0.1,

(x-xj+2)/0.1  when x<0.2

..... 

(x-xj+9)/0.1   when x<1

I only just noticed the Start.mw file being always number 1 in my Recent document list.  I don't recall it being there, ever, and I don't think it should.  Why is it there now?  Anyone else have this?

I am trying to show visually how many Lie derivatives of two different objects are needed to get a unique solution to a problem, so i want to create a graph of the form:


for the elements of this workseet:
3d_plot_of_Lie_derivatives_against_numelems.mw

Ok the error message I originally recieved in requesting 'expression' rather than 'value' was telling me the input is ambigous, isn't exactly helpful.

With the uploaded worksheet as last attempted, it gives the error of something about an invalid sequence, but still and I honestly do not see how when MathML is an XML application, surely whatever is put into the math container is retrievable considering XML applications are based on just handling the rendering of symbols.

 

Anyway I can only post here seeings that following the link for error messages rarely opens an actual help page in the brower, it simply tells you there is no help page for that error.

 


 

MathML[ImportModified](DocumentTools[GetProperty]('IIRN_CONTENT15', 'value'))

Error, (in MathML:-ImportModified) Typesetting:-merror("invalid sequence")

 

MathML[ImportModified](DocumentTools[GetProperty]('IIRN15', 'value'));

808066846690

(1)

 

``


 

Download 26052018.mw

First 819 820 821 822 823 824 825 Last Page 821 of 2429