A bit change to create a matrix variable in Python as easy as MATLAB and Julia!

In recent years, python has become very popular due to the rise of data science and machine learning. This is mainly because Python is easy to learn and has a large number of third-party libraries, thus accumulating a large number of users. When Python is applied to scientific computing, there are two problems. One is that Python itself is not fast enough, and the other is that matrix is not a basic data type. The first problem can be well solved by rewriting key codes in C/C++, or by using numba. For the second one, people have invented Numpy which has become the actual matrix computing standard in Python. Although it can do linear algebra, limited by the syntax of Python, using Numpy to initialize a matrix is always not simple enough. We have to do it like this: import numpy as np a=np.array([1,2,3]) b=np.array([[1,2,3],[4,5,6]]) While, you know, in Matlab and Julia(A new ambitious and interesting language) it is in this way: a=[1,2,3] or a=[1 2 3] b=[1,2,3;4,5,6] or b=[1 2 3;4 5 6] Of course, python, as a general-purpose language, is not limited to scientific computing, but also used for crawlers, web development, and even writing GUI programs. Therefore, many developers do not need matrix operations, nor need to use numpy as a standard library. Since numpy has become the cornerstone of Python scientific computing, there is no need to reinvent another wheel, that is, to design new matrix data types. I suggest adding some parsing rules to the List data type to facilitate the initialization of a matrix. (1) Keeping the original syntax of List unchanged,for example: a = [1,2,3] # will be parsed to a normal list. b = [[1,2,3],[4,5,6]] # will be parsed to a normal list,too. Simply put, all the original list syntax remains unchanged. (2) Using semicolons as a flag to make a better integration of a List-like data type and Numpy. The Python interpreter will check whether the numpy library is installed. If not, it will stop running and remind the user to install it. The expected syntax: c = [1,2,3;] or c = [1 2 3;] or c = [1 2 3] Notice the semicolon after the last number. If numpy is found, c will be parsed as a Numpy ndarray. All these forms are equivelent to c = np.array([1,2,3]). For a vector, the semicolon is the key for Python to parse it as a Numpy ndarray. d=[1,2,3;4,5,6] or d=[1,2,3;4,5,6;] or d=[1 2 3;4 5 6] or d=[1 2 3;4 5 6;] Notice the semicolons. If numpy is found, d will be parsed as a Numpy ndarray. All these forms are equivelent to d=np.array([[1,2,3],[4,5,6]]) You see,for defining a matrix or a vector,it will be nearly as simple as Matalab or Julia! Thank you!

On Nov 8, 2019, at 04:19, yejustme@163.com wrote:
What about this? def A(*values, **kw): return np.array(values, **kw) And now: c = A(1,2,3) d = A([1,2,3], [4,5,6]) Same number of characters, no custom parsing, no weird trailing semicolon that looks like noise to the reader, no need for any change to Python itself, just define the one-liner wherever you want to use it, and you can use it.

I am pretty sure this is a backwards incompatible change. It isn't likely syntax, but I think it is possible. I also don't like having to wait until the end of the expression to find out it isn't a list. And also seems like it would be easy to miss in a non-trivial case. How would you be parsed? a = [1, 2, 3; 4, 5, 6] On Fri, Nov 8, 2019, 09:13 <yejustme@163.com> wrote:

numpy already has a MATLAB-like sting parsing matrix creating function, buried in the "Matrix" class: In [1]: import numpy as np In [2]: np.mat('1 2 3; 4 5 6') Out[2]: matrix([[1, 2, 3], [4, 5, 6]]) That gives you a Matrix, not an array, butyou could wrap it in a little utility to convert. But: 1) Matrix is pretty much being phased out -- with the @ operator, it has little use 2) Hardly anyone ever uses is anyway. Yes, it's a bit more typing to have to use nested lists for literals, and that does make a notable difference in a REPL, but still not a big deal --how often do you need to type, at a REPL, a large array?? if you are doing that a lot, you really need a better workflow! -CHB On Sat, Nov 9, 2019 at 2:51 PM Todd <toddrjen@gmail.com> wrote:
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

On Thu, Nov 7, 2019, at 22:19, yejustme@163.com wrote:
Possible alternate mechanisms: Make any such expression implicitly call a special function named e.g. __matrixhook__ - called by name, so a default in builtins and one could be set locally in a module or imported from numpy - or have the special function be installable in sys.matrixhook rather than based on a lexically scoped special name The default hook could: - raise an error suggesting to install numpy as in OP's suggestion - just build a list of lists These would require slightly more boilerplate (from numpy import __matrixhook__, or perhaps numpy could automatically initialize sys.matrixhook when loaded), but wouldn't favor a single non-stdlib library.

