<h1>Introduction</h1>
<p>In MATLAB, Octave and Julia, matrices are defined extremely easily. The following line defines a 2x2 matrix in any of those languages:</p>
<pre><code>A = [1 3; 4 -1]
</code></pre>
<h1>Case 1: Python</h1>
<p>Someone who's &quot;native&quot; language is Python, <a href="https://chat.stackexchange.com/transcript/message/63018726#63018726"><strong>told me that</strong></a> the simplest way to accomplish the above goal would be:</p>
<pre class="lang-python prettyprint-override"><code>from numpy import matrix 
A = matrix('1 3;4 -1')
</code></pre>
<p>I see that <a href="https://numpy.org/doc/stable/reference/generated/numpy.matrix.html" rel="nofollow noreferrer">the official NumPy documentation recommends to use an even more verbose syntax</a>:</p>
<blockquote>
<p>&quot;It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future.&quot;</p>
</blockquote>
<p>The simplest way to define the matrix is then even more verbose:</p>
<pre class="lang-python prettyprint-override"><code>from numpy import array
A = array([[1,3],[1,9,-2,1]])
</code></pre>
<h3>What are the advantages of requiring us to type &quot;array&quot; or &quot;matrix&quot;, or the disadvantages that we sacrifice in other languages by not requiring any such words?</h3>
<p>I understand that <em><strong>a lot</strong></em> of Python programs don't need matrices, but Python is used enormously in machine learning (<a href="https://ai.stackexchange.com/tags">it's the most popular language tag in the Artificial Intelligence Stack Exchange</a>), and Quantum Computing (<a href="https://quantumcomputing.stackexchange.com/tags">the Python program Qiskit is the <em><strong>most</strong></em> popular tag in the Quantum Computing Stack Exchange</a>), and other areas with enormous usage of matrices, so I wonder what harm/disadvantages the type of functionality described in the first paragraph, would introduce?</p>
<h1>Case 2: R</h1>
<p>What surprises me even more, is the syntax in R. Unlike Python, which you might say is not designed specifically for numerical computation but is rather meant to be more of a &quot;general purpose&quot; language, R was designed specifically for numerical computing. There are <a href="https://techvidvan.com/tutorials/r-matrix/" rel="nofollow noreferrer">several ways to define a matrix in R and they all seem to have syntax that's as cumbersome as the Python syntax</a>. R has the <code>matrix()</code> function akin to the NumPy function that the official NumPy documentation recommends <em>not</em> to use, and it has other methods that also rely on typing out a function name in words, rather than just having a syntax that's reserved for defining matrices.</p>


<syntax><literals>
What are the disadvantages of being able to define a matrix without typing any words?
CC BY-SA 4.0
2023-05-30T06:59:29.153Z
Interestingly, APL, where matrices (and higher-dimensional arrays) are bread-and-butter, doesn't have a notation for them either (yet — I'm working on that), but does have a less noisy equivalent of the NumPy syntax: `1 3⍴1 9 ¯2 1`. However, this has lead APLers to develop several techniques to get literal matrices into their programs. None of their are particularly pleasant.
Adám
CC BY-SA 4.0
2023-05-30T07:01:40.627Z
I think you should distinguish between literal notation, as in `[[1,9],[-2,1]]` and a more declarative/computational notation like `[[2,2],[1,9,-2,1]`. Both have their merits, and a language can support either or both.
Adám
CC BY-SA 4.0
2023-05-30T11:25:18.803Z
There is a more general question waiting to get out here "what is a good syntax for tensor initialisation?"
Bruce Adams
CC BY-SA 4.0
2023-05-30T11:38:19.267Z
In fact I've decided to ask it - https://languagedesign.stackexchange.com/q/1290/285
Bruce Adams
CC BY-SA 4.0
2023-05-30T12:57:40.197Z
Aside: could there be any execution efficiency gained if the "matrix" keyword implied that the object is guaranteed to be rectangular, so the compiler or interpreter knows that all rows have the same length?
Ray Butterworth
<blockquote>
<p>I understand that a lot of Python programs don't need matrices, but [...] I wonder what harm/disadvantages the type of functionality described in the first paragraph, would introduce?</p>
</blockquote>
<p>Well, you've <strong>thrown away the list literal syntax used in nearly every program in favour of matrix literals used by very few</strong>, which is a bit of a drawback. The features you are using are from an external library, in any case, so their capacity to make fundamental extensions to the language is fairly limited. There is no reason they (or you) couldn't provide a shorter function for building such a matrix if you wanted, though:</p>
<pre class="lang-python prettyprint-override"><code>class MatrixCreator:
    def __getitem__(self, *args):
        return numpy.array(args)
