1

I am trying to work on a graphing calculator in Python using Numpy and Matplotlib. Here is my code:

import numpy as np
import matplotlib.pyplot as plt

a = input("enter operation")
#operation can be something like '**2 or + 1'
b = np.arange(1,10)
#here is where i am stuck. 

How do you add the input a to numpy array b?

I want another method besides eval. All the other projects people have showed me use eval.

Abhiraam Eranti
  • 350
  • 5
  • 19
  • 1
    I suggest you take some time to learn about string parsing. – Code-Apprentice Jul 31 '18 at 01:15
  • There are many existing duplicates, search for "evaluate mathematical expression" and the `eval` function (although that has security issues). Here are a few: [Math expression evaluation](https://stackoverflow.com/questions/1545403/math-expression-evaluation), [python if statement with variable mathematical operator](https://stackoverflow.com/questions/11847359/python-if-statement-with-variable-mathematical-operator), [eval() with a variable operator](https://stackoverflow.com/questions/33673116/eval-with-a-variable-operator)... – smci Jul 31 '18 at 01:39
  • [Evaluating mathematical expression from a string and inserting it on stack](https://stackoverflow.com/questions/41445176/evaluating-mathematical-expression-from-a-string-and-inserting-it-on-stack)... – smci Jul 31 '18 at 01:40
  • This should be closed-as-duplicate. Of which question? – smci Jul 31 '18 at 01:40

2 Answers2

0

Do you mean to apply the input operation to each value of b? if so, this may works for you, but I suggest you make it clear that which operations are allowed and make a check with regular expression, because 'eval' will run any codes that the users input, which may be quite dangerous.

import numpy as np
import matplotlib.pyplot as plt

In [4]: import numpy as np
   ...: a = input("enter operation\n")
   ...: #operation can be something like '**2 or + 1'
   ...: b = np.arange(1,10)
   ...: #here is where i am stuck.
   ...: func_str = 'lambda x:x ' + a
   ...: func = eval(func_str)
   ...: func_vect = np.vectorize(func)
   ...: result = func_vect(b)
   ...:
   ...:
enter operation
**2

In [5]: result
Out[5]: array([ 1,  4,  9, 16, 25, 36, 49, 64, 81])
Woods Chen
  • 574
  • 3
  • 13
-1

Why don't you just

import numpy as np

a = input("enter operation")
# operation can be something like '**2 or + 1'
b = np.arange(1, 10)
np.append(b, a)
# here is where i am stuck.

Did I catch your intention?

Or you can try this.

while True:
    a = input("enter operation")
    b = np.arange(1, 10)
    operator, = re.compile(r'([*+/-]+)').findall(a)
    num, = re.compile(r'([.0-9]+)').findall(a)
    print(operator, num)
    result = None
    if operator == '+':
        result = b + float(num)
    elif operator == '-':
        result = b - float(num)
    elif operator == '**':
        result = b ** float(num)
    print(result)

where re package is used for string manipulation. If you type +2, 2 is added to all the components of b to yield result. Likewise, if you type **2, you will get squares of b.

Hermis14
  • 214
  • 3
  • 12