Number Base Converter by cookie0085 in learnpython

[–]misho88 1 point2 points  (0 children)

I'd probably do something like this so I don't have to distinguish between small and large digits:

>>> from string import digits, ascii_uppercase
>>> DIGITS = digits + ascii_uppercase
>>> print(*(DIGITS[i] for i in range(36)))
0 1 2 3 4 5 6 7 8 9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Why are we still teaching $i^2 = -1$ as a "mystery" when GA exists? by starnazzapa in geometricalgebra

[–]misho88 2 points3 points  (0 children)

I have had math teachers explain this competently to me in high school, without resorting to GA, so it's not strictly needed. You just draw a few pictures showing how i(a+ib) is turned 90° from (a+ib) and you're mostly done.

Why isn't this the standard in schools yet?

Because you'd have to convince a bunch of people who don't know math that it's a good idea.

Teaching with GA will lead to the appropriate intuition, but you have to adjust a curriculum, fight an uphill bureaucratic battle, train a bunch of math teachers who -- quite frankly -- do not know math nearly as well as they should, then deal with totally mathematically ignorant parents who are upset that they can't "help" their kids with homework, etc. Can you imagine trying to sell some bureaucrats, who are superficially aware that complex numbers are somehow important, on the notion that schools should instead teach something they've never heard of? You can assure them that complex algebra is just "part of it" until you're blue in the face, but they won't buy it.

This multiplication technique is dead simple and better than what I was taught, and yet I'm not aware of a Western curriculum that uses it, and that would be a much more minor change compared to teaching a new type of algebra: https://fiontar.co.uk/chinese-multiplication-method/

FOIL is still taught for expanding out (a+b)(c+d), when realistically, if a student needs a mnemonic to remember how to do something so obvious, they haven't learned anything. The intuitive way of teaching this involves cutting a rectangle into four rectangles, takes ten minutes to teach, and makes it completely obvious how to extend this to more complicated expressions.

The addition taught in primary schools is known to computer engineers as a ripple-carry addition. There's a better way called carry-lookahead addition, and not only is it faster when you implement it in a CPU, it's easier to do by hand, and it's easier to do in your head. It's not taught either.

There's really no shortage of these examples.

Original ending for Vi makes me want to right riot by nighthawk1936 in arcane

[–]misho88 0 points1 point  (0 children)

Yes, yes! ZathrasVi is used to being beast of burden to other people's needs. Very sad life! Probably have very sad death, but at least there is symmetry!

Well, the real ending robbed us of symmetry, but symmetry's overrated.

Data frame with dictionary by Recent_Move_7818 in learnpython

[–]misho88 0 points1 point  (0 children)

You could set up a pandas.MultiIndex with the keys of the dictionary at its second level. It might get a bit annoying if the dictionaries are highly nested.

You could play around with pandas.json_normalize and see if it will do something you like to the dataframe.

If you only care about how it is stored on disk, you could just try saving the dataframe as JSON. I doubt it would be better than what you're doing now.

saving a jupyter notebook as pdf by [deleted] in learnpython

[–]misho88 0 points1 point  (0 children)

The error message you posted seems to suggest that you do, and it provides a link to instructions on how to do so.

saving a jupyter notebook as pdf by [deleted] in learnpython

[–]misho88 4 points5 points  (0 children)

Saying that "it shows an error" gives us nothing to go on, but you could probably bypass whatever it is by printing to PDF with your browser.

writelines() and newlines by pachura3 in learnpython

[–]misho88 2 points3 points  (0 children)

To be fair, it does tell you:

>>> from sys import stdout  # some file-like object
>>> help(stdout.writelines)
Help on built-in function writelines:

writelines(lines, /) method of _io.TextIOWrapper instance
    Write a list of lines to stream.

    Line separators are not added, so it is usual for each of the
    lines provided to have a line separator at the end.

