'Using float numbers in 'range' and 'subsetting'
I am working on a small assignment in which I am required to use 'array' and very basic and simple stuff to create a function that draws math functions such as: X^2, X^3, log(X) and so on. I made the following code:
> import numpy as np
>
> def func_draw(func):
> # Main Axes
> base = np.full([21, 61], ' ')
> base[10] = u'\u2014'
> base[:, 30] = '|'
> # Input and Draw
> f = lambda x: eval(func)
> for x in range(-30, 30):
> y = f(x)
> if y in range(-10,10):
> i = 10
> j = 30
> base[i - y, j + x] = u"\u26AC"
> print('\n'.join(map(lambda b: ''.join(map(str, b)), base)))
>
> func_draw('x**3')
Now the output is correct I believe, but problem is since we are exponentially increasing 'y' for example in X**3, the number of available points in the range of axes become less and less. and the shape of the graph includes few points:
Now what came to my mind was to use numpy range so that I become able to set step in my range for float numbers and this way, many points in the axes range can be shown but somewhere along the way it seems like 'int' and 'float' cannot be used together. (probably the subset of 'i' and 'j'). Is there a solution to make this work totally with floats, so that I can get many more points on diagram? or anything similar?
Solution 1:[1]
I managed to this, some sort of scaling but it gives some flat areas on the graph since it rounds the float.
y = int(f(x/10)*10)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Benyamin Hamidekhoo |