[Numpy-discussion] Assigning complex value to real array

Andrew P. Mullhaupt doc@zen-pharaohs....
Thu Oct 7 18:21:53 CDT 2010


  On 10/7/2010 4:50 PM, Christopher Barker wrote:
> Most of the features of numpy were in fact carefully designed for good
> reason
I was around back in those days.  I know a bit about the design process 
of which you speak.
> In [22]: a = np.array((5,6), dtype=np.int32)
>
> In [26]: a += 1.2
>
> In [27]: a
> Out[27]: array([6, 7])
>
> In [28]: a[0] = 4.5
>
> In [29]: a
> Out[29]: array([4, 7])
>
> Would you have wanted your array to be upcast to a float?

Either that or raise a warning or error.

> In [50]: a
> Out[50]: array([5, 6], dtype=uint8)
>
> In [51]: a+=3000
>
> In [52]: a
> Out[52]: array([189, 190], dtype=uint8)
>
> or upcast to a bigger int type?

Same as above.

>> You wouldn't want to rewrite any of numpy, just add a new class.
> If you understood what the issue was, you'd know it isn't that simple.

The issue is that it is a fraud for the interpreter to silently pretend 
that it has succesfully completed an assignment which makes no sense at all.

You know, in FORTRAN, you used to be able to assign a value to a 
literal, say you assigned the value 12 to the literal 4:

       Z = 4 + 1

Z could have the value 13. Originally, no compile time or run time error 
or warning was given. Now I forget the actual FORTRAN syntax needed to 
do this - it's not a technique I used in any program. I have a vague 
recollection that in the earliest days you could just write

     4 = 12

but as compiler writers got wise to these tricks it became necessary to 
add syntax to achieve this (insane) result.

Well yes, FORTRAN was 'carefully designed' - in fact by very smart 
people. But frankly, this example is one of the things in the 
programming world that is just wrong.

Now if you understood what FORTRAN was supposed to be, from one point of 
view - that FORTRAN was a program to translate formulas into assembler 
language - then you would realize that in the world of assembler 
language, there aren't "variables" that have "values" - there are memory 
addresses which have contents, so what is so crazy about referring to a 
memory address with a numeral as opposed to a string literal?

So yes, we can construct a point of view with which to excuse that 
disaster. In fact, that point of view is how that disaster actually 
happened. The people writing the FORTRAN compiler wrote code to resolve 
the left hand of an assignment into a memory address. If you understood 
that then you could explain to people that

       4 = X

made sense to work that way.

Best regards,
Andrew Mullhaupt



More information about the NumPy-Discussion mailing list