5
$\begingroup$

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?

$\endgroup$
8
  • 7
    $\begingroup$ How do you tell the difference between the product of the variables $a$ and $b$, and the variable called $ab$? $\endgroup$
    – vadim123
    Mar 12, 2015 at 1:14
  • 1
    $\begingroup$ I have used Mathematica, I have not used Sage and I have very little experience with MatLab. But I can tell you this much: Mathematica has a rule that variable names can't start with a numeral. So x3 is a valid variable name, 3x is not. $\endgroup$ Mar 12, 2015 at 2:23
  • $\begingroup$ @robertsoupe I can confirm Matlab doesn't like numerical or even special/reserved characters as variable prefix. $\endgroup$
    – Chinny84
    Mar 12, 2015 at 14:21
  • $\begingroup$ This is not a question about mathematics, it's a question about software. $\endgroup$ Jun 7, 2016 at 11:58
  • $\begingroup$ @Najib Idrissi Yeah now that you mention it, I agree. Is there something I should do? $\endgroup$ Jun 7, 2016 at 12:03

4 Answers 4

15
$\begingroup$

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/.

$\endgroup$
4
$\begingroup$

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$?

$\endgroup$
1
  • $\begingroup$ Since x is defined as symbolic variable, I think the ambiguity can be avoided. I agree that there is no way to drop the first '*' sign in f = 2*3*x. However I believe that f = 2*3x is not ambiguous and it will still have an error thrown for not having a * between the 3 and the x. $\endgroup$ Mar 12, 2015 at 1:21
3
$\begingroup$

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.

$\endgroup$
6
  • $\begingroup$ You do know that Wolfram Alpha is powered by Wolfram Mathematica, right? wolframalpha.com/about.html $\endgroup$ Mar 12, 2015 at 2:08
  • 4
    $\begingroup$ It's less about development time or performance than having a well-defined rule set that creates parseable code with non-ambiguous and predictable results. For instance, is x3 a variable name or is it shorthand x*3. If you only allow numbers on the left to imply multiplication, than what about x2x? Is it x * 2 * x or is x2 a variable such it means x2 * x. It just won't work. $\endgroup$ Mar 13, 2015 at 21:40
  • $\begingroup$ @AnonSubmitter85 is correct that creation of a well-defined rule set is the critical issue. I would go further and describe the accepted answer as just completely incorrect. Another example illustrating AnonSubmitter's point is a(x+y). Is this multiplication or function evaluation? Mathematica gets around this issue by using brackets (as in a[x+y]) for function evaluation. Thus, a(x+y) can represent multiplication. Another possibility is to require an explicit multiplication or, at least, a space. $\endgroup$ Mar 21, 2015 at 14:24
  • $\begingroup$ @RobertSoupe I don't see how your point that Alpha is powered by Mathematica is relevant. Certainly, the results produced by Alpha are computed by Mathematica. The parsing of input is completely different between the two, however. A very simple example is just xy which represents a single symbol in Mathematica but is parsed as the product x*y in Wolfram Alpha. $\endgroup$ Mar 21, 2015 at 14:28
  • $\begingroup$ @Mark McClure My point was that it would be possible for MATLAB or similar programs to require stars for multiplication only in cases where there would be no legal way for a combination of letters and numbers to mean anything other then multiplication as in the example given. But if a this were implemented it would increase development time and it would increase computation time. AnonSubcitter85's point is well taken though and I have edited accordingly. $\endgroup$
    – nateAtwork
    Mar 21, 2015 at 19:02
1
$\begingroup$

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!

$\endgroup$

Not the answer you're looking for? Browse other questions tagged .