How to import external library such as Numpy or Scipy to spaceclaim?

FirasBejarFirasBejar Member Posts: 3

I'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?

Best Answers

  • ekostsonekostson Posts: 888Ansys Employee
    edited November 26 Accepted Answer

    Ok 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

  • ekostsonekostson Posts: 888Ansys Employee
    edited November 30 Accepted Answer

    @FirasBejar

    Not 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

Answers

  • visridhavisridha Posts: 79Ansys Employee

    Hi,

    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

  • visridhavisridha Posts: 79Ansys Employee

    Hi,

    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

  • visridhavisridha Posts: 79Ansys Employee

    Hi,

    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

  • visridhavisridha Posts: 79Ansys Employee

    Hi,

    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

  • FirasBejarFirasBejar Posts: 5Member

    Hi,

    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 '​'

    ----------------

    Thanks,

  • FirasBejarFirasBejar Posts: 5Member

    Hello,

    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,

Sign In or Register to comment.