TAGGED: python, python-ironpython, spaceclaim
-
-
November 25, 2021 at 9:43 pm
FirasBejar
SubscriberI'm trying to do a simple automation that requires some math with numpy.
By writing import numpy I get the message "No module named numpy.
what should I do?
November 26, 2021 at 8:27 amVigneswaran Sridharan
Ansys EmployeeHi The normal NumPy and Scipy packages for CPthyon will not work with the version of IronPython that we ship with Ansys. NumPy on its own is incompatible with IronPython due to unsupported module dependencies. For example, Python includes the signal module while IronPython does not. NumPy requires the signal module to build and install as a package; IronPython's lack of this and other modules makes it impossible to import.
You can use Math.net instead of Numpy or SciPy. Math.net is supported for IronPython and available for ACT.ÔÇï Libraries are in ANSYS Installation directory : ÔÇïC:\Program Files\ANSYS Inc\v182\Addins\ACT\bin\Win64\MathNet.Numerics.dll
Here is an example
----------------------------------------------
ÔÇïÔÇïimport clr
import System
import os
clr.AddReferenceToFileAndPath("C:\\Program Files\\ANSYS Inc\\v182\\Addins\\ACT\\bin\\Win64\\MathNet.Numerics.dll")
# Example 1
import MathNet
from MathNet.Numerics.LinearAlgebra import *
V=Vector[System.Double].Build
M=Matrix[System.Double].Build
m = M.Random(3,4)
v = V.Random(3)
r=v*m
print r
Vigneswaran
November 26, 2021 at 8:27 amVigneswaran Sridharan
Ansys EmployeeHi The normal NumPy and Scipy packages for CPthyon will not work with the version of IronPython that we ship with Ansys. NumPy on its own is incompatible with IronPython due to unsupported module dependencies. For example, Python includes the signal module while IronPython does not. NumPy requires the signal module to build and install as a package; IronPython's lack of this and other modules makes it impossible to import.
You can use Math.net instead of Numpy or SciPy. Math.net is supported for IronPython and available for ACT.ÔÇï Libraries are in ANSYS Installation directory : ÔÇïC:\Program Files\ANSYS Inc\v182\Addins\ACT\bin\Win64\MathNet.Numerics.dll
Here is an example
----------------------------------------------
ÔÇïÔÇïimport clr
import System
import os
clr.AddReferenceToFileAndPath("C:\\Program Files\\ANSYS Inc\\v182\\Addins\\ACT\\bin\\Win64\\MathNet.Numerics.dll")
# Example 1
import MathNet
from MathNet.Numerics.LinearAlgebra import *
V=Vector[System.Double].Build
M=Matrix[System.Double].Build
m = M.Random(3,4)
v = V.Random(3)
r=v*m
print r
Vigneswaran
November 26, 2021 at 8:27 amVigneswaran Sridharan
Ansys EmployeeHi The normal NumPy and Scipy packages for CPthyon will not work with the version of IronPython that we ship with Ansys. NumPy on its own is incompatible with IronPython due to unsupported module dependencies. For example, Python includes the signal module while IronPython does not. NumPy requires the signal module to build and install as a package; IronPython's lack of this and other modules makes it impossible to import.
You can use Math.net instead of Numpy or SciPy. Math.net is supported for IronPython and available for ACT.ÔÇï Libraries are in ANSYS Installation directory : ÔÇïC:\Program Files\ANSYS Inc\v182\Addins\ACT\bin\Win64\MathNet.Numerics.dll
Here is an example
----------------------------------------------
ÔÇïÔÇïimport clr
import System
import os
clr.AddReferenceToFileAndPath("C:\\Program Files\\ANSYS Inc\\v182\\Addins\\ACT\\bin\\Win64\\MathNet.Numerics.dll")
# Example 1
import MathNet
from MathNet.Numerics.LinearAlgebra import *
V=Vector[System.Double].Build
M=Matrix[System.Double].Build
m = M.Random(3,4)
v = V.Random(3)
r=v*m
print r
Vigneswaran
November 26, 2021 at 8:27 amVigneswaran Sridharan
Ansys EmployeeHi The normal NumPy and Scipy packages for CPthyon will not work with the version of IronPython that we ship with Ansys. NumPy on its own is incompatible with IronPython due to unsupported module dependencies. For example, Python includes the signal module while IronPython does not. NumPy requires the signal module to build and install as a package; IronPython's lack of this and other modules makes it impossible to import.
You can use Math.net instead of Numpy or SciPy. Math.net is supported for IronPython and available for ACT.ÔÇï Libraries are in ANSYS Installation directory : ÔÇïC:\Program Files\ANSYS Inc\v182\Addins\ACT\bin\Win64\MathNet.Numerics.dll
Here is an example
----------------------------------------------
ÔÇïÔÇïimport clr
import System
import os
clr.AddReferenceToFileAndPath("C:\\Program Files\\ANSYS Inc\\v182\\Addins\\ACT\\bin\\Win64\\MathNet.Numerics.dll")
# Example 1
import MathNet
from MathNet.Numerics.LinearAlgebra import *
V=Vector[System.Double].Build
M=Matrix[System.Double].Build
m = M.Random(3,4)
v = V.Random(3)
r=v*m
print r
Vigneswaran
November 26, 2021 at 10:03 amFirasBejar
SubscriberHi Thank you for the reply.
Is this a python code? I think it is written slightly different from python.
Also when executing I get the following error:
----------------
# Python Script, API Version = V20ÔÇï
unexpected token 'ÔÇï'
----------------
ThanksNovember 26, 2021 at 12:28 pmErik Kostson
Ansys EmployeeOk is it he clr.AddRef. that is the issue - you are also using V20 so try this instead:
(I have changed the version to v212 (see path below and change as needed) which has the API Version V20.
(Also you might need to try this a couple of times in SC and new script - I had to close SC and open again to get it to work for some reason - the main thing is that the code below works)
---
import os
import System
import clr
clr.AddReferenceToFile("C:/Program Files/ANSYS Inc/v212/Addins/ACT/bin/Win64/MathNet.Numerics.dll")
import MathNet
from MathNet.Numerics.LinearAlgebra import *
V=Vector[System.Double].Build
M=Matrix[System.Double].Build
m = M.Random(3,4)
v = V.Random(3)
r=v*m
print r
---
I would type these in instead of copy pasting
All the best
Erik
November 30, 2021 at 1:08 amFirasBejar
SubscriberHello Thank you for you answer the script works perfectly.
I've noticed that you used M and V to build a matrix and vector. Since am not familiar with ironpython, I was wondering of there is a way to generate a random number in a range for both integers and floats.
The equivalent in Cpython would be:
import Random
a = random.randint(minInt, maxInt) #generate random integer for a
b = random.uniform(minFloat, maxFloat) #generate random float for b
Thanks
November 30, 2021 at 11:04 amErik Kostson
Ansys EmployeeNot sure - I would have written a c++ function (a bit old fashioned but it has a nice random generator) to generate the random numbers and use that compiled .dll in ironpython via the ctype module, but that is undocumented and unsupported so can not help.
The above might be a bit overkill if you need something simple and that exists in Ironpython.
Not sure what you are doing exactly but if you just want random numbers there is a module in ironpython called random that will do that like this:
import random
randomnr=random.randrange(1,10) # generates a random int number between 1 and 10, or
randomnr_two=random.randint(1,10) # generates a random int number between 1 and 10
randomnr_three=random.uniform(1.00,10.00)# generates a random float number between 1 and 10
All the best
Erik
Viewing 8 reply threads- The topic ‘How to import external library such as Numpy or Scipy to spaceclaim?’ is closed to new replies.
Ansys Innovation SpaceTrending discussionsTop Contributors-
3467
-
1057
-
1051
-
929
-
896
Top Rated Tags© 2025 Copyright ANSYS, Inc. All rights reserved.
Ansys does not support the usage of unauthorized Ansys software. Please visit www.ansys.com to obtain an official distribution.
-