Mac Dude

1496 Reputation

17 Badges

10 years, 208 days

MaplePrimes Activity


These are answers submitted by Mac Dude

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

You really don't give enough details, but what about the following:

You can get the set of parameters using indets(array,'name'). If you have other indeterminates besides your parameters then they will show up, too, so you may have to filter the list. You get the number of parameters from numelems(indets(array,'name')) or using the filtered list.

Presumably you have a list of values you want to assign to the parameters. You can do this using eval(array,[seq(indets(array)[ii]=values[ii],ii=1..numelems(indets(array,'name')))]). Somehow you have to know the list of your values, obviously, and it is not clear to me how you do that when you do not know the number of parameters, but that seems to be a separate problem.

Here is a code snippet:

A:=Array(1..3,[f(x1),f(x2),f(x3)]);
                       Array(1 .. 3, {1 = f(x1), 2 = f(x2), 3 = f(x3)})
 
X:=indets(A,'name'); # these are your parameters
                          {x1, x2, x3}
vals:=[1,2,3];
                           [1, 2, 3]
eval(A,[seq(X[ii]=vals[ii],ii=1..numelems(X))]);
                         Array(1 .. 3, {1 = f(1), 2 = f(2), 3 = f(3)})

This will scale to any length of A and X.

Let us know if this addresses your problem or is off the mark.

M.D.

If it is a specific Maple command that takes a longtime: not easily. You can observe the memory and time indicators in the bottom status line of the window: if memory usage goes up and up you likely have expression swell (or a memory leak) that causes things to slow to a crawl or hang; if the time stops updating, the kernel (or the interface process) may be hung.

If it is a loop, you can put a print statement to follow through the iterations. For a nicer way, put something like a gauge into your sheet & update it each iteration. I do this for calculations I know take a long time.

If you can, start with a simpler case that works (e.g. smaller matrices) & add complexity or whatever slows things down (e.g. size of Matrices). As a general rule, if you observe growth in calculation time with the size of your problem much faster than linear or quadratic you are in trouble with larger problem size.

Beyond these generalities you need to shows us a worksheet that exhibits the prroblem you are trying to address.

M.D.


Attached is an example of mine called Filters. The library is a module that contains all the functions you want in there.

You then need to save it where Maple can find it. As you can see, I pre-pended (in my maple.init file) my library directory to the others in libname; Maple will look in my own directory first before looking in its general directory (thus I can overlay my own definitions if I so chose). then you use LibraryTools:-Save to put the .mla file in there.

After that, with(Filters) (in my case) will find it.

I do agree with SGJanssens suggestion, but this example may get you going faster.

M.D.

Filters.mw

I think the closest to structs are records. They are related to modules but are more geared towards data structures, although you can store procedures as well. You can access members of a record using :- notation.

E.g.:
 

Struct := Record(c=2*Pi*rho,a=Pi*rho^2);

Struct:-c;
     2 π rho
Struct:-a;
    π rho^2

Members can be Vectors, Arrays, you name it.

As for speed, Maple can be ok if you spend time programming it right. Naive implementation can lead to constant reallocation of memory, which gets costly. I am no expert on this although I have certainly run into unexpected super-linear growth of execution time with the number of cases. Declaring datatype=float (as opposed to "anything") in Vectors can help.

Look up the Maple Programming guide; a treasure trove of good practices.

I hope this helps,

Mac Dude

You can use LinearAlgebra:-MatrixExponential(A) to evaluate exp(A).

You can also first Taylor-expand A and then substitute A:

A=<<a11|a12>,<a21|a22>>;
exp(A);
taylor((3),A,3);

eval(taylor((3),A,3),A=<<a11|a12>,<a21|a22>>);

convert((5),polynom); # (5) is the above expression

The output of exp(A) is in the attached sheet, too big to print here

You'll have to decide whether the above does what you intend.

Mac Dude

exp_of_A.mw

 

I corrected your expression by replacing the square brackets [] with parentheses () and the &ndash; with - (minus sign). Your formula then reads like this:

f := x -> exp((-1)*0.13/x)(0.4700036292*`-`(0.3364722366, 1.2)) + exp(-1/x)(0.2135277634 + 0.528*x) + (-1)*0.528*x + 0.5420043550;

Now you can try solve:
solve(f(x)=0,x);

and Maple replies

       4.37165834535564

You can now check the result

f(4.37165834535564);

        0.

I hope this helps,

M.D.

mp.mw

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