m = MatrixCreator()
A = m[[1,3],[4,-1]]
</code></pre>
<p>Pretty close? I know you don't like commas, though.</p>
<blockquote>
<p>R was designed specifically for numerical computing. There's several ways to define a matrix in R and they all seem to have syntax that's as cumbersome as the Python syntax.</p>
</blockquote>
<p>I don't think I'd agree with that characterisation of R, and instead I'd say it was designed specifically for statistical work, which isn't <em>so</em> distant from numerical computing, but R's intention definitely isn't coextensive with MATLAB's. I can understand if matrices are also deemed not-quite-core, or were at the early stages of development. Frankly, though, nobody is arguing that R is a <em>well-designed</em> language and PL people will likely overwhelmingly say the reverse — but it's also very popular and effective with its user base, so what do we know?</p>
<p>R is an interesting case here because it doesn't have literal syntax for nearly anything: even regular unidimensional vectors are written as <code>c</code> function calls. This <em>is</em> a choice, and it's somewhat pervasive in the language that you can alias or replace core function elements.</p>
<p>I suspect that matrix <em>literals</em>, as opposed to data-driven matrix construction, are relatively uncommon across the space of both R and Python programs — though perhaps not in the ones you encounter in your work! — and so it's not really something that would ever be in consideration.</p>
<hr />
<p>In both of these cases, and in similar questions more broadly, the operative factor is always <strong>what you're trading off against including feature X</strong>, not what you're missing out on by not having it: could you use that syntax better for something else? is this the kind of program you're trying to enable? what are the maintenance costs of steering into this direction? All of these questions can have different reasonable results, with none of them innately superior.</p>
<p>Once you make some choices, others are determined for you: if the <code>[]</code> characters have been used up already by something high-priority, then the other core way to express constructing a value comes in instead - a function call, and that has a name. <em>Almost everything</em> in <em>almost every</em> language is not syntactically supported, but rests on a generic (usually named) construction like a function or method call, class construction, and so on. That's in a lot of ways why those features exist, so that new non-core functionality can be added in after the fact.</p>
<p>It's unlikely to make sense for Python to provide syntax-level bindings to an optional third-party library occupying core keyboard characters, but R potentially could have, losing out instead on some of the functional behaviour they wanted. Other languages make different trade-offs, and probably most of them make the right ones for them.</p>
<p>Julia has made different choices, and it's entirely reasonable to decide to use it because of those. It's had the benefit of seeing decades of each of the others in practice to work from, and to choose to focus its energies in a particular direction and on a particular set of ergonomic pathways. It may just be more suitable for what you want to do. It doesn't have the same broad-ranging ecosystem outside that domain that Python does, but that may not matter to you—or it may be vital in ruling it out.</p>
<hr />
<p>There's also a second undercurrent in the question, which is &quot;why aren't all languages like the one I like? I'd like them to be&quot;. This is a common feeling; lots of times while writing in one language I wish for a feature I'm accustomed to in another. This isn't one with a tidy answer, but fundamentally this <em>is</em> why there are different languages at all, and those tradeoffs compound with each other to point at radically different results. The Haskell programmers and the Java programmers may wish for lazy evaluation and mutable state in each other's language, even when they're working in their shared domains, but they get different benefits instead.</p>
<p>Why do people use languages that seem suboptimal for their purpose? Sometimes it's inertia, sometimes there are secondary trade-offs that make it worthwhile, sometimes there are ecosystem benefits from libraries or from ease of collaborating with people in different domains. Sometimes one maps better to how they think than another: <strong>perhaps we'll get another question asking why MATLAB doesn't use words to describe what you're creating, and instead uses some cryptic punctuation and semantic whitespace</strong>. Ultimately, these are often not constructive questions to explore outside of a concrete local situation where standardising on a platform is useful.</p>

