Mac Dude

1561 Reputation

17 Badges

12 years, 273 days

MaplePrimes Activity


These are answers submitted by Mac Dude

@vs140580 I don't know anything about Windows. But Maple has the system() function that you can use to issue shell commands. In Unix land (Linux,macOS) you'd use something like

system("python pyfile.py");

This is not without pitfalls as the environment for the Unix process is bare and none of your paths etc. exist. This call does not wait & Maple does not get results, so your python program needs to save output to a file. An ssystem() call exists that does wait & can hand results back. Rtfm for details. I have had very limited success with this.

You will need to experiment to learn how Windows handles this.

For the other way look up maple in help. Your script can call the cmdline version (cmaple on Windows) and give it a .mpl file to run.

M.D.

Maple knows complex numbers and treats them as such..

Treating a complex solution a zero is usually a mistake. E.g if the solution to a quadratic equation is complex, it is not 0.

There is a RealDomain package (it may be named slightly different) that can  restrict certain operations to the domain of real numbers. The above hypothetical quadratic equation would then presumably yield an empty set or list of solutions, but not 0. I have not used this package.

M.D.

I have a large-ish package where I define a number of types. The way I do this is to put into the definition of ModuleLoad all my AddType calls.

At the end of the definition of my module I call ModuleLoad explicitly. My ModuleLoad also has a print stmt so I can see when it is called, and the explicit call is needed to make that print appear (in the notebook interface) upon loading.

I.e.:

Lattice:=module() option package;
... # most of the code of Lattice is here.

    ModuleLoad:=proc()

        TypeTools:-AddType(Element,'record(l,R)'); # defines type "Element"
...
    end proc; # ModuleLoad
...
    ModuleLoad(); # explicit call

end module; # Lattice

Module Lattice has submodules, and the defined types are recognized in there as well.

I hope this helps,

Mac Dude

@ianmccr Ok, I tried to get your sheet to work, no such luck. Mostly because I am utterly unfamiliar with Maple's workbook format. Is there any way you can make me a .mla file for Glyph2 so I can just use with(Glyph2)? Right now I am trying with Maple 2017; hopefully it will be able to run this; although I can use newer versions (on a different computer I have less access to). But I'd rather not have to wrestle with Maple's workbooks as I do't plan to use them anytime soon. Same actually for Maple cloud (I am not a cloudy kind of guy).

Now, I did gather from your comments above that you would like to identify subexpressions in a large expression. I have a routine that attempts just that, but I should try it out before just throwing it at you since I have no idea if it would work on large expressions like yours as it is a bit "brittle", meaning I have run into a number of situations where it does not work. In my case I use it occasionally to e.g. freeze such subexpressions so I can manipulate a larger expression at a higher level.

It probably would not just do what you want out of the box but could be something you could extend to fill your needs. In that spirit I am including it here.

(* whichop(xpr,subxpr)
   Finds the list of integers to be used in op to locate subxpr in xpr.
   For the time being only the first occurrence is found.
Parameters:
xpr         The expression to be searched.
subxpr      The expression to be searched for.
Returns a list of integers that can be used in op, subsop, etc.
*)
whichop:=proc(xpr,subxpr)
description "Find oplist of subxpr in xpr";
local oplist:=[];
local idx;

for idx from 1 to nops(xpr) do
  #print(xpr,nops(xpr),idx);
  if (has(op(idx,xpr),subxpr)) then
    #print(subxpr,idx);
    if (nops(xpr)=1) then # xpr >is< subxpr; we are done
      return;# don't return last 1, redundant
    else
      oplist:=[idx,whichop(op(idx,xpr),subxpr)[]];# append to oplist
      #print(oplist);
    end if;

    break;
  end if;
end do;

return oplist
end proc;

Hopefully I did not completely misunderstand you.

M.D.

Your examples can be done with map:

T := [[1, 2], [3, 4]];
map(convert,T,set,nested);
convert(%,set,nested);
                        [{1, 2}, {3, 4}]
                        {{1, 2}, {3, 4}}

This also works for your situation with choose(). If your nesting is deeper you may need more levels.

M.D.

