Ask Your Question
4

Best way to convert Maple to sage

asked 2010-08-20 23:07:25 -0500

TeamTeamUSA gravatar image

updated 2010-08-20 23:09:29 -0500

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 flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
3

answered 2010-08-21 03:26:31 -0500

niles gravatar image

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
alpha=rad(80)
beta=rad(40)
phi=rad(55)
mu=rad(10)
Omega=rad(30)
smM=[rad(-270)..rad(80)]
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 :)

edit flag offensive delete link more

Comments

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

FrédéricC gravatar imageFrédéricC ( 2014-07-28 15:02:07 -0500 )edit

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

niles gravatar imageniles ( 2014-07-29 08:52:38 -0500 )edit
2

answered 2014-07-29 13:52:48 -0500

FrédéricC gravatar image

updated 2014-07-30 10:46:53 -0500

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)


def rad(aa):
    """
    conversion degres -- radians
    """
    return float(aa) / 360 * 2 * pi


def Natalina():
    """
    un modele simple
    """
    dic = {'D': 1,
           'alpha': rad(80),
           'beta': rad(40),
           'phi': rad(55),
           'mu': rad(10),
           'Omega': rad(30),
           'A': 25, 'a': 12, 'b': 16, 'P': 0, 'L': 0}
    srange = (rad(-270), rad(80))
    trange = (-2 * pi, 4 * pi)
    grid = [100, 100]
    return dic, srange, trange, grid


def Lyria():
    """
    un autre modele
    """
    dic = {'D': 1, 'alpha':rad(83.9), 'beta':rad(-19),
           'phi': rad(45), 'mu':rad(1),
           'Omega': rad(-2), 'A':50, 'a':40, 'b':14, 'P':0, 'L':8,
           'W1': rad(6),
           'W2': rad(27), 'N':8}
    srange = (rad(-51), rad(9))
    trange = (-4 * pi, 4 * pi)
    grid = [80, 150]
    return dic, srange, trange, grid


def Turritella():
    """
    un autre modele
    """
    dic = {'D': 1, 'alpha': rad(88.9), 'beta': rad(4), 'phi': rad(55),
           'mu': rad(1),
           'Omega': rad(-2), 'A': 22.2, 'a': 1.3, 'b': 1.5, 'P': 0, 'L': 0};
    srange = (rad(-267), rad(39))
    trange = (-30 * pi, 4 * pi)
    grid = [30, 250]
    return dic, srange, trange, grid


def Oxystele():
    """
    """
    dic = {'D': 1, 'alpha': rad(84.9), 'beta': rad(7), 'phi': rad(-36),
           'mu': rad(1),
           'Omega': rad(-2), 'A': 47, 'a': 40, 'b': 19, 'P': 0, 'L': 0,
           'W1': 6, 'W2': 27, 'N': 8}
    srange = (rad(-70),rad(70))
    trange = (-20 * pi, 4 * pi)
    grid = [40, 150]
    return dic, srange, trange, grid


def Nautilus():
    """
    """
    dic = {'D': 1, 'alpha': rad(80), 'beta': rad(90), 'phi': rad(0),
           'mu': rad(0), 'Omega': rad(0), 'A': 2,
           'a': 2, 'b': 1.5, 'P': 0, 'L': 0}
    srange = (rad(-163),rad(163))
    trange = (pi, 4 * pi)
    grid = [30, 60]
    return dic, srange, trange, grid


def Planorbis():
    """
    """
    dic = {'D': 1, 'alpha': rad(84), 'beta': rad(85), 'phi': rad(10),
           'mu': rad(45), 'Omega': rad(5) , 'A': 45, 'a': 20, 'b': 30,
           'P': 0, 'L': 0, 'W1': 6, 'W2': 27, 'N': 8}
    srange = (rad(-150), rad(130))
    trange = (-pi, 4 * pi)
    grid = [40, 60]
    return dic, srange, trange, grid


def Ammonite():
    """
    """
    dic = {'D': 1, 'alpha': rad(83), 'beta': rad(90), 'phi': rad(1),
           'mu': rad(1), 'Omega': rad(1), 'A': 2.5, 'a': 1, 'b': 0.9,
           'P': 10, 'W1': 100, 'W2': 20, 'N': 15, 'L': 1/2}
    srange = (rad(-170), rad(170))
    trange = (-pi, 4 * pi)
    grid = [40, 60]
    return dic, srange, trange, grid


def Conus():
    """
    """
    dic = {'D': 1, 'alpha': rad(87), 'beta': rad(7), 'phi': rad(78),
           'mu': rad(0), 'Omega': rad(0), 'A': 7, 'a': 4.3, 'b': 1,
           'P': 0, 'L': 0, 'W1': 6, 'W2': 27, 'N': 8}
    srange = (rad(-180),rad(2))
    trange = (-12 * pi, 4 * pi)
    grid = [60, 80]
    return dic, srange, trange, grid ...
(more)
edit flag offensive delete link more

Comments

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.

niles gravatar imageniles ( 2014-07-29 15:22:22 -0500 )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.)

niles gravatar imageniles ( 2014-07-29 15:32:47 -0500 )edit
1

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

slelievre gravatar imageslelievre ( 2014-07-29 18:21:18 -0500 )edit

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

FrédéricC gravatar imageFrédéricC ( 2014-07-30 04:28:11 -0500 )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.

niles gravatar imageniles ( 2014-07-30 09:27:44 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2010-08-20 23:07:25 -0500

Seen: 1,179 times

Last updated: Jul 30 '14