News for hackers. Stuff that matters.
Re:Calculus with Julia by staplung on Saturday May 18, 2024 @06:13PM (#40402543)
Julia has a bunch of little niceties for mathematics:

* Prefixing a variable with a scaler will implicitly multiply, as in standard math notation (e.g. 3x would evaluate to 6 if x is 2)

* Lots of unicode support. Some feel almost gratuitous (∈ and ∉ are operators that work as you would expect) but it's pretty nice to have π predefined (it's even an irrational datatype) and to use √ as an operator (e.g. √2 is a valid expression and evaluates to a float). It's not just that Julia supports these constructions but provides a convenient way to get them to appear.

* This is a little less relevant for calculus but vectors and matrices are fist class types in Julia. Entering and visually parsing matrices is so much easier in Julia than in Python.

  m = [1 2 3;
       4 5 6;
       7 8 9]
vs.

  m = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
Transposition is a single character operator ('). Dot product can be done with the dot operator (m ⋅ n). A\b works as it does in matlab.

* Julia supports broadcasting. It also has comprehensions but with broadcasting I personally find much less need for comprehensions.

* Rationals are built-in with very simple syntax (1//2))

Re:Calculus with Julia by RivieraKid on Saturday May 18, 2024 @06:41PM (#40402695)
I quite like and use Julia but wish there was a language mixing the best aspects of Julia and Swift (which I think can be done without many compromises, i.e. it would be a better language overall).

Some things I don't like about Julia:

- array.mean().round(digits=2) is more readable than round(mean(array), digits=2)

- Poor support for OOP (no, pure FP is not the optimal programming approach).

Re:Calculus with Julia by staplung on Saturday May 18, 2024 @07:04PM (#40402805)
Well, Julia also has a pipeline operator built in so:

  array |> mean |> x->round(x; digits=2)
will do what you want and is arguably more readable than dot notation.

Not supporting OOP is very much intentional. It has structs and multiple dispatch (which has proven to be insanely useful in Julia's domain of scientific computing) so about the only thing you lose is the namespacing that dot syntax facilitates.

Re:Calculus with Julia by brabel on Sunday May 19, 2024 @07:06AM (#40406031)
That's not more readable, you're just switching a few characters.

Any language that has Uniform Function Call Syntax (UFCS) [1] lets you replace wrapped function calls with linear calls like that, it's clearly easier to read, and much easier to write, specially when you consider programmers' tooling (in case it's not obvious why: you can type the name of a symbol and a '.' and the IDE shows you the available functions... this works also for free functions that take the symbol's type as the first argument as well.... that could work for the pipe operator as well, of course, that's why I said that the two things are basically equivalent, just with slightly different notation).

[1] https://en.wikipedia.org/wiki/Uniform_Function_Call_Syntax

Re:Calculus with Julia by patagurbon on Saturday May 18, 2024 @06:49PM (#40402731)
You can overload your own types to get a lot of those things (poor support for OOP is harder but you can sort of emulate it with traits). Overloading `getproperty` for the former case.

Of course it's not built in, which I understand is annoying if that's your preferred coding style. I personally am sad that really good traits aren't encoded in the language.

Re:Calculus with Julia by abisen on Saturday May 18, 2024 @06:50PM (#40402739)
If I am not using named arguments I find myself using the pipe operator a lot. I also find it more readable.

array |> mean |> round

For scenarios with named arguments there is a little not so cleaner workaround

array |> mean |> x->round(x, digits=2)

Re:Calculus with Julia by andyferris on Saturday May 18, 2024 @07:42PM (#40402998)
array |> mean |> round(; digits=2) should also work and is a bit more concise.
Re:Calculus with Julia by SatvikBeri on Saturday May 18, 2024 @11:08PM (#40403917)
This doesn't work, at least for me on 1.10. `round(; digits=2)` would have to produce a function. But Julia doesn't have automatic currying, so that only happens if someone has implemented a special-case version (e.g. (==)(3) returns a partial function that tests for equality with 3.)
Re:Calculus with Julia by abdullahkhalids on Saturday May 18, 2024 @09:52PM (#40403541)
> round(mean(array), digits=2)

is how mathematicians have thought for hundreds of years now. They do so in this way, because it's indeed easier to parse, and philosophically it correctly represents what is happening mathematically.

> array.mean().round(digits=2)

is how a subset of software engineers have thought about computation in the last couple of decades.

Technology should whenever possible adopt already existing conventions, because technology is created to serve the user, and not vice versa.

Re:Calculus with Julia by adastra22 on Saturday May 18, 2024 @10:06PM (#40403602)
> is how mathematicians have thought for hundreds of years now.

"Because tradition" is not a good argument. We can and should strive for better.

The modern OOP-derived style of `array.mean().round(digits=2)` is just as exact in its specification, and arguably more semantically precise as well as it organizes the meaningful bits to be together.

Also I think you don't account for how much mathematical notation has changed over the years. The `round(mean(array), digits=2)` structure is relatively recent, early 20th century I believe, and the result of active reforms.

Re:Calculus with Julia by abdullahkhalids on Sunday May 19, 2024 @12:23AM (#40404266)
> mathematical notation has changed over the years.

I agree. Math notation has changed in response to problems mathematicians have faced, and as their thinking have evolved. It should not change because tool builders for mathematicians - when the tool builders in most cases are not even professional mathematicians - decide some other notation is better.

> array.mean().round(digits=2)

might be good (it is not) when you are operating on one object. As soon as you have a binary operation (say multiplication), this notation completely breaks down. Because the operation is often symmetric, or "near" symmetric, meaning it is not so different as to write something as ridiculous as x.product(y).

But more importantly, (common/dominant) mathematics is functional in nature. It's decidedly not object oriented. Given a matrix, M, doing something like M.eigenvalues() is vomit-inducing because eigenvalues is not a property of a matrix M. For matrices, its a map from a set of linear transformations (of which M is merely one item) onto a set of numbers. It is its own thing, separate from M.

Re:Calculus with Julia by bee_rider on Sunday May 19, 2024 @02:01AM (#40404570)
In programming it is typical to have an “eig” function, or something of the sort, which returns the eigenvalues of a matrix. eig(M) seems no less vomit inducing than M.eigenvaues(). Usually when mathematicians want eigenvalues they write something like “Given a matrix M with eigenvalues \lambda” and the eigenvalues just show up.
Re:Calculus with Julia by samatman on Sunday May 19, 2024 @09:49AM (#40406955)
In Julia:

    M = [1 2 3; 4 5 6; 7 8 9]
    λ = eigvals(M)
Which is much the same.
Re:Calculus with Julia by bee_rider on Sunday May 19, 2024 @11:37AM (#40407616)
Sure, my point is that even the math-y programming languages and libraries don’t map very well to how mathematicians seem to describe eigenvalues.

Personally I think of them as a characteristic of the matrix (but I just write numerical software, I’m not a mathematician) and implement them as the result of a process. So I don’t really see any reason to favor either syntax or call one more mathematical.

Re:Calculus with Julia by adastra22 on Sunday May 19, 2024 @12:30AM (#40404288)
Now you're gatekeeping on who is a real mathematician?

I don't think this subthread is going to be productive and I'm bowing out.

Re:Calculus with Julia by nightlyherb on Sunday May 19, 2024 @01:51AM (#40404533)
I'm not the GP, but I'm sorry you felt gatekeeped. I don't think GP conveyed "real mathematicians should do FP", but rather something else, so I'll try my best to share what I understood from that comment. Hope this makes you feel better.

Even though the notation changed over the years, the paradigm of "numbers as immutable entities and pure functions" has been the dominant way that math is presented, compared to something like "encapsulated objects that interact with each other via sending messages".

I don't think this has to be this way, and I do think that "real math" can also be laid out assuming principles of OOP. However, I do suspect it's the way it is because the laws of nature are unchanging, in contrast to the logic of a business application.

Because Julia is a tool with the target audience of mathematicians and scientists, I think it's a sensible decision to embrace the usual way of thinking, as opposed to presenting a relatively different way of thinking which steepens the learning curve. Not because data and functions are fundamentally better than OOP, but because it's more pragmatic for the target audience.

Re:Calculus with Julia by adastra22 on Sunday May 19, 2024 @03:06AM (#40404852)
I understood the other poster correctly. This view of mathematics is as limiting and ignorant as the other poster’s.

The entire field of theoretical computer science, to which functional programming and type theory is closely tied, is a branch of mathematics. The Church-Turing thesis which gives both to our field equates the two at a very fundamental level. Questions about type theory, programming language design, and programmer ergonomics are fundamentally about math and applied math.

Maybe you and the other poster have in mind specific fields of math, but then you need to make claims for why those fields are sufficiently different as to be exempt from applicability of any of the advances in notation observed in other fields.

Your implicit assumption that you can divide computer science into a different bucket from “real math” is incorrect, and gatekeeping.

As I said though, I don’t think this is a profitable debate to have here on HN.

Re:Calculus with Julia by nightlyherb on Sunday May 19, 2024 @03:48AM (#40405100)
Okay, I see what you mean.

> Maybe you and the other poster have in mind specific fields of math,

Yes, because this is about Julia, I assumed we are talking about the specific fields of math that happen to be commonly used in mathematical and scientific computing, such as the ones learned in university math and science courses.

> Your implicit assumption that you can divide computer science into a different bucket from “real math” is incorrect, and gatekeeping.

It is regretful that we had a miscommunication. I agree with you that computer science belongs into the same bucket as "real math". The thing is, in the context of Julia it is not easy to read "math" as "math, but not only the domains that Julia is concerned in, but really all fields of math, including theoretical computer science". At least thanks to your comment, I see what you mean more clearly, and I think it'll help some other potential readers as well.

> but then you need to make claims for why those fields are sufficiently different as to be exempt from applicability of any of the advances in notation observed in other fields.

I'm curious to know specifically about the specific advances of "notation observed in other fields". By this you mean dot notation for method application? I'm unsure if `a.method(b).anotherMethod(c)` is any different from `a |> method(b) |> anotherMethod(c)` (Edit: or `(anotherMethod(c) . method(b))(a)`) notation-wise.

Re:Calculus with Julia by nightlyherb on Sunday May 19, 2024 @05:14AM (#40405499)
I'd like to add another point worth mentioning. When I read the other poster's post, I was thinking about the semantics of OOP, specifically encapsulated objects and messages. I may have misinterpreted the other poster, but I hope it at least makes my other messages a bit clearer. (I think it's a little unwieldy to use Matrices that encapsulate its state for Linear Algebra, for example.)
Re:Calculus with Julia by FabHK on Saturday May 18, 2024 @11:07PM (#40403910)
I used to miss "OOP" in Julia. The funny thing about the dot notation is that it makes the limitations of single dispatch seem natural. Once you get used to multiple dispatch and its benefits, the dot notation seems limiting.

`x.plus(y)` dispatches based on (runtime) type of x (polymorphism) (single dispatch), and might call different functions based on the (compile time) type of y (function overloading)

In Julia, it would be `plus(x, y)`, and it can dispatch based on the runtime type of both x and y (or determine the correct method to call at compile time already, if the types of x and y can be determined then).

Re:Calculus with Julia by adastra22 on Sunday May 19, 2024 @03:09AM (#40404878)
Virtual method dispatch isn’t always a good thing though. It’s a tradeoff and typically you really want one or the other. C++ makes both available for that reason, although C++’s syntax is horrible.
Re:Calculus with Julia by jakobnissen on Sunday May 19, 2024 @04:26AM (#40405290)
It's multiple dispatch though, not virtual dispatch. If I understand correctly, virtual dispatch means the method is picked at runtime. In Julia, the compiler is able to pick the correct method at compile time, even when dispatching on all arguments of the function.
Re:Calculus with Julia by samatman on Sunday May 19, 2024 @09:53AM (#40406982)
This is correct, but it's important to realize that compile time continues during runtime.

Julia's execution model is quite distinctive, and it does take a thorough understanding of how it plays out to get native-code speeds out of it.

Re:Calculus with Julia by nightlyherb on Sunday May 19, 2024 @01:07AM (#40404408)
As a person who respects many programming paradigms including OOP and FP, I beg to disagree. Specifically, I'm assuming you're talking about OOP the paradigm, not only the dot notation, for which the pipe operator is a viable alternative.

> The modern OOP-derived style of `array.mean().round(digits=2)` is just as exact in its specification, and arguably more semantically precise as well as it organizes the meaningful bits to be together.

I would have agreed with you if I were developing business applications. However, for the domain of math and science, every piece of material that I have seen assumes the paradigm of numbers as immutable entities and functions, not the paradigm of mutable encapsulated objects and interacting messages. To a mathematician or scientist the semantics of a `Vector` (as data) and a `mean` function that takes the `Vector` as input should be more natural than a black-box `Vector` object that can receive a `mean` method.

Re:Calculus with Julia by RivieraKid on Monday May 20, 2024 @10:02AM (#40415567)
To me, the second is more readable, more pleasant to work with especially if you're using an IDE. And ultimately, it leads to higher productivity.

It may be philosophically correct per some definition of philosophically correct but this is not what I care about.

Re:Calculus with Julia by DNF2 on Monday May 20, 2024 @10:21AM (#40415751)
In some cases, applying a limited set of basic operations might make up a significant part of development time, but in my experience most of the time is spent designing algorithms, parsing function input and managing the flow of vectorized expressions across function boundaries. And that is generally more efficient in the context of multiple dispatch with efficient loops.

For example, writing a function that works on scalars, then mapping or broadcasting it over array input is very good for productivity (and is efficient in Julia). On the other hand, designing an inherently array-based algorithm is often a headache that one must contend with for the sake of performance in numpy.

So, if you design somewhat complex or novel algorithms, I don't think object-dot notation, or the lack thereof, has a significant impact on productivity.

Re:Calculus with Julia by ssivark on Saturday May 18, 2024 @11:33PM (#40404046)
> Poor support for OOP (no, pure FP is not the optimal programming approach).

Julia is not really a pure functional programming language, though it does support that style if one desires.

It's actually a "multiple dispatch" language... thinking of OOP as single dispatch, Julia is conceptually OOP on steroids -- but to benefit from the full expressiveness, the programming style looks different from the typical OOP syntax/patterns one is used to.

Re:Calculus with Julia by waveBidder on Sunday May 19, 2024 @01:22AM (#40404458)
having spent my PhD writing in Julia, coming back to object oriented programming felt like programming with a hand tied to my back. many operations just make more sense dispatching on multiple arguments rather than just one, and. cramming methods into a class felt unnatural
Re:Calculus with Julia by neonsunset on Sunday May 19, 2024 @06:32AM (#40405868)
Re:Calculus with Julia by pjmlp on Tuesday May 21, 2024 @04:24AM (#40425539)
Interesting remark about poor OOP support, when multi-methods and their interaction with protocols are two cool CLOS features yet to be fully embraced mainstream.
Re:Calculus with Julia by llm_trw on Saturday May 18, 2024 @08:00PM (#40403080)

    m = [[1, 2, 3],
         [4, 5, 6],
         [7, 8, 9]]
Re:Calculus with Julia by 3abiton on Saturday May 18, 2024 @07:28PM (#40402930)
But why would be better than Mathematica for example for calculus?
Re:Calculus with Julia by moelf on Saturday May 18, 2024 @07:47PM (#40403018)
it's very hard to argue anything is "better" than Wolfram/Mathematica when it comes to symbolic stuff, after all, most theoretical physicists use Mathematica for their professional work.

But for (entry-level) learning and possibly pivoting to application, Julia is delightful to use and can transit into some symbolics and numerical. Besides, it's free and open source.

Re:Calculus with Julia by llm_trw on Saturday May 18, 2024 @08:03PM (#40403090)
>most theoretical physicists use Mathematica for their professional work.

They do not.

Re:Calculus with Julia by fiforpg on Saturday May 18, 2024 @08:17PM (#40403144)
Surely, many do? The ones I interacted with did, for example. I'd venture that the preferred CAS varies with schools and countries.
Re:Calculus with Julia by llm_trw on Saturday May 18, 2024 @08:43PM (#40403251)
Some do, most don't.

Unless you're working on undergraduate integrals then it's pretty useless for advanced stuff without writing your own library. By that point you may as well write it in a language that's more performant and cheaper.

Re:Calculus with Julia by ssivark on Saturday May 18, 2024 @11:39PM (#40404082)
> Unless you're working on undergraduate integrals then it's pretty useless for advanced stuff without writing your own library. By that point you may as well write it in a language that's more performant and cheaper.

Hard disagree. Mathematica's symbolic dexterity makes abstract reasoning (with equations/expressions) very easy. Think of it as the companion tool for anyone doing pages of algebra that would go into a paper, or form the backend for some code.

The numerical capabilities of Mathematica are passable but nothing fancy. Once you have the math figured out, you might even want to reimplement "in a language that's more performant and cheaper." But I haven't see anything come close to Mathematica for convenience of symbolic reasoning -- not just as a technology (lisp is pretty good) but as a ready-for-use product.

Re:Calculus with Julia by abdullahkhalids on Saturday May 18, 2024 @09:48PM (#40403513)
I agree that only some physicists use Mathematica. But I haven't really seen it being used it for calculus. Maybe some differential equations.

But mostly for symbolic algebriac manipulation. I used it during my phd to work with groups. Instead of having to calculate stuff by hand, you can just ask Mathematica to do it. Also lots of stuff with tensors in GR is so easy to do in Mathematica.

Re:Calculus with Julia by adgjlsfhk1 on Saturday May 18, 2024 @10:21PM (#40403669)
I think it's fair to say that most math/physics people use mathematica from time to time, but largely for different things than they use other programming languages for. It's very good as a CAS, but it's a pretty bad programming language for things that don't have analytical solutions.
Re:Calculus with Julia by llm_trw on Saturday May 18, 2024 @10:42PM (#40403786)
It isn't. Mathematica is very much a niche product in academia.

The people who would get most out of it are students, but for some god forsaken reason universities don't support them.

I was in a pilot class with Mathematica back in 2006 and the review of the class were _all_ 5 stars and students on average got 10% higher marks in all other subjects they took that year.

They didn't run the course again.

Sagemath is now equally good if a teacher defines a DSL for the students to use in a class.

Re:Calculus with Julia by bee_rider on Sunday May 19, 2024 @02:06AM (#40404587)
Sagemath seems much more appropriate; it is better not to bind students to proprietary tools.
Re:Calculus with Julia by ykonstant on Sunday May 19, 2024 @04:26AM (#40405287)
The only "problem" with sagemath is that it is based on Python. The rationale is that Python is easy to start using and widely known. This is the usual "make it easy for newcomers" trap.

For the mathematical constructs we care about in symbolic programming, I have found Python's syntax and Sage's menagerie of objects awful to use. Initially you feel comfortable, but when you want to do some real work, it gets horribly in the way. The Wolfram language, a LISP variant, is less familiar and harder for a newbie to learn but it is vastly superior for actual work.

Re:Calculus with Julia by llm_trw on Sunday May 19, 2024 @06:04AM (#40405745)
Wolfram is not a lisp, even under the hood it is its own thing.
Re:Calculus with Julia by ykonstant on Sunday May 19, 2024 @07:26AM (#40406150)
Thanks, let me correct myself:

The only "problem" with sagemath is that it is based on Python. The rationale is that Python is easy to start using and widely known. This is the usual "make it easy for newcomers" trap.

For the mathematical constructs we care about in symbolic programming, I have found Python's syntax and Sage's menagerie of objects awful to use. Initially you feel comfortable, but when you want to do some real work, it gets horribly in the way. The Wolfram language, not a LISP variant, is less familiar and harder for a newbie to learn but it is vastly superior for actual work.

Re:Calculus with Julia by adastra22 on Saturday May 18, 2024 @10:07PM (#40403611)
TFA covers that question:

> There are many examples of integrating a computer algebra system (such as Mathematica, Maple, or Sage) into the calculus conversation. Computer algebra systems can be magical. The popular WolframAlpha website calls the full power of Mathematica while allowing an informal syntax that is flexible enough to be used as a backend for Apple’s Siri feature. (“Siri what is the graph of x squared minus 4?”) For learning purposes, computer algebra systems model very well the algebraic/symbolic treatment of the material while providing means to illustrate the numeric aspects. These notes are a bit different in that Julia is primarily used for the numeric style of computing and the algebraic/symbolic treatment is added on. Doing the symbolic treatment by hand can be very beneficial while learning, and computer algebra systems make those exercises seem kind of redundant, as the finished product can be produced much easier.

TL;DR: they want the student to actually do some of the work that Mathematic magics away.

Re:Calculus with Julia by eggy on Sunday May 19, 2024 @07:43AM (#40406257)
I like Julia and Python, however when I'm playing with doing math, or using paper, I like APL or J.

The above example in J:

   3 3 $ i.9
0 1 2

3 4 5

6 7 8

   3 3 $ >: i.9
1 2 3

4 5 6

7 8 9

J is 0-indexed. ">:" is the increment operator. APL allows you to set a parameter to do 1-indexed vs. 0-indexed.

APL:

   3 3 ρ ι9
1 2 3

4 5 6

7 8 9

There is a Calculus text for learning it with J:

https://www.jsoftware.com/books/pdf/calculus.pdf

Re:Calculus with Julia by garbthetill on Sunday May 19, 2024 @09:36AM (#40406875)
interesting, i was thinking of sharping my maths skill by programming. Have you tried something like lean, agda etc if so how do they fair against apl or j for doing maths

I honestly thought apl was extinct

Re:Calculus with Julia by eggy on Monday May 20, 2024 @08:39AM (#40414814)
I've tried Idris. J and APL are great for quick analysis once you know them. I leave J open on my desktop for engineering and stats. Frink too for effortless units work. Haskell and its ilk are good for math. Programming to learn math works really well for some, since you work through a problem incrementally, especially with dynamic interpreters and REPLs. Pluto with Julia or Jupyter with Python or Mathematica too.