If somebody walked up to *you* and asked: "Is Sage now a viable
alternative to MATLAB?" what would you say?
I'm especially interested in what people who do numerical/applied
computation think.
My answer: "It's very difficult for *me* to answer this question
myself, because MATLAB is useless for most of my own
teaching/research/work, but I realize it is very widely used in
applied mathematics. Based on going to Scipy and the resources I've
seen online, it appears that the Numpy/Scipy stack is extremely useful
to actual people doing numerical computation. Maybe I'll try
asking on sage-devel."
[NOTE: I am interested in people's answers, rather than somebody
hijacking this thread to try to define "viable alternative" or say
this isn't a scientific survey or something. Please try not to hijack
this thread. Thanks!]
-- William
--
William Stein
Professor of Mathematics
University of Washington
http://wstein.org
The scipy or numpy lists have threads on this occasionally. Usually
many people will chime in and say why they switched from MATLAB.
Here's at least one of these threads:
http://thread.gmane.org/gmane.comp.python.numeric.general/27115
and one message in particular:
http://thread.gmane.org/gmane.comp.python.numeric.general/27115/focus=27127
and another:
http://thread.gmane.org/gmane.comp.python.numeric.general/27115/focus=27127
And a message from another thread that has some very good links
comparing with matlab:
http://thread.gmane.org/gmane.comp.python.matplotlib.general/8249/focus=8259
And one more message (but look at the entire associated thread):
http://thread.gmane.org/gmane.comp.python.numeric.general/15078/focus=15080
People that would be good to ask that come to mind and from the above
threads would be:
* John Hunter (lead for Matplotlib)
* Travis Oliphant (maybe he wouldn't know, but I bet he knows people who
migrated from matlab)
* Sturla Molden
* Matthew Brett
Thanks,
Jason
Great question.
I teach in a predominately applied school and have been in lots of committees
where this issue has come up. The argument I've heard is basically that
(a) math software is a bit of a (necessary) extra burden on the student
who must first learn the theoretical aspects of the topic,
(b) matlab is used in industry more than any other math software,
(c) we should train students to know the most commonly used
software so as to maximize their value to a "potential employer".
(I teach at the USNA, which requires a military service, so by
"potential employer" I mean one after their service commitment
is up and they enter the private sector. AFAIK, the Navy does not
require matlab knowledge in any of their positions, though I do
think that a very good facility with math software *in general* is
of significant importance. In other words, if an officer arrives at
a new job and is told to "learn software X" they should be able to do
so very quickly.)
Do I agree matlab knowledge is very important? No. Do I mention
octave (as a free alternative to matlab) and Sage? Yes. Sadly for the
American taxpayer, this objection to the cost of matlab (which is
very cheap for students but very expensive for professionals) does
not have a serious effect since the engineering dept already requires
matlab.
Conclusion: I think Sage needs more engineers:-)
I presume a "matlab mode" option in the notebook would not hurt.
My 2 cents.
> I'm especially interested in what people who do numerical/applied
> computation think.
>
...
> -- William
>
> --
> William Stein
> Professor of Mathematics
> University of Washington
> http://wstein.org
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>
It sound like it wouldn't be difficult for you to name one single
Matlab matrix function that engineers would actually use that
Numpy/Scipy doesn't have? I wonder why it's not in numpy yet.
I know one example sort of like this, which is "rref", for reduced row
echelon form. However, in a numerical setting, my understanding is
that engineers would never ever use rref directly anyways.
Nonetheless, Matlab has it and Numpy/Scipy don't, because there is no
point in having it.
Incidentally, as I discovered by putting this on a homework assignment
for my class, it genuinely is really easy to implement rref in Numpy,
and the result from almost anybody is very impressively fast, even in
pure Python (Cython barely helps in this case, since Numpy is already
so optimized).
Also, with your "75%" above, do you mean when working with Sage
matrices or Numpy arrays?
-- William
>
> We have everything else you need - simple matrix operations
> (submatrices, transposes, etc, etc), all the usual functions (sine,
> cosine, exponential, etc), a user interface (the notebook). So all
> the surrounding bits and pieces are available (and possibly
> superior). The "ecosystem" is in place.
>
> We do a few things, thankfully very rarely, where we use exact
> algorithms for matrices with floating-point entries (like rank, or
> echelon form) and we shouldn't be. For those brought up doing things
> exactly (myself included) the approach and philosophy takes some time
> to get used to. I have a rant all composed on this subject from
> several months ago, but did not unleash it since I wasn't prepared to
> follow-up.
>
> I am very interested in working on all of this, as I would like to use
> both exact and inexact computations in a "second course" on linear
> algebra. Unfortunately, my extended period of time for exclusive Sage
> development is coming to an end (I'm writing my sabbatical final
> report today).
>
> So hear comes the thread-hijack - plenty to do - I have some patches
> posted and plenty of ideas for further work. Look at the bottom of
> the table at:
> http://wiki.sagemath.org/devel/LatexToWorksheet
>
> Rob
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>
--
Dear Sage project leader,
My experience is about Sage 4.6.2:
For numerical computations I have quieted of Sage/Sagenotebook and use only scipy/numpy directly using vim and shell.Sagenotebook has the advantage of shareable over the net worksheets but the editing 83 functions on it became hardwork because of those jumps on screen and no colors. I had problems making/seeing graphics. (maybe that is not a problem now)
Sage has the advantage of symbolic calculus which could help confirming some numerical calculations but the integration with scipy/numpy needs some attention to know if a given parameter is in Sage world or scipy/numpy world. Also Sage has the problem of taking to much time to load and using "attach" reveals some problems.This is what I remember that lead me to the decision of quiting sage/sagenotebook for the specific purpose of pure numerical computation. (matlab was never needed).
I will improve this notes as soon as a new numerical projects began. Is there any thematic group or wiki for "numerical sage" ?
Pedro Cruz
Assistant Teacher
University of Aveiro
Portugal
I believe there are a few such functions, and they aren't in numpy yet
because no one has implemented them. Remember that there are far fewer
numpy developers than Sage developers, for example.
I also think there are a lot of functions in matlab that are smarter
than the ones in numpy. What I mean is that the functions in matlab
handle more cases or wrap more lapack functions under a single call.
For example, there are a lot of matlab functions that will automatically
use different lapack functions depending on the structure of the matrix
or various options to the function, whereas the numpy equivalent will
just use a standard lapack function for a generic case.
Of course, this conversation would be *much* better to have on the numpy
list. Would you like me to post there?
>
> I know one example sort of like this, which is "rref", for reduced row
> echelon form. However, in a numerical setting, my understanding is
> that engineers would never ever use rref directly anyways.
> Nonetheless, Matlab has it and Numpy/Scipy don't, because there is no
> point in having it.
>
> Incidentally, as I discovered by putting this on a homework assignment
> for my class, it genuinely is really easy to implement rref in Numpy,
> and the result from almost anybody is very impressively fast, even in
> pure Python (Cython barely helps in this case, since Numpy is already
> so optimized).
>
> Also, with your "75%" above, do you mean when working with Sage
> matrices or Numpy arrays?
IIRC, Numpy matrices are discouraged. Lots of functions in numpy are
not written with the matrix class in mind, so they will take numpy
matrices, for example, but will return numpy arrays. This, of course,
messes lots of stuff up. There have been threads on the numpy list
about deprecating matrices or the lack of attention that the matrix
class has.
Note to those that are confused: numpy has two basic data structures:
arrays (any dimension) and matrices. Think of matrices as a subclass of
arrays that are (1) only 2-dimensional, and (2) have some convenience
things, like A*B is standard matrix multiplication, etc. Numpy arrays
used by the vast majority of people. You can see some of the
differences here: http://www.scipy.org/NumPy_for_Matlab_Users
Personally I decided a long time ago that it wasn't worth the gotchas to
work with numpy matrices, so when I work with numpy, it's always with
the numpy arrays.
Thanks,
Jason
> I have to be careful, because I would not claim to be a numerical
> linear algebraist, so I am willing to be corrected - but I think an LU
> decomposition would be the first choice for an alternative to rref.
> It is basically what we used to call Gaussian elimination, rather than
> Gauss-Jordan elimination, but with more care about pivoting in a
> numerical setting. Enabling LU decomposition for rectangular matrices
> (not just square), which is now possible in NumPy/SciPy, is a patch
> with a positive review (iirc) and I have posted a patch for an exact
> LU decomposition, which is able to run over generic rings twice as
> fast as echelon form (as theory would predict). Needs review. An LU
> decomposition is the lever for a similar speedup (2x) in solve_left()
> over generic rings.
>
A long time ago, I also wrote an LU decomposition patch that used at
least two different types of pivoting. See
http://trac.sagemath.org/sage_trac/ticket/3048
> I would vote for not allowing a naive/classical rref algorithm from
> Sage to be applied to a matrix over RDF/CDF.
-1 if there is no way to do it, as it serves an important pedagogical
purpose. However it definitely should not be the default. LU
decomposition with partial pivoting should be the default, IIRC (but the
same disclaimer that you give above applies to me :).
Thanks,
Jason
Sagenotebook has the advantage of shareable over the net worksheets but the editing 83 functions on it became hardwork because of those jumps on screen
On 15 August 2011 14:58, William Stein <wst...@gmail.com> wrote:
> If somebody walked up to *you* and asked: "Is Sage now a viable
> alternative to MATLAB?" what would you say? I'm especially
> interested in what people who do numerical/applied computation
> think.
With Octave we deal with people like this all the time. They basically
want Matlab, not a substitute, because their
instructor/employer/whoever has told them it must be Matlab and it
can't be anything else. They often also have code written in Matlab
that can't be translated. In the case of running the exact same code,
which happens very frequently with Matlab, I can tell you that for
these people rewriting their code in Scipy isn't an alternative (I am
obviously also referrring to Numpy, as the core component of Scipy).
One of the most frequent requests we get in Octave and then everyone
will magically jump ship from Matlab is to implement a GUI (or an
editor, or a profiler, or an integrated debugger, or all of Simulink).
They want it to look almost exactly like Matlab too, or else they
won't use it. For a long time we've ignored this request, but it comes
up so often that it's hard to keep ignoring it, and a
Matlabish-looking GUI is a release goal for our next version, and the
work is well underway.
There are also a lot of people for whom Matlab is only incidental and
what they really want is Simulink. For this, neither scipy nor Octave
have any sort of alternative, but I understand Scilab has something
called Xcos that does sort of work like Simulink.
Matlab users also tend to have highly specific requests. They want the
*exact* same function from some particular Matlab toolbox and they
want it to work the same way in Octave (or presumably, a direct
equivalent in Scipy), even for silly things like rref because, again,
their instructor/employer/whoever told them that that is the function
they must use. I have recently started working with Scipy, and my
impression is that it has smaller set of contributed functions than
Octave and smaller than Matlab, so when someone from Matlab wants this
or that low-pass filter and accepts no substitute, they get easily
frustrated with the free alternatives and go back to Matlab. A big gap
in Scipy due to silly licencing issues is FFTW, which is the de-facto
standard FFT library both Octave and Matlab use; FFTPACK is an ok
substitute and all that Scipy has. Signal processing is one of
Matlab's biggest use cases and a poor FFT implementation is
problematic.
I imagine demanding users like this exist for the other three big M's
too, but Matlab to me seems to have a well-entrenched niche separate
from the other three M's whoh at least somewhat compete with each
other, whereas Matlab has no direct big competitor. The competition in
the other three M's has at least opened up the users of those CASes to
the possibility of learning a new syntax, but my impression is Matlab
users are less flexible in this regard.
HTH,
- Jordi G. H.
GNU Octave developer
One big advantage of sage is that one can do numerical calculations
along with
computations in algebra or number theory or graph theory...
For my own work I find myself wanting to do numerical computations on
adjacency matrices
of graphs. For this matlab is not very useful because it is awkward to
generate the graphs
or read them in from a unix pipe. And if I wanted to compute an
automorphism group, I am
entirely lost. So I have been using sage for numerical computations.
The good news is that it works.
The bad news is that it is a very frustrating experience. To
illustrate with an example,
I wanted to compute spectral decompositions of symmetric real
matrices. This means I need
I'm an engineer and I use MATLAB pretty extensively so I think I can
provide that viewpoint. Sage isn't really a viable alternative to
MATLAB and really isn't likely to be any time soon. The SciPy/NumPy
parts cover the basics of MATLAB, but most people use a number of
toolboxes that go beyond the basics. In my case, I use the System
Identification toolbox, the Spline toolbox, and some of the
optimization routines. In addition, I have a lot of existing code that
I rely upon that I'd have to convert.
The Spline toolbox I could reproduce a lot with the B-Spline routines
from the SLATEC library. The Fortran routines there are from de Boor's
Splines textbook (which is also the basis for the Spline toolbox). The
System ID toolbox is partially implemented in Octave (and possibly in
SciPy), but I've found no open source implementation of the routine I
find essential, ETFE (Empirical Transfer Function Estimate) which
given a set of input data and a set of output data will estimate the
transfer function.
Oh, I also rely upon Code Generation routines in Maple that output
MATLAB code. I could have them output Fortran or C, but then I'd have
to look at how to interface with them as well (but that's likely not
as seamless).
This is even aside from things like embedded code generation or Simulink.
I think you'd find that most people will run into cases where at least
one routine they need isn't supported. Now, that's been happening with
Octave and as such, they've been getting better toolbox support, but I
know I'd have to improve at least their System ID support before I
could think about switching.
Cheers,
Tim.
--
Tim Lahey
PhD Candidate, Systems Design Engineering
University of Waterloo
http://about.me/tjlahey
I might mention several other things. In fact, I was going to mention
this yesterday to show how using them was just as easy as MATLAB, but
after constructing the last example below, I realized you were right and
matlab was way easier! Anyways:
1. You can use the -pylab option to load a matlab-like environment
(which means you don't have to figure out a lot of the imports). The
-pylab option is actually an option to ipython (i.e., not
sage-specific), but works just as well with Sage:
sage: g=graphs.OctahedralGraph()
sage: a=g.am().numpy()
sage: eig(a) # matlab-like command from numpy/scipy through pylab
(array([ 4.00000000e+00, 1.40614246e-16, -2.00000000e+00,
-2.00000000e+00, -7.71031068e-17, -2.11137087e-16]), array([[
-4.08248290e-01, -7.07106781e-01, -5.77350269e-01,
-9.58724220e-02, -8.33596830e-18, -2.93912896e-17],
[ -4.08248290e-01, -8.75811329e-17, 2.88675135e-01,
-4.45121959e-01, 2.13205450e-01, 5.03280679e-01],
[ -4.08248290e-01, -5.98255573e-17, 2.88675135e-01,
5.40994381e-01, -6.74198366e-01, -4.96697653e-01],
[ -4.08248290e-01, -3.20699816e-17, 2.88675135e-01,
5.40994381e-01, 6.74198366e-01, 4.96697653e-01],
[ -4.08248290e-01, -3.20699816e-17, 2.88675135e-01,
-4.45121959e-01, -2.13205450e-01, -5.03280679e-01],
[ -4.08248290e-01, 7.07106781e-01, -5.77350269e-01,
-9.58724220e-02, 2.96978775e-17, 1.90446830e-17]]))
2. or you could use Sage's eigenmatrix_right command:
sage: g.am().eigenmatrix_right()
(
[ 4 0 0 0 0 0] [ 1 1 0 1 0 0]
[ 0 -2 0 0 0 0] [ 1 0 1 0 1 0]
[ 0 0 -2 0 0 0] [ 1 -1 -1 0 0 1]
[ 0 0 0 0 0 0] [ 1 -1 -1 0 0 -1]
[ 0 0 0 0 0 0] [ 1 0 1 0 -1 0]
[ 0 0 0 0 0 0], [ 1 1 0 -1 0 0]
)
which gives a much nicer orthogonal basis (columns of the right matrix)!
3. Or you could explicitly use the eigenvectors command, which gives the
eigenvalues and for each eigenvalue, a list of eigenvectors and the
algebraic multiplicity.
sage: g.eigenvectors()
[(4, [
(1, 1, 1, 1, 1, 1)
], 1), (-2, [
(1, 0, -1, -1, 0, 1),
(0, 1, -1, -1, 1, 0)
], 2), (0, [
(1, 0, 0, 0, 0, -1),
(0, 1, 0, 0, -1, 0),
(0, 0, 1, -1, 0, 0)
], 3)]
4. The reason I decided you were right about matlab was when I tried to
do the commands exactly as it would be in matlab, it looked a lot harder
than my example matlab session. Note again this is with sage -pylab
sage: a=array([[1,2,3],[4,5,6],[7,8,9]])
sage: b=a.dot(a.T) # b=a*a'
sage: eig(b)
(array([ 2.83858587e+02, 1.14141342e+00, -1.90551232e-15]),
array([[-0.21483724, -0.88723069, 0.40824829],
[-0.52058739, -0.24964395, -0.81649658],
[-0.82633754, 0.38794278, 0.40824829]]))
The "dot" above is really bothersome, especially since *. (i.e., star
*dot*) in matlab is the same as "*" for numpy arrays. Numpy folks have
been trying to get python to change for years and allow multiple
multiplication characters, and it probably isn't going to change anytime
soon. The "array" command above is also much harder than the matlab
a=[1 2 3; 4 5 6; 7 8 9].
You can make it a little easier in numpy if you use the numpy "matrix"
objects, but they are not extremely well-supported by numpy, so I avoid
them as much as possible. Note that "matrix" below is the *scipy*
matrix command, since -pylab overwrote Sage's matrix command. However,
matrix multiplication works naturally below, and the matrix-creating
command uses matlab-like syntax.
sage: a=matrix("1 2 3; 4 5 6; 7 8 9")
sage: a
matrix([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
sage: b=a*a.T
sage: eig(b)
(array([ 2.83858587e+02, 1.14141342e+00, -1.90551232e-15]),
matrix([[-0.21483724, -0.88723069, 0.40824829],
[-0.52058739, -0.24964395, -0.81649658],
[-0.82633754, 0.38794278, 0.40824829]]))
One conclusion: we should make Sage's matrix command accept a string
like the above example and parse it as matlab would.
Thanks,
Jason
> One conclusion: we should make Sage's matrix command accept a string like the above example and parse it as matlab would.
+1
Another option is to make the preparser recognize syntax like:
[1 2 3; 4 5 6; 7 8 9]
or even
[A B; C D]
for making a matrix by stacking and concatenating matrices.
I don't know how strong of a case we could make for such a preparser
change, though. It'd have to have lots of support before it would even
be considered seriously.
Jason
> I think you'd find that most people will run into cases where at
> least one routine they need isn't supported. Now, that's been
> happening with Octave and as such, they've been getting better
> toolbox support, but I know I'd have to improve at least their
> System ID support before I could think about switching.
It seems like every dedicated Matlab user has a specific feature
request without which they absolutely can't use Octave (or Scilab or
Scipy). When I was but a wee lad, for me it was Matlab's JIT (although
I didn't even know that I was using it). I've learned to change my
habits to work with what Octave has, and I submit patches for what I
wish it had.
That being said, what is the system identification toolbox? The
Mathworks makes it look like it's a combination of Simulink with
something else.
- Jordi G. H.
There may be connections with Simulink, but on the whole it doesn't
have anything to do with it. The toolbox is based upon the book,
"System Identification - Theory for the User" By Lennart Ljung. It
uses various methods to get models of a system. Basically, it fits
data to differing kinds of models. The method I use ETFE (Empirical
Transfer Function Estimate) in the end gives a frequency spectrum
corresponding to the system's plant (based upon the given input and
output data). It's different than just doing a FFT on the output data.
But there are methods for Linear Time-Invariant systems, as well as
time-varying and non-linear systems. Most of the toolbox is centred
around these models and parameter estimation. Using the ETFE I'm not
limited to a specific model since the approach treats the system like
a black box getting a model of what happens based upon input and
output data alone rather than trying to fit to a specific modelling
approach (other than assuming the system is linear).
Documentation:
http://www.mathworks.com/help/toolbox/ident/
And the specific function I use most:
http://www.mathworks.com/help/toolbox/ident/ref/etfe.html
One of the advantages is that I can customize the output frequency
resolution. This is handy when I'm trying to find the natural
frequency since I need good resolution around the damped natural
frequency peaks.
This is now #11699: http://trac.sagemath.org/sage_trac/ticket/11699
I agree with Rob; this probably should be in matrix(), and not in the
preparser.
Dan
--
--- Dan Drake
----- http://mathsci.kaist.ac.kr/~drake
-------
I've added example code to the ticket. Here is the current iteration:
# if a was the string passed in as the list of elements
a=a.replace(',', ' ').replace(';','\n') # or a.translate(s)
rows = a.split('\n')
# throw away empty rows
rows = [r for r in rows if r.strip()]
elements=[map(sage_eval, r.split()) for r in rows]
# now continue matrix processing with "elements" as the list of elements
Or if we could afford to initialize something at the module level, we'd
initialize this:
import string
s=string.maketrans(';,','\n ')
and replace the first line above with:
a=a.translate(s)
>
> The bigger problem is: How do you convert the strings representing
> matrix entries to sage? What is going to be the base ring of the
> matrix? This determines what to use instead of the "eval" above. Such
> problems do not arise in Matlab because of the more limited scope: the
> only numerical type is a float (double probably). In Sage it is not so
> easy.
This would be determined by the other arguments to matrix. I'm
proposing that the string would just take the place of the normal nested
list of entries, wherever it is, and would be interpreted as if they had
typed in a normal nested list of entries (using sage_eval). Thus, the
string would be converted to a nested list at the top of the matrix
constructor function.
Jason
Yes, this code is already there.
> I'm proposing
> that the string would just take the place of the normal nested list of
> entries, wherever it is, and would be interpreted as if they had typed in a
> normal nested list of entries (using sage_eval). Thus, the string would be
> converted to a nested list at the top of the matrix constructor function.
To throw my 2 cents in, I'm not opposed to expanding the preparser to
handling [a, b; c, d] as matrix literals. Accepting strings is nice,
but not as natural (e.g. one looses tab completion and errors are much
more indirect). Splitting on spaces is error prone (e.g. "1 + sin(x)")
and so is naively splitting on commas [1, 2; pi, f(3, 4)]."
- Robert
Good point. It turns out to be much harder if we don't just assume
numbers. I guess one way to do it would be to construct a parse tree
using the python parsing, except, of course, that semicolons make [1 2
3; 4 5 6] not valid syntax, though I suppose we could replace semicolons
and newlines with "][" and then filter out empty lists. Spaces also
would be problem in constructing a parse tree, since [1 2] isn't valid
syntax.
So yeah, expanding the preparser for this very common situation (easily
creating a doubly-nested list) is starting to look more like a good idea.
If we expanded the preparser, would [1 2; 3 4] create a matrix or just a
doubly-nested list? I can see arguments both ways, and this is where
Nils point really hits home. Almost always, I want matrices over QQ,
but [1 2; 3 4] won't naturally create a matrix over QQ. On the other
hand, typing matrix(QQ, [1 2; 3 4]) seems to take the fun out of the
syntax, since it's not too much easier than matrix(QQ, [[1,2], [3,4]])
Jason
Some of the code used to manage this:
sage: M = matrix(2,2,[1,2,3,4])
sage: PM = pari(M)
sage: PM
[1, 2; 3, 4]
sage: PM.sage()
[1 2]
[3 4]
could perhaps be reused like this:
sage: s = "[1,2;3,4]"
sage: pari(s).sage()
[1 2]
[3 4]
sage: s = "[1.1,2.2;3.3,4.4]"
sage: pari(s).sage()
[1.1000000000000000000000000000000000000
2.2000000000000000000000000000000000000]
[3.3000000000000000000000000000000000000
4.4000000000000000000000000000000000000]
so we are using the pari parser to construct a pari matrix, which then
knows how to be converted into sage.
John
that's a very small syntax price to pay for …
My impression from my own math department is that a lot of Matlab
users know more or less only Matlab as a programming language...
It shouldn't be that hard to implement functions which at least
partially translate MATLAB / Mma / whatever syntax (passed as a
string, or from a file) to corresponding Sage expressions and
commands, analoguous to preparse().
IIRC, it's been in numpy for years. Certainly at least 5 years or so,
and probably far longer than that. In fact, you can do much more, just
like in matlab. You can index using a boolean array, for example. See
http://docs.scipy.org/doc/numpy/reference/arrays.indexing.html or
http://www.scipy.org/NumPy_for_Matlab_Users#head-13d7391dd7e2c57d293809cff080260b46d8e664
for more details and examples.
You can use that notation with Sage matrices too. We've only had it for
maybe 4 years now, though. See
http://www.sagemath.org/doc/reference/sage/matrix/docs.html#indexing
Thanks,
Jason
Since we rely so much on numpy/scipy for this stuff, maybe we could
continue to invite some of their top numerical linear algebra people to
a Sage Days, thus "sponsoring" them to work on things like this, e.g.,
wrapping more needed LAPACK functions, or wrapping them smarter, etc.
We've already done this in March (Mark Wiebe and Chuck Harris came).
It'd be cool to do it again.
Jason
ok, good ;)
About your tutorials and documentation ideas, I think what could help
at least for the start is to mention the respective function in the
documentation. Then, when someone searches for function "x" s/he ends
up at the corresponding page in the sage documentation.
On Fri, Aug 19, 2011 at 14:32, Jason Grout <jason...@creativetrax.com> wrote:
> maybe we could continue to invite some of their top numerical linear algebra
> people to a Sage Days
big +1
H
There is http://axiom-developer.org/axiom-website/rosetta.pdf
which does not include Sage, although I'd be happy to replace
it with a newer version that does. I'm not enough of a Sage
person to do it properly.
Tim Daly
+1
> Designing modules, classes and functions such that tab completion will
> eventually give some insight on a topic (like UNIX's 'apropos') is
> quite limited, and doesn't work at all for "foreign" functions (unless
> we'd put them into the namespace, which would likely be limited and
> IMHO a bad idea).
Where it really shines is when I have an object, say E, and do
E.<tab>
But I agree that that alone is often insufficient.
More on topic, I strongly agree with the sentiments that we are trying
to create a viable alternative, not a clone. Sage does lack a concise
syntax for matrices which are a pretty basic type, and I think this
deficiency is probably worth addressing with the preparser. The [a, b;
c, d] syntax is a natural one (pari as well as Matlab uses it), though
I'm open to suggestions. FWIW, Maple and Mathematical require nested
lists just as Sage does now. As for the basering, we have
[a, b; c, d].change_ring(QQ)
but change_ring might be a bit obscure. Maybe "over" could be provided as well.
- Robert
What if I want a 1-row matrix. Will this work?
[a,b,c,d]
Of course, this will be a problem since that is valid list syntax. For
that reason, it seems more consistent that the semicolon is a shortcut
for doubly-nested lists, and still insist the user do something with a
matrix creation command.
But then what about:
[1, 2,
3, 4]
From the previous messages (where newlines are treated like
semicolons), that should be a square 2x2 matrix, but again, it is valid
python syntax.
Thanks,
Jason
I like that.
Interestingly, it works in MATLAB, but not in PARI.
William
It makes sense to work in matlab. It seems that matlab compresses empty
rows (for example, [1 2 3 4 ; ; 5 6 7 8] gives a 2x4 matrix). So
initially it is a 3-row matrix, but then matlab compresses the second
row since it is an empty row. Or I guess another way to say it is that
matlab compresses consecutive column delimiters. So matlab being sloppy
(or convenient?) makes it work.
Jason
Interesting. In python, this is often stated as "the comma operator
makes a tuple, not the parentheses". So we're saying that the semicolon
is what makes a matrix. Interesting. So if there is no semicolon, it
isn't a matrix.
Should we support spaces for element delimiters? That would be
consistent with matlab, and I personally prefer [1 2; 3 4] over
[1,2;3,4] (since the , and ; are too similar, so it takes a moment to
determine which is an element and which is a row delimiter). If we
support spaces, then it seems a little weird that [1 2 3] is not good
syntax, but [1 2 3; ] is good syntax.
Also, I guess it is nice in matlab that you can do:
a=[
<paste in a table of numbers you copied from a web page
]
and get a nice matrix, since newlines count as row delimiters.
Thanks,
Jason
That would greatly complicate things, I'd rather the syntactic sugar
stay simple. I don't see it as being any worse than
sage: (x + 1.00000000000000000001).change_ring(RealField(100))
x + 1.0000000000000000000101643954
If one cares about the basering that much, use the normal matrix constructor.
>> What if I want a 1-row matrix. Will this work?
>>
>> [a,b,c,d]
>
> In analogy to (a,) being a singleton, this should probably be
> [a,b,c,d;]
+1
>> But then what about:
>>
>> [1, 2,
>> 3, 4]
-1.
I don't think we should treat newlines specially.
>> From the previous messages (where newlines are treated like
>> semicolons), that should be a square 2x2 matrix, but again, it is valid
>> python syntax.
>
> Hence, I don't think a preparser based solution should support this.
> Saving the "..." comes at the cost of having to type the ;s
>
> Also note that once we're doing this, we should probably also support
> matrices over matrices, so
>
> [ [1,2;3,4] , [5,6;7,8]; [9,10;11,12], [13,14;15,16]]
>
> should also be supported (just because your grammar specification will
> be a horribly ugly mess if you want to disallow this construct).
> This basically means that the preparser actually has to parse its
> input (http://en.wikipedia.org/wiki/
> Pumping_lemma_for_regular_languages can be used that parenthesis
> matching cannot be done using regular languages, although some modern
> "regex" implementation might have features for it).
I don't see a compelling reason to support it. For better or for
worse, "Un-preparsed sage" doesn't really have a grammar. It's more
like the C preprocessor that does textual substitutions before the
real parser takes effect. (Not that the C preprocessor isn't a
horrendous hack, but writing C without it would be even worse. :-)
That being said, counting brackets would suffice without needing to do
a full parsing. (String literals are already stripped at this point.)
> Matrices over matrices are weakly supported presently:
>
> sage: M=matrix(2,2,[1,2,3,4])
> sage: A=matrix(2,2,[M,M,M,M])
> sage: B=A^2
> sage: B[0,0]
> [14 20]
> [30 44]
> sage: M*M+M*M #that's what the entry should be
> [14 20]
> [30 44]
>
> but pretty much anything else fails (including printing these objects,
> because printing apparently needs hash(M))
>
> Independent of the example above, nested matrix literals could happen
> anyway:
>
> [ 1, 2; 3, det( [4,1;0,1]) ]
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>
IMHO, small difference == bad idea
> The line break replaces the semicolon, hence, the example that you
> give corresponds to [1,2,;3,4] (which hopefully is a syntax error in
> Matlab), while the second version becomes [1,2;3,4] (which seems to be
> the matlab idea of a matrix).
>
> That said, I think one of the strength of Sage is to use a mainstream
> language. It should not be weakened too much by syntactical sugar, in
> particular if it lacks precision (by gratuitous assumptions on the
> base ring).
>
> Concerning some matlab users feeling irritated by any deviation from
> matlab syntax: I guess that Sage is not going to be a Matlab clone,
> and thus that kind of users is lost.
>
> Every thinking person must be aware that, to some extent, *the same*
> idea can be expressed in different languages. I hope that every person
> working in numerics would not mind to use a CAS with a decent
> programming language, provided that it offers *the functions* (but not
> necessarily the function names or the syntax) of Matlab.
I don't see this a question of cloning Matlab, rather it's a feature
(concise matrix literals) that we'd like to have too.
> Concerning matrices: Are people working in numerics really
> interactively typing in matrices? Or are they usually just reading
> matrices from files that are created by programs (e.g., based on data
> from an experiment)? If the latter is the case then syntactical sugar
> wouldn't really matter.
Personally, I'd want them for 2x2 matrices.
I would also be in favor of supporting a much more flexible
matrix(str) constructor, where (for example) newlines would be treated
as row breaks and other whitespace between identifiers treated as
column breaks (similar to implicit multiplication) and
leading/trailing []'s are ignored. E.g. could do
sage: matrix("""
[1 2]
[3 4]
""")
- Robert
Again, matlab is (sloppy|convenient|lenient):
>> [1,2,;3,4]
ans =
1 2
3 4
Thanks,
Jason
I completely agree with you! Also, just adding this parsing of
matrix-strings doesn't change anything in terms of
adoption-friendliness. It might be nice for playing around or solving
some educational examples, but it's irrelevant for the average matlab
user.
H
+1. I'm pretty convinced now that an easier syntax for creating
matrices should be rather low on the priority list (especially since it
is rather more difficult than I initially thought).
Jason
Since you mention "InputForm[]", I can't help but mention Sage's
analogue of that (written by Carl Witty), which is called
"sage_input":
sage: a = random_matrix(QQ,4)
sage: sage_input(a)
matrix(QQ, [[0, 1, -1, 1/2], [0, -1, -2, 0], [0, 0, 2, 0], [1, 0, -1, 2]])
If I were entering the above matrix by hand, I would likely do:
a = matrix(QQ, 4,
[0, 1, -1, 1/2,
0, -1, -2, 0,
0, 0, 2, 0,
1, 0, -1, 2])
I would gain absolutely nothing at all in this case by using
semicolons at the end of each line.
Since I'm not familiar with Matlab myself, I asked some people who is
familiar "Why do they choose Matlab and not any other system, for
example, Sage".
- Most of them didn't know about Sage at all till that moment.
- Most of them started to use Matlab just because they heard this word
from other students / PhD students / lecturers and once they got used
to it, they want nothing else.
- Some of them a ready to switch to ANY other system any time IF they
find it more useful or more suitable for their work.
- Some of them had not used any kind of CAS before and are ready to
start with any system (including Sage) if somebody teaches them how to
work with it.
These two last categories seem the most promising to me. And while
the people from the latter one do not make any decisions basing on
*their own* opinion, people from the former one tend to be more
conscious and flexible. What they need is a set of functions they use
in their work (with any names - not necessarily matching those from
Matlab), a detailed reference manual to look for such functions and
their usage and a huge amount of ready recipes and scripts on the
Internet for copy-pasting into their own programs.
For example, one of them said: "When I need to use some relatively
new method, it is very likely that I will find its implementation in a
form of a Matlab script (or maybe he said "toolbox" - I'm not sure
about how they call it) much earlier that in any other language/system.
That is why I just take it and use it. And of course, I need to use
Matlab to run it."
Another example - one of them asked me: "Where can I find a complete
list of functions available in Sage?" I had to explain him that Sage
includes a number of mathematical packages each one having its own
documentation, reference manuals, examples and so on on their websites.
But such an answer simply sounds like "I don't know". And what kind of
help is that?
So, summarizing all stated above, it seems to me that the main cause
of Sage being undervalued is the lack of documentation/books/reference
manuals/forums/ready recipes/etc. in different languages. For example,
almost all people in xUSSR countries usually look for some kind of
support in Russian. And that's quite a lot of people. They can easily
obtain any kind of help on Matlab in Russian, but they can get almost
no help at all in Russian for Sage.
P.S. None of them mentioned the syntax differences, namely in the
matrix notation, although this probably does not say much by itself
Regards,
Vladimir
On Mon, 15 Aug 2011 12:58:38 -0700
William Stein <wst...@gmail.com> wrote:
> Hi,
>
> If somebody walked up to *you* and asked: "Is Sage now a viable
> alternative to MATLAB?" what would you say?
> I'm especially interested in what people who do numerical/applied
> computation think.
>
> My answer: "It's very difficult for *me* to answer this question
> myself, because MATLAB is useless for most of my own
> teaching/research/work, but I realize it is very widely used in
> applied mathematics. Based on going to Scipy and the resources I've
> seen online, it appears that the Numpy/Scipy stack is extremely useful
> to actual people doing numerical computation. Maybe I'll try
> asking on sage-devel."
>
> [NOTE: I am interested in people's answers, rather than somebody
> hijacking this thread to try to define "viable alternative" or say
> this isn't a scientific survey or something. Please try not to hijack
> this thread. Thanks!]
>
> -- William
>
-----
<v_...@ukr.net>
You might look at http://sagemath.org/doc/reference/genindex.html,
though it's so big, I don't know how helpful it is to just browse the
entire list...
Jason
You could set the default "system" for the notebook to be Python
instead of Sage.
Then you can use the course materials you've already developed with
almost no changes at all, since all of "Python/ IPython/ SciPy/ NumPy/
Matplotlib" are included in Sage. The main thing you have to do is
explain how to draw matplotlib plots in the notebook (using savefig).
> Another issue is that students want to be able to run their numerical
> software as standalone on their laptops. My course would need to help
> them install Sage on Windows, Mac and possibly Linux. Right now, the
> students are using Python directly on the Windows machines in the
> labs, and using ssh on the Macs in the labs to connect to a Linux-
> based cluster running Python,
It sounds like all of that lab installation has nothing to do with the students.
> as well as running on their own laptops.
That's the one part that would involve students actually doing
something. Normal people do successfully install Sage... That said,
running Sage on Windows is definitely not the best experience, since
Windows has fostered a less than optimal ecosystem for open source
software. (Sage is fine on Linux and Mac.)
> I have encouraged them to try the Enthought Academic Download, to ease
> any installation difficulties.
Has that worked well? EPD of course has excellent Windows support.
> These are questions which I did not give myself enough time to study
> in the first semester before I had to start teaching in second
> semester. Perhaps if I start now, I can obtain a clearer idea of how
> to teach numerical linear algebra using Sage from next year onward.
> Would participating in a Sage Days help?
Yes, though the extent to which it helps would depend on the Sage Days.
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to sage-devel+...@googlegroups.com
> For more options, visit this group at http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>
--
(1) If I remember correctly, things are this way because Paul
Zimmerman was "set off" by the above returning a real root, which it
used to do (the default in Maxima, by the way). He had some very good
consistency reasons for preferring the complex branch by default.
(2) Just out of curiosity, did you even *try* the above in Matlab!?
You might be surprised to find that it returns the same thing as Sage!
< M A T L A B >
Copyright 1984-2006 The MathWorks, Inc.
Version 7.2.0.283 (R2006a)
January 27, 2006
To get started, type one of these: helpwin, helpdesk, or demo.
For product information, visit www.mathworks.com.
>> (-2.0)^(1/3)
ans =
0.6300 + 1.0911i
>
> --
> To post to this group, send an email to sage-...@googlegroups.com
> To unsubscribe from this group, send an email to
> sage-devel+...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/sage-devel
> URL: http://www.sagemath.org
>
--
No worries! I've been hanging out on sage-flame too much recently, so
I'm sorry that my response was so inappropriate.
By the way, try typing "matlab" on sage.math.washington.edu.
-- William
I would also like to here what parts David is referring to.
Personally, I've used scipy for various things and have been quite
pleased with everything I've used from scipy, except the stats module.
All the optimization and special functions (say) that I used seemed
pretty good. The stats module definitely needed more work. It may
have improved a lot since then, but I don't know.
-- William
just for the sake of completeness, the next layer above scipy are those scikits:
http://scikits.appspot.com/scikits
especially scikit-learn ( http://scikit-learn.sourceforge.net/stable/
) is quite interesting (ex google summer of code project) and
personally, i hope those newer modules are of higher quality than
those parts in scipy.
H
This is quite strange: I think that scipy calls lsode in this case, is
not it?
lsode is a good program, even if it is old.
The problem may be in the interface (the callbacks?) betwen scipy and lsode.
I agree with you: scipy is low level!
Some parts *cannot* be used: the sparse matrices for example: this is
extremly slow (matrices are build with a dictionary, or at least pure
python).
t.d.
Coincidentally, there is a current thread on scipy-dev to improve the
optimization routine interfaces and docs to take care of issues like
this [1]. My guess is that a similar discussion about the integrate
package might also solicit help in improving the interface, if you or
someone had time to work on it. Also, I bet that there is some sort of
funding that could be applied for to work on this sort of thing as well.
Thanks,
Jason
[1] See http://thread.gmane.org/gmane.comp.python.scientific.user/27823
Sorry; it's on scipy-user. The link is still correct.
Jason