There is no generally valid answer to your question (other than:. You need to analyse your applications and where the bottlenecks are.

  • Are you running primarily multithreaded or singe-threaded tasks? If single-threaded, then the raw power of each core will dominate your results, else having more cores will help.
  • Are you memory starved? Occasionally Maple processes can become memory hogs gobbling up significant amounts of memory. If the machine starts swapping it will slow down by a lot.
  • Do you know where your cycles go? Maple has facilities to profile usage of routines that can help identifying routines the system spends most of its time in.
  • Are you producing a lot of output into the worksheet? While the worksheet runs in a separate process, the necessary synchronization does slow things down.
  • And many more…

If you really want to soecifically speed up certain worksheets, profile them. If you have an itch to buy a new computer I'd suggest get the fastest single-core performance you reasonably can. Check the memory use on your machine and note that on macOS the actual use tends to be twice what Maple reports at the bottom of the worksheet.

Don't expect miracles from new hardware. If you look for a large factor speed-up, you will need to revisit the algorithm you use and its implementation.

M.D.

In your sheet IntPlaneX and IntPlaneZ are really the same planes, just expressed differently.

In your proc p, the test asks if a point is below or above this plane and changes the return value accordingly.

The plot shows the result. If you look at it along the plane you see that the red figure above and below is different, so it is not just a simple sphere.

What did you expect?

M.D.

It appears your Mma program does not use Mma packages and has a relatively simple structure, which at least opens up a chance to translate it with acceptable effort. I once translated an Mma program, but I used an editor to change some of the more routine things (like [] to () for functions, and I think the ";" in Mma suppresses output) before manually converting the rest.

Maple has the MmaTranslator package that may be helpful. However, don't expect to run the whole thing through it, it won't do you any good. Doing it statement-by-statement may have a chance to at least speed up the conversion. I have no experience with MmaTranslator, however.

Ultimately it depends how badly you want it.

It will be very helpful if you have access to Mma so you can check correctness of your conversion.

M.D.

@lcz For me, collect and simplify,size come close:

collect(k*(-n2+n+6-b)+(n2-b)*k+2*a,k);
                     (n + 6 - 2 b) k + 2 a
simplify(k*(-n2+n+6-b)+(n2-b)*k+2*a,size);
                     (n + 6 - 2 b) k + 2 a

Your specific desired form seems difficult to get without fairly cumbersome casting of operations to specific parts of the expression, with no generality under any changes of the expression.

M.D.

Try kernelopts(opaquemodules=false);

M.D.

While I don't consider Maple a true OO language, I often use the following construct which at least mimics the method overloading you are looking for:

# define the module ("class") 
Animal:=module()
   export data1,move,dog;

   move:=proc(_self,$)
     print("In Animal class. moving ....");
   end proc; 

   dog:=proc()

     return module()

       export move:=proc(_self,$)
         print("In dog class. moving ....");
       end proc;  

    end module;

  end proc;
 
end module;
 
# Use the module
A:=Animal;# Can only have one class!
                             Animal
Buster:=A:-dog():# this instantiates dog, can have multiples of this.
                        
Buster:-move();
                  "In dog class. moving ...."
A:-move();
                 "In Animal class. moving ...."

This is actually quite powerful & I use it in many packages. You can have a whole hierarchy of subclasses . Procedure dog is an example of a "Module Factory" and comes straight out of the Programming  Guide. Note that this programming model has been available since Maple 15 (much older than 2015 where the "object" option was introduced).

I hope this helps,

M.D.

Animal.mw

@janhardo So what is it you are actually asking for? Is it how to program a procedure in Maple, or is it how to program the Sieve?

There is a number of articles on the web on how to program the Sieve in Maple. A random one is https://www.cs.drexel.edu/~jjohnson/2008-09/spring/cs303/lectures/sieve.html (which provides a Maple worksheet).

If you are really after learning how to write procedures in Maple, there is no better resource than the Programming Guide. Sections 1.4 and 6 are relevant here. I do not understand your assertion that questions have no answers, maybe because I do not know your questions.

Study these materials and post here specific questions when you get stuck.

M.D.

It is not clear what your expression sequence is supposed to achieve, but interval arithmetic is done with evalr and INTERVAL:

evalr(INTERVAL(-1..1))*cos(x),evalr(cos(INTERVAL(0..2*Pi)));

gives

Note that the x from the second element of the sequence does not get substituted in the first element; you have to do that using substitute. (I just put it into the cosine to demonstrate what Maple does in his case.) Look up evalr() and subs() in the Help pages.

M.D.

Edit: Now reading your next question it looks like you may want a sequence of values. That can be achieved using the seq() function:

seq(evalf(cos(n*Pi/10)),n=0..20,0.1);

which gives a cosine table with 200 entries.

It has to do with the fractional exponent. The last sentence in help/series reads like this:

Usually, the result of the series function is represented in the form of a series data structure. For an explanation of the data structure, see the type/series help page. However, the result of the series function will be represented in ordinary sum-of-products form, rather than in a series data structure if it is a generalized series requiring fractional exponents.

So it seems to work as designed.

HTH

M.D.

PS: In my experience, convert will accept pretty much anything & try to convert to the desired form.

Well, you may be able to convince your teacher with an explicit solution, and yourself that such a situation is perfectly normal:

You have two values for an integral of an unknown function f(x), namely from 1 to 4 and from 1 to 6. If you give f(x) two parameters (call them a and b) and can find the integral for the two intervals given, then you can determine f(x) and hence do the integral from 4 to 6. The trick is to find a simple enough function f(x) so this can actually be done. The simplest case I can come up with is a straight line: f(x)=a*x+b.

Then the calculation goes like this:

f:=(x) -> a*x+b;
x -> a x + b
res:=int(f(x),x=1..X);
                    1   / 2    \            
                    - a \X  - 1/ + b (X - 1)
                    2                       
solve([eval(res,X=4)=7,eval(res,X=6)=4],[a,b]);
                      [[    -23      37]]
                      [[a = ---, b = --]]
                      [[    15       6 ]]
assign((4));
f(x);
                            23     37
                          - -- x + --
                            15     6 
int(f(x),x=1..4);
                               7
int(f(x),x=1..6);
                               4
int(f(x),x=4..6);
                               -3
plots:-display(plot(f(x),x=1..6,color=green),plot(int(f(x),x=1..X),X=1..6,color=red));


confirming your assertion. The plot looks like this:

I hasten to add that f(x) is not uniquely defined by the integral values; there will be infinitely many different ones with the same integrals. But since the integrals are additive (rule # 4 in your book), the result is in fact general and not dependent on the details of f(x).

I hope this helps,

M.D.

Question_for_maple_primes_5.mw

1 2 3 4 5 6 7 Last Page 1 of 20