Re: GUI user input to function

2017-12-29 Thread Nico Vogeli
Am Donnerstag, 28. Dezember 2017 14:00:14 UTC+1 schrieb Chris Angelico:
> On Thu, Dec 28, 2017 at 11:06 PM, Nico Vogeli  wrote:
> > Am Donnerstag, 28. Dezember 2017 12:59:24 UTC+1 schrieb Chris Angelico:
> >> On Thu, Dec 28, 2017 at 8:38 PM, Nico Vogeli  wrote:
> >> > Withs test, this return a correct value for the two x functions:
> >> >
> >> > from sympy import symbols
> >> >
> >> > x = symbols('x')
> >> > f1 = eval(input('function 1 '))
> >> > f2 = eval(input('function 2 '))
> >> >
> >>
> >> What are you typing as input? It's hard to grok your code without knowing 
> >> that.
> >>
> >> ChrisA
> >
> > I'm sorry! User input would look like this for example: x**2 + 3*x or x**3
> >
> 
> Cool. That's an expression, but it isn't a function. To make that into
> a function, you need to prefix it with the lambda keyword.
> 
> So you should be able to construct functions like this:
> 
> f1 = eval("lambda x: " + input("function 1: "))
> 
> Then, when you type "x**3", Python evaluates "lambda x: x**3", which
> is a function.
> 
> ChrisA

Thank you very much! It now works perfectly fine now!!

Thank you!

Nicco
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GUI user input to function

2017-12-28 Thread Chris Angelico
On Thu, Dec 28, 2017 at 11:06 PM, Nico Vogeli  wrote:
> Am Donnerstag, 28. Dezember 2017 12:59:24 UTC+1 schrieb Chris Angelico:
>> On Thu, Dec 28, 2017 at 8:38 PM, Nico Vogeli  wrote:
>> > Withs test, this return a correct value for the two x functions:
>> >
>> > from sympy import symbols
>> >
>> > x = symbols('x')
>> > f1 = eval(input('function 1 '))
>> > f2 = eval(input('function 2 '))
>> >
>>
>> What are you typing as input? It's hard to grok your code without knowing 
>> that.
>>
>> ChrisA
>
> I'm sorry! User input would look like this for example: x**2 + 3*x or x**3
>

Cool. That's an expression, but it isn't a function. To make that into
a function, you need to prefix it with the lambda keyword.

So you should be able to construct functions like this:

f1 = eval("lambda x: " + input("function 1: "))

Then, when you type "x**3", Python evaluates "lambda x: x**3", which
is a function.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GUI user input to function

2017-12-28 Thread Nico Vogeli
Am Donnerstag, 28. Dezember 2017 12:59:24 UTC+1 schrieb Chris Angelico:
> On Thu, Dec 28, 2017 at 8:38 PM, Nico Vogeli  wrote:
> > Withs test, this return a correct value for the two x functions:
> >
> > from sympy import symbols
> >
> > x = symbols('x')
> > f1 = eval(input('function 1 '))
> > f2 = eval(input('function 2 '))
> >
> 
> What are you typing as input? It's hard to grok your code without knowing 
> that.
> 
> ChrisA

I'm sorry! User input would look like this for example: x**2 + 3*x or x**3

Regards

Nicco
-- 
https://mail.python.org/mailman/listinfo/python-list


Re: GUI user input to function

2017-12-28 Thread Chris Angelico
On Thu, Dec 28, 2017 at 8:38 PM, Nico Vogeli  wrote:
> Withs test, this return a correct value for the two x functions:
>
> from sympy import symbols
>
> x = symbols('x')
> f1 = eval(input('function 1 '))
> f2 = eval(input('function 2 '))
>

What are you typing as input? It's hard to grok your code without knowing that.

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


GUI user input to function

2017-12-28 Thread Nico Vogeli
Hello again

I think my question got lost in all the others raised, so here I am again :P



So, I tried my best, was looking into the eval and exec function, but still no 
succsess..

Maybe it helps when I provide the code I want to implement:

def newton_verfahren(self):
if self.cB_1.currentText() == 'Newton':
   
x0 = self.newton_x.text()
f1 = self.func_1.text()
f2 = self.func_2.text()
tol = self.newton_tol.text()
n = self.newton_n.text()
k = Numerik_Funktionen.newton(int(x0), eval(f1), eval(f2),
  int(tol), int(n))
k1 = str(k[0])
k2 = str(k[1])
k3 = str(k[2])
self.ausgabe.setText('Startwert -> x0: '+ k1 + "\n" +
  'Anzahl Iterationen: ' + k2 +"\n" +
  'f(x~): ' + k3)  

Notice that cB_1 is a combobox, newton_x, func_1, func_2, newton_tol and 
newton_n are all QLineEdits.

The code above doesn't work, so I've opend a 'test' file to just play around 
with the functions.

Withs test, this return a correct value for the two x functions:

from sympy import symbols

x = symbols('x')
f1 = eval(input('function 1 '))
f2 = eval(input('function 2 '))

But now I get and error from the imported module in the line where the 
abs(f(x)) > tol.

It states that the 'Pow' object is not callable. And now I don't know how to 
proceed, I can't figure out a way to make the call to the imported module:

x = symbols('x')
def newton(x0, f, fx, tol, n = 2):
'''
Näherung zur lösung einer Gleichung mit dem Newton-Verfahren
x0 = Startwert
f = zu lösende Funktion
fx = Ableitung der Funktion
   
'''

   
x = x0
k = 0
   
while n > k and abs(f(x)) > tol:
k += 1
x = x - f(x)/fx(x)
print('x', k, '\n', x)
   
print('Startwert -> x0', '\n', x0)
print('Anzahl Iterationen', '\n', k)
print('f(x~)', '\n', f(x))
print('Näherungswert -> x~', '\n', x)

 
return x0, k, f(x), x

Regards

Nicco
-- 
https://mail.python.org/mailman/listinfo/python-list