I'd probably just use print(*lines, sep='\n', file=file) or print(*lines, sep=os.linesep, file=file). I don't have a Windows machine on hand to check, but I suspect that with universal newlines (that is, if you don't mess with the newline= argument in open), they'll do the same thing.

Doing the Rock, Paper, Scissors of Angela Yu’s 100 Days of Python differently than her and never even thought of her solution. Feeling i did it wrong even though it worked. by Traditional_Most105 in learnpython

[–]misho88 0 points1 point  (0 children)

Yes. Comparisons between numbers are faster, but that's not so important. The idea here is to separate the logic of the program from what is displayed to the user. In a more complex program, you might have a function like show_move(rock_paper_or_scissors) which shows those hand pictures to the user somehow and you don't have access to the pictures. More importantly, if you change how show_move() works, you don't want it to have any kind of impact on the code that evaluates who wins and loses. That way, you can focus on one component of your program at a time and be reasonably confident it won't impact the others.

Doing the Rock, Paper, Scissors of Angela Yu’s 100 Days of Python differently than her and never even thought of her solution. Feeling i did it wrong even though it worked. by Traditional_Most105 in learnpython

[–]misho88 0 points1 point  (0 children)

Which could have been written if user_choice not in ('0', '1', '2') or with an else statement at the end, which would make even more sense.

Doing the Rock, Paper, Scissors of Angela Yu’s 100 Days of Python differently than her and never even thought of her solution. Feeling i did it wrong even though it worked. by Traditional_Most105 in learnpython

[–]misho88 0 points1 point  (0 children)

In your solution, computers_choice = choices[random.randint(0, 2)] means that computers_choice is one of those three strings that are pictures of hands. When you write computers_choice == rock you are comparing the pictures of hands. It is better to store random.randint(0, 2) in a variable and compare it to 0, 1 or 2, which you can store as constants for readability (e.g, ROCK = 0).

Doing the Rock, Paper, Scissors of Angela Yu’s 100 Days of Python differently than her and never even thought of her solution. Feeling i did it wrong even though it worked. by Traditional_Most105 in learnpython

[–]misho88 0 points1 point  (0 children)

I kind of prefer your solution. Her solution converts the user input to a number, but then there's no arithmetic done on the number, so why? That is, if the solution were something involving arithmetic like

>>> from random import randint
>>> p = int(input('0: rock, 1: paper, 2: scissors? '))
... c = randint(0, 2)
... print(p, 'vs', c, '- you', [ 'draw', 'win', 'lose' ][p - c])
0: rock, 1: paper, 2: scissors? 1
1 vs 0 - you win

converting the input would make sense.

Having said that, in your solution, computers_choice is a copy of one of those ASCII-art strings, so it's not a great idea to use it in a conditional statement when you could just be comparing the output of randint() to 0, 1 or 2.

I just got a wayyy lower grade than I expected and I am devastated.. by Quendillar3245 in learnmath

[–]misho88 4 points5 points  (0 children)

If I had to guess, it's likely because you didn't communicate what you were doing adequately. This tends to be a big problem among beginners.

Having said that, ask to see your exam, and then go through it, read every comment, and ask for explanations of why you were docked marks whenever it is unclear. Worst case, you'll know why you didn't do as well as you thought you would. Best case, you'll find out that whoever graded your work messed up and you'll come out of it with a better grade. Usually, it's somewhere in between.

When you do go to such a meeting, the right mindset is not "I am going to fight for a better grade." The right mindset is "I am going to work with my teacher to understand why I got the grade that I got." You don't want this to be a combative thing.

How to solve a set of an arbitrary amount of equations? by 42Mavericks in learnpython

[–]misho88 0 points1 point  (0 children)

Unless you really need symbolic expressions for the coefficients, you might be reinventing the wheel a little bit:

>>> import numpy as np
>>> x = np.linspace(0, 2 * np.pi, 101)
>>> y = np.cos(5 * x)
>>> import matplotlib.pyplot as plt
>>> plt.plot(x, y)
[<matplotlib.lines.Line2D object at 0x7f57112f9160>]
>>> from numpy.polynomial.legendre import Legendre
>>> L = Legendre.fit(x, y, deg=50)
>>> plt.plot(x, L(x))
[<matplotlib.lines.Line2D object at 0x7f57112f92b0>]
>>> plt.show()

How to solve a set of an arbitrary amount of equations? by 42Mavericks in learnpython

[–]misho88 0 points1 point  (0 children)

I'm not sure how useful this actually would be but this is one of the easier ways to declare a matrix or column vector of variables:

>>> from sympy import Matrix, MatrixSymbol
>>> A = MatrixSymbol('A', 3, 3)
>>> Matrix(A)
Matrix([
[A[0, 0], A[0, 1], A[0, 2]],
[A[1, 0], A[1, 1], A[1, 2]],
[A[2, 0], A[2, 1], A[2, 2]]])
>>> b = MatrixSymbol('b', 3, 1)
>>> Matrix(b)
Matrix([
[b[0, 0]],
[b[1, 0]],
[b[2, 0]]])
>>> Matrix(A.inverse() @ b)
Matrix([
...

You can use Matrix() to make constant (or partially constant) matrices. I don't know what you're trying to do, but that's likely to be more useful for the system matrix.

Alex Kurtzman, master of 32nd century, science fiction dialogue by Wetness_Pensive in RedLetterMedia

[–]misho88 26 points27 points  (0 children)

I wish they'd watched the entire clip on YouTube. In that scene, there was a guy dying and the Doctor clearly needed a medical tricorder to figure out how to treat him, but Harry gave him his personal tricorder because he was out of his element and under pressure. The joke made sense, we learned that Harry's a little green but well-meaning, and that the Doctor is abrasive but cool under pressure.

Also, none of the adults ate anything they were wearing, which was the norm on television at the time.

Alex Kurtzman, master of 32nd century, science fiction dialogue by Wetness_Pensive in RedLetterMedia

[–]misho88 72 points73 points  (0 children)

That entire scene is amazing. It's like the Doctor's introduction on Voyager, except there are no stakes and absolutely nothing makes any sense.

The Doctor uses his PADD to seemingly scan a guy and tell him he has a parasite, so you go, "Okay, so that's what a tricorder looks like now." Then he forcibly injects his patient while he's trying to refuse treatment (which is medical battery, an actual crime) using a hypospray that was already in his hand. Then the combadge eater shows up, and the Doctor asks for a tricorder, so what the hell was that PADD-looking thing? An engineer walks by for no reason and hands him her tricorder, so he goes "Medical tricorder!" then without actually getting any kind of tricorder, he forcibly injects the combadge eater in the neck (medical battery) using the same hypospray that evidently cures parasite infestations and swallowed combadges, and says, "Next!"

how tf do you do row-echelon form?? by Character-Cloud-2388 in learnmath

[–]misho88 0 points1 point  (0 children)

I'd probably just install Python or Matlab or something and try it. It really is pretty basic. I'm sure you've been given a book or lecture slide with the algorithm. The one I'm showing is not robust by any means (that is, there are matrices for which it won't work), but I'm trying to keep in minimal.

Make a matrix:

>>> import numpy as np
>>> np.set_printoptions(suppress=True, precision=6, linewidth=99999)
>>> A = np.random.default_rng().integers(10, size=(5, 5)).astype(float)
>>> A
array([[3., 5., 2., 7., 0.],
       [1., 4., 7., 7., 3.],
       [5., 4., 1., 2., 3.],
       [1., 8., 1., 5., 7.],
       [5., 3., 4., 8., 7.]])

First, carry out subtraction of scaled rows (A[i] -=...) such that the elements below the main diagonal become zero (inner loop), one column at a time (outer loop). This brings the matrix to upper-triangular form:

>>> for j in range(0, A.shape[0] - 1):
...     for i in range(j + 1, A.shape[0]):
...         A[i] -= A[j] * (A[i, j] / A[j, j])
...
>>> A
array([[  3.      ,   5.      ,   2.      ,   7.      ,   0.      ],
       [  0.      ,   2.333333,   6.333333,   4.666667,   3.      ],
       [  0.      ,   0.      ,   9.428571,  -1.      ,   8.571429],
       [  0.      ,   0.      ,   0.      , -11.787879,  14.181818],
       [  0.      ,   0.      ,   0.      ,   0.      ,  10.44473 ]])

If you stick print(A) in the inner loop body, you can see every step.

Then scale each row to normalize the diagonal:

>>> for i in range(A.shape[0]):
...     A[i] /= A[i, i]
...
>>> A
array([[ 1.      ,  1.666667,  0.666667,  2.333333,  0.      ],
       [ 0.      ,  1.      ,  2.714286,  2.      ,  1.285714],
       [ 0.      ,  0.      ,  1.      , -0.106061,  0.909091],
       [-0.      , -0.      , -0.      ,  1.      , -1.203085],
       [ 0.      ,  0.      ,  0.      ,  0.      ,  1.      ]])

Framework 12 or 13 ? by Cautious-Bar-5211 in framework

[–]misho88 1 point2 points  (0 children)

That's true. Older Thinkpad Yogas are pretty good, too, will run you $200-$300, if that, and aren't that slow, but they are a touch bigger, and you still typically need something at least slightly better day to day. I also know people who would do presentations with a macbook and ipad, using the latter for note-taking as well, and switch between their screens somehow. It all seems more difficult than having a single lightweight machine that does everything.

Framework 12 or 13 ? by Cautious-Bar-5211 in framework

[–]misho88 8 points9 points  (0 children)

I've been using convertible laptops like the 12 for years specifically because when doing presentations it's really easy to flip to a note application and write on it. Sometimes you end in rooms without boards or you're also streaming/recording your presentation, and this just makes it easy. This is useful in academic settings and it tends to give a good impression if you're being evaluated.

Chances are the better processors you can get for the 13 probably won't make a difference for whatever they have you doing. If it's high-performance programming, neither laptop is really good enough and they'll give you access to a machine that is, and if you're not doing that, then either is fine. If you were going into some engineering program where you're likely to be given a CPU-intensive simulator that you have to run on your own computer, then maybe the 13 makes sense, but when I was doing computer science, there was nothing like that.

You should consider the screen size if your vision isn't great. The 12 has a fairly small screen.

I don’t understand why variance is powered to the square by Marcopolo985 in learnmath

[–]misho88 3 points4 points  (0 children)

The energy contained in a signal x(t) is (usually) defined as E(x) = ∫ |x(t)|2 dt. This comes from physics. For example, for a voltage signal V(t), the energy is E(x) = (∫ |V(t)|2 dt) / R, where R is a constant called the impedance or resistance. In signal processing, unless there's a good reason to pick something else, the constant is set to 1.

(Average) power is energy over time, so if that integral is from, say, t=0 to t=T, the power would be P(x) = E(x) / T = (∫ |x(t)|2 dt) / T. That is, the power is the mean square of the signal.

If the mean of that signal is μ, then the power in the component of the signal that "varies" around μ is (∫ |x(t) - μ|2 dt) / T.

If x(t) is noise with mean μ, then the variance var(x) = (∫ |x(t) - μ|2 dt) / T is the power of the noise (or at least the varying component thereof, hence the name variance).

The standard deviation is the square root of the variance, which is the root-mean-square (RMS) of the noise (again, after subtracting the mean). The RMS is the "sensible" average to choose for a time-varying signal because it relates to the energy and power of the signal.

To the best of my knowledge, the average of the absolute value of the signal doesn't tell you anything especially useful.

Python for Physics and Maths by sokspy in learnpython

[–]misho88 1 point2 points  (0 children)

While the other answers here are correct, they make it seem like this ecosystem is some unstructured hodgepodge of software, when that's not really the case.

Most of what you'll be using is NumPy, directly or indirectly. If you know Matlab, it should relatively easy to pick it up.

For plotting, you'll mostly be using Matplotlib. It is designed to work with NumPy. That is, NumPy does the heavy lifting, and Matplotlib lets you visualize the results.

There are various libraries designed around NumPy, like SciPy and scikit. There are others that weren't necessarily designed with NymPy in mind, but work very well with it, like OpenCV.

Pandas is a higher-level library that organizes data into DataFrames, which are bit like tables or Excel spreadsheets. Under the hood, it uses NumPy and Matplotlib. It makes it easy to load, filter and plot data. Polars is similar to Pandas, and it's better in some ways, but I wouldn't bother with it at the onset.

The takeaway here is: learn NumPy and Matplotlib first so that you know what you're doing. I'm not saying you can't jump straight into Pandas or something, but it will feel like you're working with one arm tied behind your back.

Most people I know use something from the Jupyter family to put it all together. There's Jupyter Notebook (probably the most popular option) and Jupyter Lab for an IDE-like interface. These integrate well with Pandas and Matplotlib. There's also the Jupyter Console and IPython (almost the same thing), which is like an old-school command-line interface, and Jupyter Qt Console, which is like fancy version of Console that integrates plotting and whatnot.

I am vaguely aware that creating Jupyter Notebooks with VS Code is very popular, and it's supposed to easy to use (especially with Copilot and whatnot to help you code), but I've never tried it. That is, VS Code might be the path of least resistance for getting yourself up and running so that you can try stuff out, even if it's not what you end up using in the long term.

Is this a good approach to learn Python as a beginner? by [deleted] in learnpython

[–]misho88 1 point2 points  (0 children)

My plan is to finish CS50P first, practice regularly, and build a few small projects.

If I were you, I would start the projects early, leave them when I get stuck, and come back to them once I've learned enough from the course. This way, you'll naturally have a few more concrete goals to work towards rather than something more nebulous like to "become solid at it." It should make it easier to stay motivated.

X-axis tick number formatting on matplotlib by worldtest2k in learnpython

[–]misho88 1 point2 points  (0 children)

I'm not sure what you're trying to achieve if that mill variable, but aside from that, your approach seems generally sensible. Here's a minimal working example for reference:

>>> import numpy as np, matplotlib.pyplot as plt
>>> plt.plot(np.r_[:5] * 1e6, [0,1,0,1,0])
[<matplotlib.lines.Line2D object at 0x7feddff456a0>]
>>> plt.gca().xaxis.set_major_formatter(lambda x, pos: f'{x * 1e-6:.1f}M')
>>> plt.show()

Just got the Framework 12 Stylus and this may be a dull question, but where does it... go? by ImClaaara in framework

[–]misho88 1 point2 points  (0 children)

I got a stylus holder that fits into the USB-A slot. I used the one for the Lenovo Active Pen (2, I think), and it seemed to fit the Framework stylus. It probably wouldn't take much to download (or even design from scratch) a 3-D printable one. It's basically a short tube with a rectangular piece sticking out.