On Nov 11, 2019, at 04:11, Random832 <random832@fastmail.com> wrote:
This could also be a use for user-defined prefixes or suffixes: c = a[1, 2, 3] d = a[[1,2,3],[4,5,6]] e = [a[1,2,3], a[4,5,6]] This looks up the installed prefix “a” and calls its handler with the stream of tokens, or their text, or the AST subtree, or the cooked list value. There could even be a default handler that recommends NumPy before raising the “no installed handler” error. Maybe you could even do this: f = a[1,2,3]f32 … to specify the dtype as a suffix. And if you want to reuse most of your code in Jython with a Java array library instead of NumPy, it could install the same affix as NumPy. And if NumPy one day goes the way of Numeric, merging with another library into something even better, Python wouldn’t be calling an obsolete library forever, it would be calling whatever you installed. You can create arrays, and multidimensional arrays, and even lists of arrays, and it all seems pretty obvious to read. And I think a prefix or suffix signals what’s going on better than whether or not there a semicolon somewhere inside. (If there are cases where a semicolon actually helps readability rather than being just noise, which I think may be the case for some multidimensional arrays, especially where one of the dimensions is 1, then that wouldn’t be too hard to allow.) Last time user-defined affixes were discussed a few months ago, it fizzled out. But that discussion was about strings primarily, numbers secondarily, with only brief offhand mentions of container displays. It’s possible that affixing displays would have fewer problems or read more compelling or just have an advocate willing to push harder than with strings. There are a couple of added issues, but neither one seems insurmountable. First, if you go with tokens or text, a display is a stream of tokens rather than a single one. Also, you’d have to figure out what to do about comprehensions, because they’re (at least as far as the grammar) a kind of display. On the other hand, except for dtype example, you can already do all of this exactly the same without changing Python just by using A(...) instead of a[...], and I don’t think it looks that much worse.

On Nov 8, 2019, at 04:19, yejustme@163.com wrote:
What about this? def A(*values, **kw): return np.array(values, **kw) And now: c = A(1,2,3) d = A([1,2,3], [4,5,6]) Same number of characters, no custom parsing, no weird trailing semicolon that looks like noise to the reader, no need for any change to Python itself, just define the one-liner wherever you want to use it, and you can use it.

I am pretty sure this is a backwards incompatible change. It isn't likely syntax, but I think it is possible. I also don't like having to wait until the end of the expression to find out it isn't a list. And also seems like it would be easy to miss in a non-trivial case. How would you be parsed? a = [1, 2, 3; 4, 5, 6] On Fri, Nov 8, 2019, 09:13 <yejustme@163.com> wrote:

numpy already has a MATLAB-like sting parsing matrix creating function, buried in the "Matrix" class: In [1]: import numpy as np In [2]: np.mat('1 2 3; 4 5 6') Out[2]: matrix([[1, 2, 3], [4, 5, 6]]) That gives you a Matrix, not an array, butyou could wrap it in a little utility to convert. But: 1) Matrix is pretty much being phased out -- with the @ operator, it has little use 2) Hardly anyone ever uses is anyway. Yes, it's a bit more typing to have to use nested lists for literals, and that does make a notable difference in a REPL, but still not a big deal --how often do you need to type, at a REPL, a large array?? if you are doing that a lot, you really need a better workflow! -CHB On Sat, Nov 9, 2019 at 2:51 PM Todd <toddrjen@gmail.com> wrote:
-- Christopher Barker, PhD Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython

On Thu, Nov 7, 2019, at 22:19, yejustme@163.com wrote:
Possible alternate mechanisms: Make any such expression implicitly call a special function named e.g. __matrixhook__ - called by name, so a default in builtins and one could be set locally in a module or imported from numpy - or have the special function be installable in sys.matrixhook rather than based on a lexically scoped special name The default hook could: - raise an error suggesting to install numpy as in OP's suggestion - just build a list of lists These would require slightly more boilerplate (from numpy import __matrixhook__, or perhaps numpy could automatically initialize sys.matrixhook when loaded), but wouldn't favor a single non-stdlib library.

On Nov 11, 2019, at 04:11, Random832 <random832@fastmail.com> wrote:
This could also be a use for user-defined prefixes or suffixes: c = a[1, 2, 3] d = a[[1,2,3],[4,5,6]] e = [a[1,2,3], a[4,5,6]] This looks up the installed prefix “a” and calls its handler with the stream of tokens, or their text, or the AST subtree, or the cooked list value. There could even be a default handler that recommends NumPy before raising the “no installed handler” error. Maybe you could even do this: f = a[1,2,3]f32 … to specify the dtype as a suffix. And if you want to reuse most of your code in Jython with a Java array library instead of NumPy, it could install the same affix as NumPy. And if NumPy one day goes the way of Numeric, merging with another library into something even better, Python wouldn’t be calling an obsolete library forever, it would be calling whatever you installed. You can create arrays, and multidimensional arrays, and even lists of arrays, and it all seems pretty obvious to read. And I think a prefix or suffix signals what’s going on better than whether or not there a semicolon somewhere inside. (If there are cases where a semicolon actually helps readability rather than being just noise, which I think may be the case for some multidimensional arrays, especially where one of the dimensions is 1, then that wouldn’t be too hard to allow.) Last time user-defined affixes were discussed a few months ago, it fizzled out. But that discussion was about strings primarily, numbers secondarily, with only brief offhand mentions of container displays. It’s possible that affixing displays would have fewer problems or read more compelling or just have an advocate willing to push harder than with strings. There are a couple of added issues, but neither one seems insurmountable. First, if you go with tokens or text, a display is a stream of tokens rather than a single one. Also, you’d have to figure out what to do about comprehensions, because they’re (at least as far as the grammar) a kind of display. On the other hand, except for dtype example, you can already do all of this exactly the same without changing Python just by using A(...) instead of a[...], and I don’t think it looks that much worse.
participants (5)
-
Andrew Barnert
-
Christopher Barker
-
Random832
-
Todd
-
yejustme@163.com