nm

8552 Reputation

19 Badges

13 years, 31 days

MaplePrimes Activity


These are questions asked by nm

After solving something, sometimes the result comes in piecwise. I'd like to just obtain all those results which are not undefined, into a list, so I can process them more easily. I am not sure what is the correct way to do this in Maple.

Here is an example.

restart;
f := piecewise(x>-1 and x<=0, x^3, 
               x>0 and x<=Pi/2, sin(x), 
               x>Pi/2, undefined, 
               x=20, 5, 
               x=50, undefined);

I need to pick all entries that do not have "undefined". I do not need the rhs condition  which is x<=1. So for the above, I'd like to obtain

                               sol:=[ x^3, sin(x), 5]

If there more entries which are also not undefined, they go into the list in order. 

when I do 

sol:=convert(sol,pwlist,x);

This gives     

sol := [0,-1,x^3,0,sin(x), (1/2)*Pi,undefined,20,undefined,20,undefined,50,undefined,50,undefined]

Now it gets confusing. How to change the above to 

       sol:=[ x^3, sin(x), 5]

I do not even understand the conversion result above. I do not know why it gives 50 twice. And I do not even see the "5" in there. So I do not know how to decode this now.

Update:

I found this solution. But it might not be best:

contents :=[op(f)];
[seq(`if`(contents[i]=undefined,NULL,contents[i]),i=2..nops(contents),2)];

gives

              [x^3, sin(x), 5]

I am learning patmatch. And found strange problem.

When I make an expression inside a proc A, and pass this expression to another proc B, which then uses patmatch  on it, the pattern fails to match.

but calling proc B directly, with exactly same expression, patmatch does match.

This is confusing and not sure why it happens. It seems related to using symbol which was declared local vs. same symbol but is global. Here is an example

restart;
foo:=proc()
  local x,C1;
  boo(exp(C1+x));
end proc;

boo:=proc(expr)
  local a,b,c,la;
  patmatch(expr,a::anything*exp(b::anything*C1+c::anything),'la');
end proc;

Call to foo() returns false. But call to boo() returns true. Even though I am using same expression.

foo();
   false


C1:='C1':
x:='x':
boo(exp(C1+x));
   true

  

What Am I doing wrong, and how to make it work in both cases?

Currently, I have some definitions of modules and or functions in one worksheet, say A.

To reduce clutter in A, I'd like to open second worksheet, say B, and invoke the functions defined in A in the B worksheet. This way I keep the definitions in one worksheet, and the tests/trying them, in another worksheet.  I do this all the time in Mathematica. It useful, as I like to keep one notebook for just definitions, and second one for testing and calling the functions.

I just found out, that a function defined in worksheet is not "seen" in worksheet B.  I looked at options and settings, googled a little, but not able to find an option to allow me to do this. I tried Tools->General->Share one engine among all documents. Clicked Apply. But that still did not do it.  From worksheet B, it tells me it does know the function I am trying to call. Only in worksheet A it can see the function.

Is there an option to allow one to have the function definition be seen on all open worksheets?

I know I could save the functions as mpl files, and then load them in the second worksheet. But during development, the first method is easier.

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. 

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?

First 123 124 125 126 127 128 129 Last Page 125 of 164