r/Python • • Aug 05 '19

Python's response to MATLAB

Post image
4.3k Upvotes

372 comments sorted by

View all comments

Show parent comments

97

u/Eurynom0s Aug 05 '19

My favorite thing about Matlab is it has both i and j defaulted as the imaginary unit, but will silently let you overwrite those values. I'm not saying that's an invalid design decision, but it doesn't really make sense to spring on the intended audience: physicists/engineers/etc who don't really have a programming background but need to be able to code some simulations. That crowd is often going to want to basically just type in the equations in their textbooks (hence reserving both i and j for the imaginary unit—in electrical contexts, i is current, so they use j for the imaginary unit), which will often result in looping over ijk indices since those are what textbooks usually use for 3D vectors/matrices. That crowd will also typically not have the background to realize the potential problem here.

I found out about this via someone on reddit saying that they spent an entire summer pulling their hair out trying to figure out why their complex equation was returning a pure real result and finally figured out it was because he'd used i as a loop variable and instead of being reset to sqrt(-1), it was staying as the last value it hit while iterating through the loop. If they want to keep the behavior they should really at least print a warning about what you're doing to the console.

30

u/cue_the_strings Aug 05 '19

Our instructor warned us of the j, k that when giving us an intro to loops and stuff. This design decision further shows that MATLAB was meant for the engineering / physics crowd. As you already know, using i and j as unnamed counters is pretty much a convention in computer programming (C-derived languages especially), the designers certainly knew that it would be a problem, yet they decided they'd just make it more convenient for the non-programmer crowd.

I see MATLAB as a mutation of an ancient attempt to make a tool + language for engineers and scientists, which would be 'simple to learn' and have everything they need for numerical computation, visualization, ... But decidedly not for programmers. The dumb BASIC-like syntax is a consequence of that decision - at the time, BASIC was considered the 'easy' language for non-programmers that everybody could learn. It was even taught in schools.

Anyway, as it usually goes, once something is made one way, it's really hard to change it. There are and were large swaths of people to whom MATLAB is the only language they use. They don't know or care about any subtleties of programming as long as MATLAB helps them do their job. And those people learned it one way and are very resistant to change, they would be pissed off if the syntax was changed (say, for the better, not that they would know). So the language was just added onto and never revised, there are often multiple ways to do one thing because removing syntax is a taboo. The result is, of course, a hot mess.

8

u/ergzay Aug 06 '19

You haven't met the people that "program" programmable logic controllers with "ladder logic". If you want to talk about lock in... Now that's an entirely different level.

2

u/cue_the_strings Aug 06 '19

I saw that in an AvE video, the horror!

1

u/ergzay Aug 06 '19

There's tons of people who do it in industry notably the automotive industry.

2

u/Eurynom0s Aug 06 '19

Yeah, they're going to use i or j as the imaginary unit, and any numerical methods recipes they look up are going to have them using i and j and k as loop variables. It will obviously not end well.

And you're right that they're probably stuck with the design decision at this point, which is why I suggested printing a warning to the console, it would give people a heads-up without breaking backward compatibility.

9

u/[deleted] Aug 05 '19

... but i, j, k are default indicies for so many functions.

That's just downright evil, I don't care who ya are.

3

u/ThereOnceWasAMan Aug 06 '19

This doesn’t invalidate your point at all, but just as a note: the language also supports any integer concatenated with j as imaginary. So 1j is still the square root of negative one, even if j is being used as an iterator variable.

4

u/[deleted] Aug 05 '19

that happens, for sure, but generally I wouldn't claim this to be a problem.

stem people are problem solvers, so they will likely find a solution, additionally, I know a good chunk of people from high school that I though fer sure would go for computer science, but went into physics / math instead, so I also think it's unlikely that in a classroom or tutor setting no one has an idea what the problem is.

of course, this is probably a well known problem and instructors will likely point it out, and ( and I am of course biased in that, because of my "programming background" ) on the other hand, that assigning the variable might have changed the value of i or j can probably be concluded by people dealing with formulas on the daily

3

u/Feminintendo Aug 06 '19

So really smart STEM people are smart enough to figure out all the stupid shit Matlab does... Except the T and M parts of STEM, almost none of which use Matlab because they actually have a clue. But the S and E's are so much smarter than those stupid mathematicians that it doesn't matter.

Brilliant argument. "We use inferior tools because we are literally smarter than the people who use better tools."

1

u/[deleted] Aug 07 '19

Your math courses don't use MATLAB?

Also, I will put this brilliant argument into my repertoire for when I encounter a rare non-MATLAB person ( sophisticated term for humanities student ), thank you very much.

1

u/Feminintendo Aug 07 '19

In graduate school I had to teach a calculus lab with Matlab for the engineering calculus sequence. It was a nightmare for everyone involved. The students learned neither Matlab nor calculus from the assignments. It was a good idea on paper: engineering students would get some exposure to a tool in their field while learning some calculus concept. In practice, figuring out how to write the code was so distracting to them that any calculus benefit was completely lost, and the assignments, being calculus assignments, weren’t nearly sufficient for them to actually learn any useful coding. The problem wasn’t Matlab-specific. We also had a Maple-based version of the course that was just as bad.

1

u/I_Feel_It_Too Aug 06 '19

stem people are problem solvers, so they will likely find a solution,

It's not a problem that Matlab is a shitty programming language because STEM people are problem solvers? Really?

1

u/[deleted] Aug 06 '19

Yes

1

u/I_Feel_It_Too Aug 07 '19

At least we both agree that Matlab is a shitty programming language. Let's get a beer!

-3

u/mexiKobe Aug 05 '19

It’s self-explanatory though. If you set i or j equal to something else.. that’s what it will equal.

10

u/Eurynom0s Aug 05 '19

Again, Matlab is targeted at non-programmers, which is the kind of audience you might expect to assume that the scope of the ijk being used as loop variables is restricted to the loop.

0

u/[deleted] Aug 05 '19

[deleted]

3

u/IcecreamLamp Aug 05 '19

They're often used as counter variables in a loop. If you then later want to use i as the imaginary number, it will instead have the same value it had when you exited the loop.

5

u/Eurynom0s Aug 05 '19

This isn't really specifically about programming. In math and physics textbooks, you often see the unit vectors given as i j and k (instead of x y and z); vector/matrix indices will often be given ijk in a subscript; you'll often see triple summations with i on one, j on the second, and k on the third; etc. So the audience Matlab is intended for is going to want to loop over ijk (xyz) since it matches the notation they're used to using.

1

u/Feminintendo Aug 07 '19

What you are saying is contradictory. If everyone assumes i, j, and k are the orthonormal basis vectors for R3, then they obviously wouldn’t loop over them, because what the hell does it mean to “loop over” a basis vector? On the other hand, if everyone assumes i, j, and k are undistinguished available names for integer variables, they’d happily loop over them but wouldn’t think to use them as an orthonormal basis for R3.

But you’re mixing and matching, it seems. You seem to be saying that since STEM people see i, j, and k in textbooks used as basis vectors, it follows that they would assume i, j, and k are ineger iterators in loops. That’s a total non sequiter.

In a sane language, if i is the imaginary unit, it is write protected. I mean, there are a lot of un-sane languages according to this definition, but most CAS’s get this right, at least.

1

u/[deleted] Aug 05 '19

They are used in looping variables commonly, and correspond in math to imaginary numbers (most folks are familiar with i == sqrt(-1)). The nerdy term is quaternion.

→ More replies (1)
→ More replies (6)

1

u/eypandabear Aug 06 '19

That’s great, especially in a programming environment with global name scope.