Good catch, was just beginner me(I made this a while ago when I had only been programming for only a few months). It is inefficient having that variable recreated every iteration, true.
The lazy in me will tell myself that its pointless to fix since Python isn't used for its speed ha. I've been working with cpp lately and maybe will try to implement a similar function then benchmark them both. I want to see how MUCH faster c++ is. I know about interpreted vs compiled, but never realized how much quicker c++ was. In some cases its an order of magnitude faster, 10x, 100x etc.
Does anybody know if there are available tools/libraries to render plots in c++?
Haha exactly! I also didn't realize that the string formatting for these functions takes LaTeX, which, if you know, is not the most elegant and beautiful syntax
Yup. It determines if a Geometric series convergence/divergences by the Common Ratio test. If the common ratio is a less than(absolute value, so -1 < r < 1) 1 it converges. Greater than 1 is diverges. Its fairly straight forward once you write it out:
if r = 0.5 and sum up some terms: a_1(r)n-1 = 1(0.5)^0 + 2(0.5)^1...25(0.5)^24 = 1 + 1 + ... 0.00000149(wow that is small!)
so the greater n, the smaller the number you add gets. The opposite happens when its greater than 1. Eventually the number you add gets so small as you approach infinity that we just say the limit has been reached.
At the time I didn't know you can use f strings with ax object. Tbh, even now I don't know much of the newest Python syntax. I TA for python, and we use the older version(will have to check which). The only time I use the newest version is with LC or something.
If anyone is interested here is a slightly cleaner/faster version.
I have not replicated some of the plotting stuff.
def gen_plot(a, b, iterations):
'''plot the partial sums of i=1 to inf of a * b^(i - 1)'''
# create lower left triangle matrix
# each row i is integers [0, i)
idxs = np.arange(0, iterations).reshape(1, -1)
pows = np.tril(np.repeat(idxs, iterations, axis=0))
# raise b to the matrix elementwise and sum
ys = np.power(b, pows)
ys = np.sum(ys, axis=1)
# we will have extra b^0 so remove them
ys -= np.arange(iterations - 1, -1, step=-1)
ys *= a
fig, ax = plt.subplots()
fig.suptitle(f'Sequence of partial sums for $\sum_{{i=1}}^{{\infty}}{{{a}\cdot{b}^{{i - 1}}}}$')
ax.set_xlabel('Number of sequence elements used for partial sum')
ax.set_ylabel('Partial sum')
# Use integers only for x-axis
ax.xaxis.set_major_locator(MaxNLocator(integer=True))
# plot
ax.scatter(idxs, ys, label='partial sums')
ax.axhline(y=ys[-1], color='g', alpha=0.8, linestyle='--', label='limit')
ax.legend()
plt.show()
Nice! I made it when I was a novice so wasn't aware of all the built in math functions, so I did a lot of stuff "the long way". I'm planning on making this function but in C++ !
matplotlib has methods that can take like 10+ parameters. Graph formatting is with LaTeX, it gets the job done but its no the most beautiful code. It produces beautiful results though!
Though, is your code like this because of Matplotlib, or all your code? feel free to PM/send me some code you've written! I can give some pointers and suggestions if needed.
67
u/MurdoMaclachlan public boolean isInt(int i) { return true; } May 06 '23
Image Transcription: Code
I'm a human volunteer content transcriber and you could be too! If you'd like more information on what we do and why we do it, click here!
28
u/carcigenicate May 06 '23
Why are
xandybeing assigned in the loop?27
u/ALapsedPacifist May 07 '23
In fairness,
matplotlib(and other libraries that have functions with 15+ parameters) are painfully ugly to use.6
u/Ruin369 shameless May 06 '23
Can anybody figure out what this is doing?
If you've taken Calculus II or a discrete math class, it should be fairly easy.
Bonus points if you know: Which test am I using to determine the end behavior.
7
u/faCt011 May 06 '23
Now finally show the diagram!!
2
u/Flaming_Eagle May 07 '23
f strings my dear child, please use them! You don't need to explicitly convert numbers to strings, that's taken care of for you
2
u/seftontycho May 07 '23
If anyone is interested here is a slightly cleaner/faster version. I have not replicated some of the plotting stuff.
17
u/Leo-Hamza May 07 '23
Trust me, as a math student this is very good for our us
6
u/S1v95 May 07 '23
Least readable you've written so far
1
u/Ruin369 shameless May 07 '23
Github repo
1
u/Better-You-3301 May 07 '23
Wait this is the least readable? this is how I normally write code.
How do I improve?
1
u/Professional-Key-266 May 07 '23
SEMICOLON?????
2
u/Tc14Hd May 07 '23
That
abs((var2))is just horrendous.1
u/Estatic_Penguin May 07 '23
Can you please explain wth is going on with that 's' variable , It's like a geometric sum then in the scatterplot it's set to 1.8 then it's set to x .
1
u/OpenSourcePenguin May 07 '23
What python version do you use?
Use fstrings
1
u/[deleted] May 07 '23
I genuinely think a few blank lines to separate different sections (think paragraphs) and a comment for each section would make it 10x more readable
1
u/kongu3345 May 07 '23
Matplotlib is /r/badcode easy mode
1
u/drksntt May 08 '23
The if statement is sending me lol
1
u/sulizu May 08 '23
Why no comments ? That's straight torture for people having to read this.
1
u/Hexagonforcefield May 08 '23
Bro this is the equivalent of a war crime but in python ;)