CC BY-SA 4.0
2023-05-30T06:19:44.713Z
2023-05-31T06:00:25.160Z
2023-05-31T06:00:25.160Z
Michael Homer
CC BY-SA 4.0
2024-01-31T18:20:00.853Z
*"It's unlikely to make sense for Python to provide syntax-level bindings to an optional third-party library occupying core keyboard characters"* << Well, actually, [it was proposed to add operator `@`](https://peps.python.org/pep-0465/) to python, specifically for use with numpy. Operator `@` is now officially part of the core language, and any class can implement this operator by including a `__matmul__` method, even though no core type implements it, and it is used almost-exclusively by numpy.
Stef
# What are the disadvantages of being able to define a matrix without typing any words?
Posted by: username_0 on 2023-05-30
Introduction
In MATLAB, Octave and Julia, matrices are defined extremely easily. The following line defines a 2x2 matrix in any of those languages:
A = [1 3; 4 -1]

Case 1: Python
Someone who's "native" language is Python, told me that the simplest way to accomplish the above goal would be:
from numpy import matrix 
A = matrix('1 3;4 -1')

I see that the official NumPy documentation recommends to use an even more verbose syntax:

"It is no longer recommended to use this class, even for linear algebra. Instead use regular arrays. The class may be removed in the future."

The simplest way to define the matrix is then even more verbose:
from numpy import array
A = array([[1,3],[1,9,-2,1]])

What are the advantages of requiring us to type "array" or "matrix", or the disadvantages that we sacrifice in other languages by not requiring any such words?
I understand that a lot of Python programs don't need matrices, but Python is used enormously in machine learning (it's the most popular language tag in the Artificial Intelligence Stack Exchange), and Quantum Computing (the Python program Qiskit is the most popular tag in the Quantum Computing Stack Exchange), and other areas with enormous usage of matrices, so I wonder what harm/disadvantages the type of functionality described in the first paragraph, would introduce?
Case 2: R
What surprises me even more, is the syntax in R. Unlike Python, which you might say is not designed specifically for numerical computation but is rather meant to be more of a "general purpose" language, R was designed specifically for numerical computing. There are several ways to define a matrix in R and they all seem to have syntax that's as cumbersome as the Python syntax. R has the matrix() function akin to the NumPy function that the official NumPy documentation recommends not to use, and it has other methods that also rely on typing out a function name in words, rather than just having a syntax that's reserved for defining matrices.
**Tags:** syntax literals
> **Comments:**
> **username_1**: Interestingly, APL, where matrices (and higher-dimensional arrays) are bread-and-butter, doesn't have a notation for them either (yet — I'm working on that), but does have a less noisy equivalent of the NumPy syntax: `1 3⍴1 9 ¯2 1`. However, this has lead APLers to develop several techniques to get literal matrices into their programs. None of their are particularly pleasant.  
> **username_1**: I think you should distinguish between literal notation, as in `[[1,9],[-2,1]]` and a more declarative/computational notation like `[[2,2],[1,9,-2,1]`. Both have their merits, and a language can support either or both.  
> **username_2**: There is a more general question waiting to get out here "what is a good syntax for tensor initialisation?"  
> **username_2**: In fact I've decided to ask it - https://languagedesign.stackexchange.com/q/1290/285  
> **username_3**: Aside: could there be any execution efficiency gained if the "matrix" keyword implied that the object is guaranteed to be rectangular, so the compiler or interpreter knows that all rows have the same length?  
---
## 1 answers
### Answer by username_4 on 2023-05-30. Score: 9
I understand that a lot of Python programs don't need matrices, but [...] I wonder what harm/disadvantages the type of functionality described in the first paragraph, would introduce?

