Why can you not omit the '*' sign for multiplication in Sage, Matlab, and other mathematical software?

1.5k Views Asked by At

Question

I have some experience with Sage and Matlab. Both mathematical software packages require you to have the '*' sign when multiplying symbolic variables with integers. For example, in Matlab you have to define a function as f = 3*x instead of f = 3x. The latter throws a Error: Unexpected MATLAB expression in between the 3 and the x.

Attempts

I have done a google search of why the '*' is required with little luck. I found a Intro to Sage documentation which states the following:

[e]xponents are '^', and multiplication must be '*'. (There are good reasons for this.)

What exactly is this "good reason"?

I found a stack exchange answer that hints at potentially why this is the case:

(And maybe also worth mentioning is that of course a CAS like Mathematica has to be told how to interpret something like ab and thus we'll have to write "" with space inbetween which makes it look and feel more like "a b's".)

However, I am confused by this comment since defining a function as f = 3 x with a space between the 3 and the x still throws the same error in Matlab. If humans are able to identify that 3x is 3*x, how come popular mathematical software packages cannot?

4

There are 4 best solutions below

6
On BEST ANSWER

The short answer is that the programs weren't designed that way because and the designers didn't want to put the development time or take the performance hit in order to anticipate every possible way a user might want to multiply. But this needs a bit more explanation.

It would make sense for the example you gave for f=3x to be automatically changed to f=3*x, but if you replaced the 3 with another variable y, a program like MATLAB would assume you wanted to multiply a new variable xy that you forgot to initialize. there are other situations where the user's intent would be ambiguous without a * (as illustrated by some comments on this answer). It would be possible for MATLAB to look at each equation and try and determine when the user needed a *, but this would require both computation and development time. It would also make code less consistent and easy to read.

There are software tools that more intuitive than MATLAB and Mathematica that will recognise 3x as 3*x (such as Wolfram Alpha), but this is because the target audience is more broad and intuitiveness is valued over speed and, to a certain extent, capability.

1
On

I'm sure there are many reasons for this. An obvious one is to avoid ambiguity in multiplication. For example, if expressions like

f = 3x

were allowed, then how would

f = 23x

be interpreted? Would it be $f=(23)\cdot x$ or $f=2\cdot3\cdot x$?

0
On

In SageMath (also known as Sage), implicit multiplication can be turned on for your comfort!

In fact, several levels of implicit multiplication can be set:

  • 0 - Do nothing
  • 1 - Numeric followed by alphanumeric
  • 2 - Closing parentheses followed by alphanumeric
  • 3 - Spaces between alphanumeric
  • 10 - Adjacent parentheses

To illustrate these different levels:

sage: implicit_multiplication(1)
sage: 2x
2*x

sage: implicit_multiplication(2)
sage: (x + 1)x
(x + 1)*x

sage: implicit_multiplication(3)
sage: x x
x^2

sage: implicit_multiplication(10)
sage: (x + 1)(x + 1)
(x + 1)^2

Level 10 is set that high because of the danger of confusion with function calls, for instance (x)(x) might mean the function x applied to x, which is x, or the product of x and x, which is x^2.

Read more about Sage's approach to implicit multiplication in the documentation.

To experiment without having to install Sage, you can use Sage's cell server at http://aleph.sagemath.org/ or http://sagecell.sagemath.org/.

0
On

At a certain point Maple changed to allow multiplication like this, $$ \mathtt{product = variable1\ variable2.} $$ with a space in there (can you see the space?). But since that time the Maple question boards have been filled with beginners who get it wrong and cannot figure out what's up. Personally, I always write the * in Maple. And in fact that is required in the majority of computer languages. For more information on why, ask in a computer forum!