As example, my first module is
from sympy import *
x,y=symbols('x y')
def A():
equation=2*x+y
return equation
print(A())
the output is
2*x + y
my second module is
def B(x,y):
equation=2*x + y
return equation
I have to copy past the output of first module to second one each time from the terminal. Is there a way to pass this output automatically to the source code of B(x,y)?
Thank you for your attention
Related
For example, in my folder, I have my ipython notebook "program.ipynb" and a python file "functions.py" which has some functions in it, for example, "func"
from numpy import sqrt
def func(x):
return N + sqrt(x)
that is going to be used in "program.ipynb" which looks like that
from functions import func
N = 5
func(2)
--> name 'N' is not defined
To fix the bug i need to define the variable N in my functions.py file but isn't there a way around? I want to define all my global variables in my main programm (program.ipynb).
You can't access a variable like that, the best way would be:
functions.py
from numpy import sqrt
def func(x, N):
return N + sqrt(x)
program.ipynb
from functions import func
N = 5
func(2, N)
I'm working with Python Sympy, solving a quadratic, and wanting to print the result using LaTex. For example, if the result is x = (1 + sqrt(3))/2, I would like it to print via LaTex as \frac{1 + \sqrt{3}}{2}. However, Python Sympy either splits this into two fractions, as in \frac{1}{2} + \frac{\sqrt{3}}{2}, OR factors out the half, as in \frac{1}{2}(1 + \sqrt{3}). I have tried to return the numerator via sympy.fraction(expr) and have viewed other articles (Sympy - fraction manipulation and others), but none were able to produce the result.
Check out how to override the default printers.
import sympy
from sympy.printing.latex import LatexPrinter # necessary because latex is both a function and a module
class CustomLatexPrinter(LatexPrinter):
def _print_Add(self, expr):
n, d = expr.as_numer_denom()
if d == sympy.S.One:
# defer to the default printing mechanism
super()._print_Add(expr)
return
return r'\frac{%s}{%s}' % (sympy.latex(n), sympy.latex(d))
# doing this should override the default latex printer globally
# adopted from "Examples of overloading StrPrinter" in the sympy documentation
sympy.printing.latex = lambda self: CustomLatexPrinter().doprint(self)
print(sympy.printing.latex((1 + sympy.sqrt(3)) / 2)) # \frac{1 + \sqrt{3}}{2}
** EDIT: Copy-pasting my actual file to ease confusion. The code snippet below is in a file named train_fm.py:
def eval_fm(x,b,w,V):
# evaluate a degree 2 FM. x is p X B
# V is p x k
# some python code that computes yhat
return(yhat);
Now in my main file: I say the following
from train_fm import eval_fm
and I get the error:
ImportError: cannot import name f1
When I type
from train_fm import train_fm
I do not get an error.
OLD QUESTION BELOW :
def train_fm(x,y,lb,lw,lv,k,a,b,w,V):
# some code
yhat = eval_fm(x,b,w,V);
# OUTPUTS
return(b,w,V);
I have a file called f2.py, where I define 2 functions (note that one of the functions has the same name as the file)
def f1():
some stuff;
return(stuff)
def f2():
more stuff;
y = f1();
return(y)
In my main file, I do
from aaa import f1
from aaa import f2
but when I run the first of the 2 commands above, I get
ImportError: cannot import name f1
Any idea what is causing this? The second function gets imported fine.
In #PSP_soil.py:
def evaporation_flux(psi):
h_s = exp(mw*psi/(R*T))
return(E_p*(h_s-h_a)/(1-h_a))
I want to change this function to:
def evaporation_flux(psi):
h_s = exp(mw*psi/(R*T))
return(h_s)
but console in spyder (Python 2.7) do not run the program (E_p and h_a are constant variables), and just show UMD has deleted: PSP_readDataFile, PSP_grid, PSP_ThomasAlgorithm, PSP_soil
Any advice in this case?
You can do this:
from PSP_soil import *
def evaporation_flux(psi):
h_s = exp(mw*psi/(R*T))
return(h_s)
This redefines evaporation_flux from PSP_soil so when you do evaporation_flux(value), it gets called.
from PSP_soil import * imports all constants you need for this function, but you can also do from PSP_soil import evaporation_flux, mv, R, T
I would like to integrate the function e^(-x**2/2) by simpson rule
but it is keep having an error and I don't know what is the problem.
a=eval(input('a:'))
b=eval(input('b:'))
n=eval(input('n:'))
def f(x):
e**(-x**2/2)
h=(b-a)/n
s= f(a)+f(b)
def simpson_rule(f(x),a,b,n):
#Approximation by Simpson's rule
c=(a+b)/2.0
h=abs(b-a)/2.0
return h*(f(a)+4.0*f(c)+f(b))/3.0
def simpson_rule(f(x),a,b,n):
"""Approximates the definite integral of f from a to b by the composite Simpson's rule, using n subintervals"""
for i in range (1,n,2):
s+=4*f(a+i*h)
for i in range(2,n-1,2):
s+=2*f(a+i*h)
return s*h/3
print simpson_rule(f(x),a,b,n)
You define 2 integration routines with the same name. Which one do you expect to run if you call simpson_rule()?
The first one is the special case where n=1. You might rename it accordingly.
Second, your call is print simpson_rule(f(x),a,b,n) but you only need to hand over f() to the function, like this print simpson_rule(f,a,b,n).
You can see that f is a function, and f() is a function return value:
def f(x):
return x + 13
f
<function f at 0x0000000002253D68>
f(5)
18
Try it out and if you still have errors please post the error message(s).