Size: 1063
Comment: added reference to combinatorica compare page.
|
Size: 2582
Comment:
|
Deletions are marked like this. | Additions are marked like this. |
Line 14: | Line 14: |
= Sage and Python Quickstart for Mathematica users = This is not a proper introduction to Python, but a list of examples that Mathematica users will need to figure out how to do if they want to use Sage. == Basic functionality == === Declaring variables === Mathematica assumes that all otherwise unknown symbols are algebraic quantities. Python and Sage don't, they are declared as follows: {{{ sage: var('x,y,a,b,c') (x, y, a, b, c) sage: y == a*x^2 + b*x + c y == a*x^2 + b*x + c }}} === Space as multiplication === {{{ sage: implicit_multiplication(True) sage: y == a x^2 + b x + c y == a*x^2 + b*x + c }}} == Procedural programming == === Table === Mathematica: {{{ Table[f[i], {i, 1, 10}] }}} {{{ sage: [f(i) for i in range(1, 11)] [g(1), g(2), g(3), g(4), g(5), g(6), g(7), g(8), g(9), g(10)] }}} (note that the endpoint of the range is not included). == Advanced Mathematica syntax == === Mapping functions across a list === From a list called ''data'', create a new list where a function ''f'' is applied to each element of ''data''. Mathematica: {{{f /@ data}}} Python: {{{[f(d) for d in data]}}} Unlike in Mathematica, this ''for d in data'' cannot be applied to an arbitrary expression ''data''. So {{{[f(d) for d in g(x,y,z)]}}} is not possible. === Mapping pure functions across a list === (Replacing elements that are less than zero with zero.) Mathematica: {{{data /. _?(# < 0&) -> 0}}} Python: {{{[(0 if d < 0 else d) for d in data]}}} |
SAGE for Mathematica Users
This page is modeled on the http://www.scipy.org/NumPy_for_Matlab_Users
Introduction
SAGE has many of the capabilities of Mathematica, and many additional ones (e.g. wiki-creating software and a 3D raytracer). Some features of SAGE have been inspired by Mathematica, but overall the syntax and structure of SAGE are quite different. One of the main influences on SAGE is the use of the language Python.
This page is intended to help users familiar with Mathematica migrate to SAGE more easily.
Key Differences
Indexing: Lists in Mathematica are indexed starting from 1. In SAGE, as in Python, indices start at 0. Also, where Mathematica accepts a list of indices, in SAGE you can construct sub-lists using "slice" operations. For example, if we have a list of numbers, num_list = [0,1,2,3,4], then numlist[1:3] would return the list [1,2].
Combinatorica
For a comparison of graph theory functionality between SAGE and the Mathematica Combinatorica package, see the CombinatoricaCompare page.
Sage and Python Quickstart for Mathematica users
This is not a proper introduction to Python, but a list of examples that Mathematica users will need to figure out how to do if they want to use Sage.
Basic functionality
Declaring variables
Mathematica assumes that all otherwise unknown symbols are algebraic quantities. Python and Sage don't, they are declared as follows:
sage: var('x,y,a,b,c') (x, y, a, b, c) sage: y == a*x^2 + b*x + c y == a*x^2 + b*x + c
Space as multiplication
sage: implicit_multiplication(True) sage: y == a x^2 + b x + c y == a*x^2 + b*x + c
Procedural programming
Table
Mathematica: Table[f[i], {i, 1, 10}]
sage: [f(i) for i in range(1, 11)] [g(1), g(2), g(3), g(4), g(5), g(6), g(7), g(8), g(9), g(10)]
(note that the endpoint of the range is not included).
Advanced Mathematica syntax
Mapping functions across a list
From a list called data, create a new list where a function f is applied to each element of data.
Mathematica: f /@ data
Python: [f(d) for d in data]
Unlike in Mathematica, this for d in data cannot be applied to an arbitrary expression data. So [f(d) for d in g(x,y,z)] is not possible.
Mapping pure functions across a list
(Replacing elements that are less than zero with zero.)
Mathematica: data /. _?(# < 0&) -> 0
Python: [(0 if d < 0 else d) for d in data]