Best way to convert Maple to sage

I want to convert this Maple to sage:

http://www.maplesoft.com/applications/view.aspx?SID=3851&view=html

What sage functions/packages should I be using? I'm new to sage so any help is greatly appreciated.

Thanks,

=miles=

edit retag close merge delete

This question is much more specific than the title suggests. Perhaps the title should be altered. People who are interested in translating Maple code to sage in general will fall upon this page. Those people might benefit from visiting https://doc.sagemath.org/html/en/refe... instead.

( 2018-02-02 21:52:41 -0600 )edit

Sort by » oldest newest most voted

Those are cool!

I just learned how to apply a colormap to 3dplots--probably a secondary issue for you at this stage, but it gives an example of plotting a surface:

var('r v')
cmsel = [colormaps['autumn'](i) for i in sxrange(0,1,0.05)]
p = plot3d(0.2*(r**2 + v**2) + cos(2*r)*sin(2*v),(r,-2,2), (v,-2,2), adaptive=True, color=cmsel, plot_points=10, opacity=0.9)
p2 = sphere((0,0,0),1,color='black',opacity=0.5)
(p+p2).show(aspect_ratio=(1,1,1), figsize=[7,3])


For doing the seashell plots, it looks like parametric_plot3d might be the plotting function you want. Here's an example:

u, v = var('u,v')
f1 = (4+(3+cos(v))*sin(u), 4+(3+cos(v))*cos(u), 4+sin(v))
f2 = (8+(3+cos(v))*cos(u), 3+sin(v), 4+(3+cos(v))*sin(u))
p1 = parametric_plot3d(f1, (u,0,2*pi), (v,0,2*pi), texture="red")
p2 = parametric_plot3d(f2, (u,0,2*pi), (v,0,2*pi), texture="blue")
p1 + p2


And maybe the last pieces of information you need are defining functions and setting parameters (copied from your Natalina example):

def rad(aa):
return float(aa/360*2*pi)

D=1
A=25
a=12
b=16
P=0
L=0


In general, you don't need to import anything to use sage's plotting functions, but you do need to remember to declare the variables with var(). Also, in python and hence also sage, _indentation is part of the syntax_, so for example the function declaration won't work if the return.. line isn't indented, and sage knows the function declaration is over because the following lines are not indented. Maybe you already know this, but if not it can be really confusing (or at least it was for me)!

Finally, here's a link to the sage documentation for 3d graphics; from there you can navigate to either the plot_3d or parametric_plot3d pages. And here's a link to http://sagenb.org, which is a good place to find examples of using the notebook (which is probably where you want to start).

Unfortunately I don't know anything about Maple, so I don't see immediately how to finish the translation, but hopefully this will get you pretty close to finished :)

more

I have done the translation. Where should I post the code ? Here ?

( 2014-07-28 15:02:07 -0600 )edit

@FrédéricC Great -- yes, post them as a new answer :)

( 2014-07-29 08:52:38 -0600 )edit

Here is a translation of the code. Colors are not yet possible, see http://trac.sagemath.org/ticket/12212

"""
Code traduit depuis Maple, le code original est ici:

http://www.maplesoft.com/applications/view.aspx?SID=3851&view=html#mapleautobookmark9

REFERENCES:

A. Cortie : *Digital Seashells* Comput & Graphics Vol 17 , pp. 79-84 (1993)
"""
pi = float(pi)

"""
"""
return float(aa) / 360 * 2 * pi

def Natalina():
"""
un modele simple
"""
dic = {'D': 1,
'A': 25, 'a': 12, 'b': 16, 'P': 0, 'L': 0}
trange = (-2 * pi, 4 * pi)
grid = [100, 100]
return dic, srange, trange, grid

def Lyria():
"""
un autre modele
"""
'Omega': rad(-2), 'A':50, 'a':40, 'b':14, 'P':0, 'L':8,
trange = (-4 * pi, 4 * pi)
grid = [80, 150]
return dic, srange, trange, grid

def Turritella():
"""
un autre modele
"""
'Omega': rad(-2), 'A': 22.2, 'a': 1.3, 'b': 1.5, 'P': 0, 'L': 0};
trange = (-30 * pi, 4 * pi)
grid = [30, 250]
return dic, srange, trange, grid

def Oxystele():
"""
"""
'Omega': rad(-2), 'A': 47, 'a': 40, 'b': 19, 'P': 0, 'L': 0,
'W1': 6, 'W2': 27, 'N': 8}
trange = (-20 * pi, 4 * pi)
grid = [40, 150]
return dic, srange, trange, grid

def Nautilus():
"""
"""
'a': 2, 'b': 1.5, 'P': 0, 'L': 0}
trange = (pi, 4 * pi)
grid = [30, 60]
return dic, srange, trange, grid

def Planorbis():
"""
"""
'P': 0, 'L': 0, 'W1': 6, 'W2': 27, 'N': 8}
trange = (-pi, 4 * pi)
grid = [40, 60]
return dic, srange, trange, grid

def Ammonite():
"""
"""
'P': 10, 'W1': 100, 'W2': 20, 'N': 15, 'L': 1/2}
trange = (-pi, 4 * pi)
grid = [40, 60]
return dic, srange, trange, grid

def Conus():
"""
"""
'P': 0, 'L': 0, 'W1': 6, 'W2': 27, 'N': 8}
trange = (-12 * pi, 4 * pi)
grid = [60, 80]
return dic, srange, trange, grid ...
more

1

fantastic! For people who are initially confused (as I was), note that you need to click "more" to see this entire post. The function to plot the shells is at the bottom.

( 2014-07-29 15:22:22 -0600 )edit
1

Note that these can be _much_ faster if you use symbolic functions rather than lambda functions in the shell code. This is because sage can convert symbolic functions to very fast code, but doesn't do this for lambda functions. For example, you could use x(s, t) = D*... instead of x = lambda s, t: D*... (if you do this for the function g, you'll have to replace round with something else. I used floor and the results looked fine to me.)

( 2014-07-29 15:32:47 -0600 )edit
1

@niles: You could always define round(x) = floor(x+0.5r), then use round(x).

( 2014-07-29 18:21:18 -0600 )edit

I am not used to symbolic functions. How to achieve things like x(s,t)=.. without preparsing (I want pure python files) ?

( 2014-07-30 04:28:11 -0600 )edit
1

I guess these things are actually "callable symbolic expressions"; you can create them with the .function() method of a symbolic expression. See http://www.sagemath.org/doc/reference/calculus/sage/symbolic/expression.html#sage.symbolic.expression.Expression.function For example, you would do RE = (1 / ...).function(s) and so on.

( 2014-07-30 09:27:44 -0600 )edit