2

x in the following has the value:

[mpf('0.0') mpf('0.10000000000000001') mpf('0.20000000000000001')
 mpf('0.30000000000000004') mpf('0.40000000000000002') mpf('0.5')
 mpf('0.60000000000000009') mpf('0.70000000000000007')
 mpf('0.80000000000000004') mpf('0.90000000000000002')]

code 1

import numpy as np
import mpmath as mp
import matplotlib.pyplot as plt

x = mp.arange(0,1,0.1)
y=x
plt.plot(x,y)
plt.show()

Everything is fine

code 2

import numpy as np
import mpmath as mp
import matplotlib.pyplot as plt

x = mp.arange(0,1,0.1)
y = 2.*x

plt.plot(x,y)
plt.show()

error occurs, says: can't multiply sequence by non-int of type 'float'. So in code 3 I change 2. to 2

code 3

import numpy as np
import mpmath as mp
import matplotlib.pyplot as plt

x = mp.arange(0,1,0.1)
y = 2*x

plt.plot(x,y)
plt.show()

It says this time: x and y must have same first dimension.

Finally, I found I can use np.array to make x to be an array, all the trouble gone.

code 4

import numpy as np
import mpmath as mp
import matplotlib.pyplot as plt

x = mp.arange(0,1,0.1)
y = 2.*np.array(x)

plt.plot(x,y)
plt.show()

Can anyone explain to me, what does x represents, what is mpf. why the above codes behave like that? If x is not an numerical array, why can it be used to plot? If it is an array, why can't it multiply by a number? I am so confused!

Doon
  • 19,719
  • 3
  • 40
  • 44
an offer can't refuse
  • 4,245
  • 5
  • 30
  • 50

4 Answers4

2

Your x just a list:

>>> x = mp.arange(0, 1, 0.1)
>>> type(x)
list

That means you get the normal list behavior:

>>> x * 2.0
TypeError: can't multiply sequence by non-int of type 'float'
>>> y = [e * 2.0 for e in x]

This converts to a NumPy array of objects:

>>> np.array(x).dtype
dtype('O')

This means you can do element-wise operations:

>>> np.array(x) * 2000.0
array([mpf('0.0'), mpf('200.0'), mpf('400.0'), mpf('600.00000000000011'),
       mpf('800.0'), mpf('1000.0'), mpf('1200.0000000000002'),
       mpf('1400.0000000000002'), mpf('1600.0'), mpf('1800.0')], d

plt.plot(x,y) can use lists or NumPy arrays as input.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
1

mpf is a class in mpmath library to represent real float but it is not an int, nor float ...

in 1-2) Have you tried with y=mp.mpf(2).*x?

in 3) I haven't seen how the multiplication of int with mpf is defined, but if it works as with a string, then y in that case wouldn't have the same dimension. EDIT: in fact, 2*x gives me the extended array [x,x]

in 4) it works, because you're not using mpf numbers, but a numpy array, which has the multiplication operation defined as you wrote.

tglaria
  • 5,678
  • 2
  • 13
  • 17
0

x is an array of mp-floats; the np.array you made from it can be used in ways a normal python array cannot.

Scott Hunter
  • 48,888
  • 12
  • 60
  • 101
0

mpmath provides its own numerical types. The class mpf will be used for real float (mpc for complex float etc.). You can create mph instances from different other python types like strings, integers, floats and from other mpf instances also. The precision of a mpf instance will be set by the global mpmath working precision.

For more information you maybe visit SymPy Modules Reference

Roman Vogt
  • 3,871
  • 1
  • 8
  • 6
  • That documentation link is from ages ago when mpmath was distributed as a SymPy submodule. That's no longer the case and mpmath docs are on http://mpmath.org/doc/current/ –  Jan 02 '18 at 00:41
  • You are right, thank you. I wanted to add a reference for more information to my comment and took the first one from Google... I apologize for that. The interested reader may visit http://mpmath.org/doc/current/contexts.html#arbitrary-precision-floating-point-mp – Roman Vogt Jan 03 '18 at 12:50