SymPy in Jython - python

I'm making a calculator app. I am using Java and Jython. I wanted to use the SymPy module but after calling the code below I get an error that there is no such module. Any advice?
try (PythonInterpreter interpreter = new PythonInterpreter()) {
interpreter.exec("""
from sympy import cos, sin, pi
def f(x):
\treturn\040""" + wartoscCalki.getText() +
"""
\ndef simpson(a, b, n):
\tx1=0.0
\tx2=0.0
\tx0=f(a)+f(b)
\th=(b-a)/n
\twynik=0.0
\tfor i in range(1,n):
\t\tx=(a+(i*h))
\t\tif i%2==0:
\t\t\tx2+=f(x)
\t\telse:
\t\t\tx1+=f(x)
\twynik=h*(x0+2*x2+4*x1)/3
\treturn wynik
c=float(simpson(""" + granicaDolna.getText() + """
,""" + granicaGorna.getText() + """
,""" + liczbaPodprzedzialow.getText() + """
))"""
);
}
Caused by: Traceback (most recent call last):
File "", line 2, in module
ImportError: No module named sympy

sympy is not a jython module. What you need is this:
from Java.lang import Math
Once imported, you can use Math.sin(), Math.cos(), and Math.PI to perform the above listed calculations.
A complete list of Java.lang.Math methods can be found here:
Oracle Documentation

Related

Python call a imported function from a different .py file, but the imported function cannot see the other imported moduel in main file

I have a function defined in a different file nhpp_next_arrival.py, which contains a function. The important thing to notice is that I am using numpy package.
def nhpp_next_arrival(t,Lambda, h_lam):
# current starting time t
# rate handle h_lam
# Lambda a majorizing function/constant
U = np.random.uniform()
V = np.random.uniform()
t = t - np.log(U)/Lambda
while V > h_lam(t)/Lambda:
t = t - np.log(U)/Lambda
U = np.random.uniform()
V = np.random.uniform()
return t
I imported this function in a different file as the following
import numpy as np
from nhpp import *
#Call nhpp_next_arrival
t_arrival = nhpp_next_arrival(t=t, Lambda=max_arrival, h_lam=h_arr_total)
Then I got the following error message.
U = np.random.uniform() NameError: name 'np' is not defined
Thank you!
You might be confused with a C #include (or something similar).
Your code is using numpy.random at nhpp_next_arrival.py, so you should have at its top
import numpy as np
Even though you imported it before the import to this file, when the interpreter sees
from nhpp import *
it doesn't import the packages into the namespace of that module.

Cannot import mean from statistics

I'm trying to import statistics module in python. It's giving me an error message when i execute the program.
Here is my code:
from statistics import mean
import numpy as np
import matplotlib.pyplot as plt
xs = np.array([1,2,3,4,5,6,7,8])
ys = np.array([2,8,5,0,5,7,3,6])
def best_fit_line(xs ,ys):
m = ( ((mean(xs)* mean(ys))- mean(xs*ys)) /
(mean(xs)*mean(xs))-(mean(xs*xs)))
return m
m = best_fit_line(xs,ys)
The error message:
Traceback (most recent call last):
File "/home/kudzai/Documents/Machine Learning/LinearRegAlg.py", line 1, in <module>
from statistics import mean
ImportError: No module named statistics
The statistics module was added in Python 3.4. Perhaps you're using an older Python version.
If you can't upgrade for whatever reason, you can also just use numpy's mean function: np.mean(xs) etc. For numpy arrays, it's probably faster too.

math built-in functions 'log'

I want to work with logarithms, but my math module doesn't work like it is supposed to.
Here's my code:
import math
n= int(2)
x = n**2
y = 2*n** log(3,2) +1
while float(x) < float(y):
n += 1
print(n)
It prints the following Error:
Traceback (most recent call last):
File "C:\Users\User\Desktop\python\exp102.py", line 72, in <module>
y = 2*n** log(3,2) +1
NameError: name 'log' is not defined
Can you help me put the math module work perfectly, if that's the problem?
If you want to use a function from a package, you have several options to do that. The two most use:
from math import log
This will make log available in the namespace and you can use it directly.
import math
This will make math available and you can access the function as math.log().
Try to import log module with below statement:
from math import log.

Issue with module changes in python

I am a newbie to python. Below is my module
mymath.py
pi = 3.142
def circle(radius):
return pi * radius * radius
In terminal, I run it following way:
>>import mymath
>>mymath.pi
>>3.142
When I change pi to a local variable and reload(mymath) and do import mymath, still I get value of mymath.pi as 3.142. However the result of mymath.circle(radius) does reflect the change in result.
def circle(radius):
pi = 3
return pi * radius * radius
>>import imp
>>imp.reload(mymath)
>>import mymath
>>mymath.pi
>>3.142
>>circle(3)
>>27
Can anyone tell me what might be the issue?
From the docs for imp.reload():
When a module is reloaded, its dictionary (containing the module’s global variables) is retained. Redefinitions of names will override the old definitions, so this is generally not a problem. If the new version of a module does not define a name that was defined by the old version, the old definition remains.
So when you do imp.reload(mymath), even though pi no longer exists as a global name in the module's code the old definition remains as a part of the updated module.
If you really want to start from scratch, use the following method:
import sys
del sys.modules['mymath']
import mymath
For example:
>>> import os
>>> os.system("echo 'pi = 3.142' > mymath.py")
0
>>> import mymath
>>> mymath.pi
3.142
>>> os.system("echo 'pass' > mymath.py")
0
>>> import sys
>>> del sys.modules['mymath']
>>> import mymath
>>> mymath.pi
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'pi'

Using Python Ctypes for ssdeep's fuzzy.dll but receive error

I am trying to use Python and ctypes to use the fuzzy.dll from ssdeep. So far everything I have tried fails with an access violation error. Here is what I do after changing to the proper directory which contains the fuzzy.dll and fuzzy.def files:
>>> import os,sys
>>> from ctypes import *
>>> fn = create_string_buffer(os.path.abspath("fuzzy.def"))
>>> fuzz = windll.fuzzy
>>> chash = c_char_p(512)
>>> hstat = fuzz.fuzzy_hash_filename(fn,chash)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
WindowsError: exception: access violation writing 0x00000200
>>>
From what I understand, I have passed the proper c_types. From fuzzy.h:
extern int fuzzy_hash_filename(char * filename, char * result)
I just cannot get past that access violation.
There are two problems with your code:
You should not use windll.fuzzy, but cdll.fuzzy -- from ctypes documentation:
cdll loads libraries which export functions using the standard cdecl calling convention, while windll libraries call functions using the stdcall calling convention.
For return value (chash), you should declare a buffer rather than creating a pointer to 0x0000200 (=512) -- this is where the access violation comes from. Use create_string_buffer('\000' * 512) instead.
So your example should look like this:
>>> import os, sys
>>> from ctypes import *
>>> fn = create_string_buffer(os.path.abspath("fuzzy.def"))
>>> fuzz = cdll.fuzzy
>>> chash = create_string_buffer('\000' * 512)
>>> hstat = fuzz.fuzzy_hash_filename(fn,chash)
>>> print hstat
0 # == success

Categories

Resources