Well, you've thrown away the list literal syntax used in nearly every program in favour of matrix literals used by very few, which is a bit of a drawback. The features you are using are from an external library, in any case, so their capacity to make fundamental extensions to the language is fairly limited. There is no reason they (or you) couldn't provide a shorter function for building such a matrix if you wanted, though:
class MatrixCreator:
    def __getitem__(self, *args):
        return numpy.array(args)
m = MatrixCreator()
A = m[[1,3],[4,-1]]

Pretty close? I know you don't like commas, though.

R was designed specifically for numerical computing. There's several ways to define a matrix in R and they all seem to have syntax that's as cumbersome as the Python syntax.

I don't think I'd agree with that characterisation of R, and instead I'd say it was designed specifically for statistical work, which isn't so distant from numerical computing, but R's intention definitely isn't coextensive with MATLAB's. I can understand if matrices are also deemed not-quite-core, or were at the early stages of development. Frankly, though, nobody is arguing that R is a well-designed language and PL people will likely overwhelmingly say the reverse — but it's also very popular and effective with its user base, so what do we know?
R is an interesting case here because it doesn't have literal syntax for nearly anything: even regular unidimensional vectors are written as c function calls. This is a choice, and it's somewhat pervasive in the language that you can alias or replace core function elements.
I suspect that matrix literals, as opposed to data-driven matrix construction, are relatively uncommon across the space of both R and Python programs — though perhaps not in the ones you encounter in your work! — and so it's not really something that would ever be in consideration.

In both of these cases, and in similar questions more broadly, the operative factor is always what you're trading off against including feature X, not what you're missing out on by not having it: could you use that syntax better for something else? is this the kind of program you're trying to enable? what are the maintenance costs of steering into this direction? All of these questions can have different reasonable results, with none of them innately superior.
Once you make some choices, others are determined for you: if the [] characters have been used up already by something high-priority, then the other core way to express constructing a value comes in instead - a function call, and that has a name. Almost everything in almost every language is not syntactically supported, but rests on a generic (usually named) construction like a function or method call, class construction, and so on. That's in a lot of ways why those features exist, so that new non-core functionality can be added in after the fact.
It's unlikely to make sense for Python to provide syntax-level bindings to an optional third-party library occupying core keyboard characters, but R potentially could have, losing out instead on some of the functional behaviour they wanted. Other languages make different trade-offs, and probably most of them make the right ones for them.
Julia has made different choices, and it's entirely reasonable to decide to use it because of those. It's had the benefit of seeing decades of each of the others in practice to work from, and to choose to focus its energies in a particular direction and on a particular set of ergonomic pathways. It may just be more suitable for what you want to do. It doesn't have the same broad-ranging ecosystem outside that domain that Python does, but that may not matter to you—or it may be vital in ruling it out.

There's also a second undercurrent in the question, which is "why aren't all languages like the one I like? I'd like them to be". This is a common feeling; lots of times while writing in one language I wish for a feature I'm accustomed to in another. This isn't one with a tidy answer, but fundamentally this is why there are different languages at all, and those tradeoffs compound with each other to point at radically different results. The Haskell programmers and the Java programmers may wish for lazy evaluation and mutable state in each other's language, even when they're working in their shared domains, but they get different benefits instead.
Why do people use languages that seem suboptimal for their purpose? Sometimes it's inertia, sometimes there are secondary trade-offs that make it worthwhile, sometimes there are ecosystem benefits from libraries or from ease of collaborating with people in different domains. Sometimes one maps better to how they think than another: perhaps we'll get another question asking why MATLAB doesn't use words to describe what you're creating, and instead uses some cryptic punctuation and semantic whitespace. Ultimately, these are often not constructive questions to explore outside of a concrete local situation where standardising on a platform is useful.
> **Comments:**
> **username_5** (2024-01-31): *"It's unlikely to make sense for Python to provide syntax-level bindings to an optional third-party library occupying core keyboard characters"* << Well, actually, [it was proposed to add operator `@`](https://peps.python.org/pep-0465/) to python, specifically for use with numpy. Operator `@` is now officially part of the core language, and any class can implement this operator by including a `__matmul__` method, even though no core type implements it, and it is used almost-exclusively by numpy.  
---
