How to include an equation model in python code

Multi tool use
So um I wanted to know how to include an equational model in python such that I put in the values and get the output according to the equation . Like for example if my model was x^2 + y^2 = output. How do I incorporate this model into my code , so that when I give values for x and y I get the output .
python
add a comment |
So um I wanted to know how to include an equational model in python such that I put in the values and get the output according to the equation . Like for example if my model was x^2 + y^2 = output. How do I incorporate this model into my code , so that when I give values for x and y I get the output .
python
1
Write it as a function.
– Paritosh Singh
Dec 28 '18 at 21:36
Look at function definitions in Python
– Daniel Mesejo
Dec 28 '18 at 21:37
link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math
– 123abc321bca
Dec 28 '18 at 21:41
In python^
is treated as XOR so you might want to look for operators in Python as well. You can use**
.
– Rishabh Mishra
Dec 28 '18 at 21:42
add a comment |
So um I wanted to know how to include an equational model in python such that I put in the values and get the output according to the equation . Like for example if my model was x^2 + y^2 = output. How do I incorporate this model into my code , so that when I give values for x and y I get the output .
python
So um I wanted to know how to include an equational model in python such that I put in the values and get the output according to the equation . Like for example if my model was x^2 + y^2 = output. How do I incorporate this model into my code , so that when I give values for x and y I get the output .
python
python
asked Dec 28 '18 at 21:35
reliance69reliance69
31
31
1
Write it as a function.
– Paritosh Singh
Dec 28 '18 at 21:36
Look at function definitions in Python
– Daniel Mesejo
Dec 28 '18 at 21:37
link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math
– 123abc321bca
Dec 28 '18 at 21:41
In python^
is treated as XOR so you might want to look for operators in Python as well. You can use**
.
– Rishabh Mishra
Dec 28 '18 at 21:42
add a comment |
1
Write it as a function.
– Paritosh Singh
Dec 28 '18 at 21:36
Look at function definitions in Python
– Daniel Mesejo
Dec 28 '18 at 21:37
link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math
– 123abc321bca
Dec 28 '18 at 21:41
In python^
is treated as XOR so you might want to look for operators in Python as well. You can use**
.
– Rishabh Mishra
Dec 28 '18 at 21:42
1
1
Write it as a function.
– Paritosh Singh
Dec 28 '18 at 21:36
Write it as a function.
– Paritosh Singh
Dec 28 '18 at 21:36
Look at function definitions in Python
– Daniel Mesejo
Dec 28 '18 at 21:37
Look at function definitions in Python
– Daniel Mesejo
Dec 28 '18 at 21:37
link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math
– 123abc321bca
Dec 28 '18 at 21:41
link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math
– 123abc321bca
Dec 28 '18 at 21:41
In python
^
is treated as XOR so you might want to look for operators in Python as well. You can use **
.– Rishabh Mishra
Dec 28 '18 at 21:42
In python
^
is treated as XOR so you might want to look for operators in Python as well. You can use **
.– Rishabh Mishra
Dec 28 '18 at 21:42
add a comment |
3 Answers
3
active
oldest
votes
You should use sympy for cleaner calculations
from sympy import *
x = Symbol('x') # define first symbol
y = Symbol('y') # define second symbol
output = x**2 + y**2 # form the equation
print(output) # print the equation on console
Output
x**2 + y**2
Now substitute the values of x and y like we do in any math equation
output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result
Output
2 # 1**2 ==1 and 1**2==1 and 1+1 =2
For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations
def func(x,y): return x**2 + y**2
Now you can use the function to get the output
func(1,1) #2
add a comment |
A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp
def my_equation(x, y):
return x**2 + y**2
oh yes should have done that. thanks a lot !!
– reliance69
Dec 29 '18 at 3:41
add a comment |
There are two ways to do this,
- @paritosh-singh said, write it as a function
- use a library that allows you to do symbolic calculations, such as sympy
with sympy, you can do this as
from sympy import *
x, y, z= symbols('x y z')
z = (x^2)+(y^2)
you can now assign values to x and y and get output as z.
sympy documentation
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53964514%2fhow-to-include-an-equation-model-in-python-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should use sympy for cleaner calculations
from sympy import *
x = Symbol('x') # define first symbol
y = Symbol('y') # define second symbol
output = x**2 + y**2 # form the equation
print(output) # print the equation on console
Output
x**2 + y**2
Now substitute the values of x and y like we do in any math equation
output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result
Output
2 # 1**2 ==1 and 1**2==1 and 1+1 =2
For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations
def func(x,y): return x**2 + y**2
Now you can use the function to get the output
func(1,1) #2
add a comment |
You should use sympy for cleaner calculations
from sympy import *
x = Symbol('x') # define first symbol
y = Symbol('y') # define second symbol
output = x**2 + y**2 # form the equation
print(output) # print the equation on console
Output
x**2 + y**2
Now substitute the values of x and y like we do in any math equation
output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result
Output
2 # 1**2 ==1 and 1**2==1 and 1+1 =2
For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations
def func(x,y): return x**2 + y**2
Now you can use the function to get the output
func(1,1) #2
add a comment |
You should use sympy for cleaner calculations
from sympy import *
x = Symbol('x') # define first symbol
y = Symbol('y') # define second symbol
output = x**2 + y**2 # form the equation
print(output) # print the equation on console
Output
x**2 + y**2
Now substitute the values of x and y like we do in any math equation
output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result
Output
2 # 1**2 ==1 and 1**2==1 and 1+1 =2
For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations
def func(x,y): return x**2 + y**2
Now you can use the function to get the output
func(1,1) #2
You should use sympy for cleaner calculations
from sympy import *
x = Symbol('x') # define first symbol
y = Symbol('y') # define second symbol
output = x**2 + y**2 # form the equation
print(output) # print the equation on console
Output
x**2 + y**2
Now substitute the values of x and y like we do in any math equation
output.subs({x:1,y:1}) #substitue x::1 and y::1 to get the result
Output
2 # 1**2 ==1 and 1**2==1 and 1+1 =2
For completeness, you can define the equation inside the function as well but it will be less descriptive for complex equations
def func(x,y): return x**2 + y**2
Now you can use the function to get the output
func(1,1) #2
edited Dec 28 '18 at 21:59
answered Dec 28 '18 at 21:51
mad_mad_
3,89011021
3,89011021
add a comment |
add a comment |
A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp
def my_equation(x, y):
return x**2 + y**2
oh yes should have done that. thanks a lot !!
– reliance69
Dec 29 '18 at 3:41
add a comment |
A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp
def my_equation(x, y):
return x**2 + y**2
oh yes should have done that. thanks a lot !!
– reliance69
Dec 29 '18 at 3:41
add a comment |
A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp
def my_equation(x, y):
return x**2 + y**2
A function can receive one or more inputs, and return a value: https://www.w3schools.com/python/python_functions.asp
def my_equation(x, y):
return x**2 + y**2
answered Dec 28 '18 at 21:40
user8181134user8181134
32614
32614
oh yes should have done that. thanks a lot !!
– reliance69
Dec 29 '18 at 3:41
add a comment |
oh yes should have done that. thanks a lot !!
– reliance69
Dec 29 '18 at 3:41
oh yes should have done that. thanks a lot !!
– reliance69
Dec 29 '18 at 3:41
oh yes should have done that. thanks a lot !!
– reliance69
Dec 29 '18 at 3:41
add a comment |
There are two ways to do this,
- @paritosh-singh said, write it as a function
- use a library that allows you to do symbolic calculations, such as sympy
with sympy, you can do this as
from sympy import *
x, y, z= symbols('x y z')
z = (x^2)+(y^2)
you can now assign values to x and y and get output as z.
sympy documentation
add a comment |
There are two ways to do this,
- @paritosh-singh said, write it as a function
- use a library that allows you to do symbolic calculations, such as sympy
with sympy, you can do this as
from sympy import *
x, y, z= symbols('x y z')
z = (x^2)+(y^2)
you can now assign values to x and y and get output as z.
sympy documentation
add a comment |
There are two ways to do this,
- @paritosh-singh said, write it as a function
- use a library that allows you to do symbolic calculations, such as sympy
with sympy, you can do this as
from sympy import *
x, y, z= symbols('x y z')
z = (x^2)+(y^2)
you can now assign values to x and y and get output as z.
sympy documentation
There are two ways to do this,
- @paritosh-singh said, write it as a function
- use a library that allows you to do symbolic calculations, such as sympy
with sympy, you can do this as
from sympy import *
x, y, z= symbols('x y z')
z = (x^2)+(y^2)
you can now assign values to x and y and get output as z.
sympy documentation
answered Dec 28 '18 at 21:42
AuxilusAuxilus
20719
20719
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53964514%2fhow-to-include-an-equation-model-in-python-code%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
QoVBOGEaD3eKO,0Zg6vOUtMu,c,W5bYl3lz,QYqeWX,SEGrC6pTk lO PzHTEKN 61Y9
1
Write it as a function.
– Paritosh Singh
Dec 28 '18 at 21:36
Look at function definitions in Python
– Daniel Mesejo
Dec 28 '18 at 21:37
link to python math operations : en.wikibooks.org/wiki/Python_Programming/Basic_Math
– 123abc321bca
Dec 28 '18 at 21:41
In python
^
is treated as XOR so you might want to look for operators in Python as well. You can use**
.– Rishabh Mishra
Dec 28 '18 at 21:42