6

I am looking to perform element-wise mpmath operations on Python arrays. For example,

import mpmath as mpm
x = mpm.arange(0,4)
y = mpm.sin(x)        # error

Alternatively, using mpmath matrices

x = mpm.matrix([0,1,2,3])
y = mpm.sin(x)             # error

Does mpmath have any capibilities in this area, or is it necessary to loop through the entries?

John
  • 13,197
  • 7
  • 51
  • 101
Doubt
  • 1,163
  • 2
  • 15
  • 26

3 Answers3

9

mpmath does not appear to handle element-wise operation, but you can use numpy to get this functionality:

import numpy as np
import mpmath as mpm
x = np.array(mpm.arange(0,4))

sin = np.vectorize(mpm.sin)
y = sin(x)
Stefan Gruenwald
  • 2,582
  • 24
  • 30
DrRobotNinja
  • 1,381
  • 12
  • 14
3

mpmath.arange apparently returns regular Python lists, so you can use map to apply a function on each element:

import mpmath
x = mpmath.arange(0,4)
y = map(mpmath.sin, x)
Lynn
  • 10,425
  • 43
  • 75
-1

The apply method simply should work

A = mpmath.arange(0,4)

A.apply(sin)
arulmr
  • 8,620
  • 9
  • 54
  • 69
nasir
  • 1