8000 Support for float hex format for matrix() · Issue #18060 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

Support for float hex format for matrix() #18060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
kenorb opened this issue Dec 23, 2020 · 3 comments
Closed

Support for float hex format for matrix() #18060

kenorb opened this issue Dec 23, 2020 · 3 comments
Labels
57 - Close? Issues which may be closable unless discussion continued

Comments

@kenorb
Copy link
kenorb commented Dec 23, 2020

Similar to #5504, np.matrix should be able to interpret float hex format.

So this works fine:

>>> np.matrix("[1.1 1.2 1.3 1.4]")
matrix([[1.1, 1.2, 1.3, 1.4]])

Also the following array of hexes works fine using np.array():

>>> np.array("[%s %s %s %s]" % (1.1.hex(), 1.2.hex(), 1.3.hex(), 1.4.hex()))
array('[0x1.199999999999ap+0 0x1.3333333333333p+0 0x1.4cccccccccccdp+0 0x1.6666666666666p+0]',
      dtype='<U85')

However np.matrix() fails to interpret it:

>>> np.matrix("[%s %s %s %s]" % (1.1.hex(), 1.2.hex(), 1.3.hex(), 1.4.hex()))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/site-packages/numpy/matrixlib/defmatrix.py", line 142, in __new__
    data = _convert_from_string(data)
  File "/usr/local/lib/python3.9/site-packages/numpy/matrixlib/defmatrix.py", line 26, in _convert_from_string
    newrow.extend(map(ast.literal_eval, temp))
  File "/usr/local/Cellar/python@3.9/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 62, in literal_eval
    node_or_string = parse(node_or_string, mode='eval')
  File "/usr/local/Cellar/python@3.9/3.9.1_1/Frameworks/Python.framework/Versions/3.9/lib/python3.9/ast.py", line 50, in parse
    return compile(source, filename, mode, flags,
  File "<unknown>", line 1
    0x1.199999999999ap+0
       ^
SyntaxError: invalid syntax

My version:

>>> np.__version__
'1.19.4'
@eric-wieser
Copy link
Member
eric-wieser commented Dec 23, 2020

It's worth noting that 0x1.199999999999ap+0 is not valid python syntax, and raises SyntaxError. The output of float.hex is only usable with float.fromhex, and not eval which matrix is using under the hood. If you feel strongly about this, you could ask on python-ideas about making it legal syntax.

Also the following array of hexes works fine using np.array():

That's not creating an array at all, that's just a single string inside a 0-d Array, hence the dtype="U..."

However np.matrix() fails to interpret it:

np.matrix is deprecated, I'd recommend not using it.

@eric-wieser eric-wieser added the 57 - Close? Issues which may be closable unless discussion continued label Dec 23, 2020
@kenorb
Copy link
Author
kenorb commented Dec 23, 2020

np.matrix is deprecated, I'd recommend not using it.

np.matrix is the only method that I've found (apart of json and similar modules) that can interpret multidimensional arrays (higher than 1D) from the string correctly, where loads()/loadtxt()/fromstring() and other fails. Related: How to read numpy 2D array from string?.

My case is having file with arrays in the text file which needs to be loaded (I'm aware of .npy format). So wrapping np.array() around np.matrix() works fine, e.g.

>>> np.array(np.matrix("[1.1 1.2 1.3 1.4]"))
array([[1.1, 1.2, 1.3, 1.4]])

However I'd like to load arrays from the text file which I've written in the hex format (dumped with "formatter": {"float": lambda x: float(x).hex()}) such as this one:

>>> np.array(np.matrix("[0x1.3956d40000000p-3 -0x1.81a7620000000p-5 0x1.c5db060000000p-4 0x1.5473460000000p-6 0x1.385f480000000p-3]"))

but this generates the SyntaxError.

@rossbar
Copy link
Contributor
rossbar commented Aug 4, 2021

I agree with @eric-wieser here, especially since the hex float repr isn't valid Python syntax: as pointed out above, this is not something that is works with np.array either.

@rossbar rossbar closed this as completed Aug 4, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
57 - Close? Issues which may be closable unless discussion continued
Projects
None yet
Development

No branches or pull requests

3 participants
0