Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

From time to time, I would like to use Mathematica purely numerically, e.g., plotting a function which is defined as an integral which cannot be solve analytically or a solution of a differential equation, or ... . It turns out that Mathematica is rather efficient (I would say comparable to Matlab) if you know what you are doing. However, Mathematica even though it has incredible numerical possibilities makes numerical life quite hard. Problems which occur:

  1. it tries to evaluate a function analytically, because you type something like f[x] where x does not have (yet) a numerical value.

  2. it tries to symbolically preprocess expressions (check out the "SymbolicProcessing" option of NIntegrate) even if I know that no symbolic solution exists

  3. lists are not properly packed, because somewhere Pi appears which does not get converted to a numerical value

  4. ...

I would like very much if there would be a switch, which changes Mathematica into purely numerical mode which would include:

  • a) all numbers are converted in machine numbers (lists are packed)

  • b) there is no attempted of any symbolic operation, any function f[x] with x not a number remains unevaluated (of course, I know the trick to define f[x_?NumericQ] but the numerical mode would save me lots of typing)

  • c) turn off symbolical (pre-)processing

Is there a way which I can get the required behavior a) to c)?

Do other people suffer from the same problems?

It turns out that for me it is not a problem any more because I know how to deal with it; however, inexperienced user typically suffer from very bad performance if they would like to use Mathematica for numerical means. In fact Mathematica is a marvelous numerical tool with good plotting capabilities.

I hope that some of you might even now better than me what I am hunting for. I guess not all problems can be avoided. A problematic expression is, e.g., the following Plot[{Re[#], Im[#]}&[f[x]], {x,0,1}]. Plot does not know that it has a list as an argument, but running evaluate on it decreases the performance because f[x] is evaluated twice. What I am looking for is to put Mathematica in a mode which a typical user with a typical problem does not suffer the low speed as it does now. So typing Pi + 1 should result in 4.1416, {Pi,1} should give a packed array, and Mathematica should forget about commutativity and associativity of addition since it does not hold for numerical problems.

share|improve this question
    
While numerical addition is not associative, it is commutative (you might think of "commutations" like $a+b+c$ vs $a+c+b$, but when removing associativity you'll note that one is $(a+b)+c$ while the other is $(a+c)+b$, and you cannot transform one into the other using commutativity without associativity). –  celtschk Feb 24 '12 at 16:15
    
@celtschk it doesn't even have the guarantee of commutativity. The simplest example: 1.*10^16 + 1. - 1.*10^16 returns 0, and will return always return 0 unless 1. is the last term. Additionally, in mma, that isn't even completely true as it will do some pruning prior to evaluation. For example, f[a_,b_,c_]:=1/(a^2 - 2 a b + b^2 - c^2) will overflow for f[10.^10, 10.^10, 10.] despite the first three terms cancelling. –  rcollyer Feb 24 '12 at 16:29
    
This question could be interpreted as: how can beginners use Mathematica effectively? I think that the problems you describe are very real and very practical when one is working with a colleague who is not too familiar with Mathematica. –  Szabolcs Feb 24 '12 at 16:44
    
@rcollyer: Please read my comment again. You cannot get the 1.*10^16 away from the last place by commutativity alone because if you have no associativity, the expression a+b+c has to be evaluated as (a+b)+c. Since Mathematica does assume associativity for addition, whatever Mathematica does is no indication about what can or cannot be done without associativity. –  celtschk Feb 24 '12 at 16:47
    
@Szabolcs, you have that problem for any language (also human). It needs time and willingness to learn. Would a tutorial (howto) help on this matter? –  user21 Feb 24 '12 at 16:50
show 11 more comments

1 Answer 1

up vote 24 down vote accepted

I think it is important to realize that switching off symbolics (also for numerics) would deprive you of some of the best optimizations Mathematica has to offer. When Mathematica tries to use symbolics on numerical input, that can mean things like term analysis or optimizations for the compiler. Just think about it, this would mean that the compiler, for example, cannot factor out common terms, NIntgrate could not integrate oscillatory integrals because it can not do analysis on the integrand. It does not matter if an analytical solution exists a symbolic analysis can never the less be very useful. Andrew made a good presentation about what is happening under the hood you might find Hybrid Computing interesting.

Seen the benefits just mentioned and the fact that it is not impossible to figure out how to use Mathematica efficiently, make me feel that such a switch is undesirable. I can hopefully help you with a) see below.

For b) I feel that the typing _?NumericQ is not that bad especially compared to the downsides of not having symbolic analysis.

c) In some functions you can switch that off via a Method option. You could set these in a file, system options or some such.

My suggestion for your switch is to collect the various items and set them in an init.m file.

Concerning packed array you might find a visual PackedArrayForm helpful.

$Post = Developer`PackedArrayForm

This then gives:

a = Range[20]
"PackedArray"[Integer, "<" 20 ">"]

If you then set

a[[1]]=1.;
a

Will give you a list.

{1., 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, \
20}

This allows for a relatively easy distinction between packed and non packed. For the automatic conversion to packed arrays consider this:

Table[Pi, {5}]
{\[Pi], \[Pi], \[Pi], \[Pi], \[Pi]}

If you set (or some more clever variant)

$Pre = Developer`ToPackedArray[N[#]] &

and evaluate

Table[Pi, {5}]
"PackedArray"[Real, "<" 5 ">"]

Again, for your switch you could have these in your init.m file.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.