12 ms·
Draft of OCaml Scientific Computing book
- logicchains 6y agoI wonder if there's any overlap between this and https://www.ffconsultancy.com/products/ocaml_for_scientists/index.html https://www.ffconsultancy.com/products/ocaml_for_scientists/..., or would it still be helpful to read both of them?
- forgotpwd16 6y agoThat's what the kind of book it came to my mind when I read the title. No, they differ though there is slight overlap and yes, someone should read them both. OfS first, OSC next. Specifically. OfS is an introductory OCaml book (first half) which has example usage of interest to scientists (second half). OSC though it has some introductory text per section is mostly concerned in showing Owl, which is what you'll end up using.
- dna_polymerase 6y agoIs OfS available anywhere? Not that I advocate for piracy, but the site seems abandoned and the payment link is dead.
- chalst 6y agoThat book was published in 2005, so I guess they will not be close competitors.
- marmaduke 6y agoI think the ff people moved on to f# for performance reasons. Owl is fairly new, and it has hopefully been able to learn from other ecosystems like Python.
- forgotpwd16 6y agoOnline version is available on https://ocaml.xyz/book/ https://ocaml.xyz/book/. Pretty interesting. Reading it, seems closer to a tutorial in using Owl, an OCaml-written package for technical computing (what e.g. Matlab is; though architecture differs according to post).
- zelphirkalt 6y agoI am not sure why you are getting downvoted. If you had not posted the direct link already, I would have done so. Perhaps the downvotes are about the second part of your comment.
- G4BB3R 6y agoEach year I think wether should I learn OCaml or not. What is the current state of multi-thread OCaml? Is that a game changer or just a cool feature? I can't understand why OCaml doesn't have mass adoption.
- doteka 6y agoI think Rust kind of stole OCaml’s thunder. It has most of the same benefits, with a mostly familiar syntax and a lot of industry buy-in. And with recent versions, I never find myself fighting the borrow checker like I used to.
- pjmlp 6y agoI rather have the productivity of a tracing GC. I don't see Rust being the best option for anything other than low level systems code.
- vmchale 6y agoReally? Just made me more reluctant to use a language without sum types :p
- zucker42 6y agoI don't know. I think Scala and F# have stolen OCaml's thunder more than Rust. While Rust has been inspired by ML, OCaml and Rust have much different usecases.
- TallGuyShort 6y agoI've never encountered a real OCaml project or anyone who uses it in my career (same is true for Haskell). I have assumed these languages are a hobby for CS academics and get used for pet projects by their devotees. Not that that's bad - they're interesting and the ideas are cool. I would just be afraid of locking myself into an isolated ecosystem that it's hard to hire experienced people for. Is anyone on HN actually using these languages for scientific computing, or other large production projects? Curious to know what the pros / cons are in practice and how common that is.
- non-entity 6y agoI've talked to at least one persons who works on production Haskell applications, and Jane Street, a company that was discussed on HN just yesterday makes heavy use of OCaml, but yeah they're pretty rare and I suppose the people working with them are just lucky. I've heard particularly about the Haskell market that if you want a chance of competing for the few jobs available you have to be among the top-haskellers, but I'm not sure.
- Tarq0n 6y agoJane Street being the only company that anyone ever mentions when discussing OCaml is even worse in my opinion. It means the ecosystem is going to be heavily driven by their needs, not to mention that banks tend to have idiosyncratic development cultures.
- mseri 6y agoIt is not the only one though. There are Ahrefs, Tarides, Tezos, Citrix (XenServer and a part of Xen are in OCaml), Inria, Facebook (for compilers, typecheckers and ReasonML), Bloomberg (was Bucklescript/ReScript, now at facebook though I believe). There are also some academic projects with industrial uses. Directly to mind come Coq, Frama-C, Mirage and the Zélus compiler. EDIT: added Inria and Frama-C
- brmgb 6y agoJaneStreet is not the only user of OCaml. Both INRIA and the CEA uses OCaml heavily (Coq, CompCert, Frama-C). Cambridge uses it for MirageOS, Facebook to write software analysers and now web applications (the web version of Facebook Messenger), Citrix in XenServer. Bloomberg developed a compiler from OCaml to Javascript.
- dunefox 6y agoInteresting idea to use an ML for scientific programming, but I don't see any practical reasons not to use Julia or Python. I'd rather take advantage of everything Julia already offers (+ Python with PyCall.jl) than wait for the same support in a language not widely used in the first place.
- mseri 6y agoYou can use PyML to call python from OCaml in the same way, and it works fine to pass an owl ndarray to numpy. As a user of both, I think they have different treadoffs. I tend to use OCaml when I am playing around with the code because I find it infinitely easier to refactor (and to figure out what I was doing if I leave the code rotten for too long)
- fluffything 6y agoSkimming through this book, one thing i was constantly wondering, is how well does this ocaml framework use the hardware. Leaving ocaml aside, the connection between scientific computing and hardware is the one thing I miss the most in "scientific computing" books and courses, because it sooner or later limits the science that any researcher doing scientific computing can do. To give an example, earlier this week, one of our scientists was waiting 10 minutes between each interactive iteration of their data-set, so I was called to help, and the only feedback they gave was that "its slow", to which I replied "slow with respect to what? how fast are you expecting this to be and _why_?". The answer to these questions is the difference between "maybe they just need a faster computer", "maybe they need a different algorithm", or even "maybe this problem cannot be solved today because computers this fast do not exist". From their facial expression, it looked to me that they actually had never thought about any of this, probably because whatever they did before was always fast enough, but now this issue was limiting their science and they were lacking the bare minimum set of tools to even get proper help. If you are doing scientific computing, chances are that the problems you are going to be dealing with are going to be getting bigger and harder as you advance in your career. For many scientists, the first problems will actually be big enough for the hardware to matter. I wish scientific computing courses and books will at least provide the most basic tools to these scientist for them to at least be able to get meaningful help. Having someone on call for when this matters is quite expensive.
- matrixanger 6y agoThis has a chapter on low level optimisation in Owl: https://ocaml.xyz/book/core-opt.html https://ocaml.xyz/book/core-opt.html, which includes how core functions are implemented in C, and how OpenMP is utilised etc. Besides, Owl relies on certain libraries such as OpenBLAS and FFTPack for the performance of key operations in e.g. linear algebra.
- fluffything 6y agoThis chapter showcases the problem perfectly. It gives scientists a lot of information about how to perform low level optimization on code, e.g., if your code is "slow", use SIMD, OpenMP, BLAS, or do this or that trick. But it does not provide the scientist with even the most basic tools to answer the question: "Is my code fast or slow?" (i.e. should I optimize it at all?), much less "_Why_ is it slow, and what's the best way to address that?" (e.g. if it is slow because its using 100% of the peak FLOPs of the CPU, but your hardware has a GPU, so you end up with 1% total FLOP utilization, then none of the "tricks" there will help). It also completely avoids the issue that, in practice, a O(N) algorithm beats a OpenMP+SIMD-optimized O(N^2/p) algorithm pretty much all the time. The chapter kind of assumes that scientists OCaml code will be slow, and gives them a "bag of tricks" that they can try to make it faster. So we end up with the irony of a book on scientific computing that completely ignores the scientific method.
- rich_sasha 6y agoI'm beginning to think of learning either OCaml or F# for data sciency-kind of things. Any points of comparison between those? Library ecosystem seems better on F#, but I must admit I'm somewhat wary of the behemoth that is .NET . What else should I consider?
- cultus 6y agoScala might be something worth looking at. There's a lot of libraries on the JVM, but sparse linear algebra is still kind of a wasteland compared to Python or Numpy. I wish someone would just make some good bindings to Eigen or something for the JVM.
- deleted 6y ago[deleted]
- rich_sasha 6y agoI quite like the idea of Scala, and JVM is a plus for me too, but as you say, various bits seem to be curiously missing. I'm not saying F# or OCaml are better, just... saying.
- devmunchies 6y agoData science and interactive programming are actually the main focus of the next release of f#. Not saying it’s bad now, but Microsoft wants it to be a strong option in data workflows. https://devblogs.microsoft.com/dotnet/announcing-f-5-preview-1/ https://devblogs.microsoft.com/dotnet/announcing-f-5-preview... I had originally been learning ocaml but switched to f# because it has much better tooling and more uses (e.g. better web server support since I use .net libs)
- rich_sasha 6y agoInteresting, thank you! From just preliminary research, F# seems both loved by its devs, but also found to be a bit of an unloved child on a sidetrack - that was probably another reason I hesitated in getting started. Do you think that is justified?
- cultus 6y agoIn my opinion, static languages don't bring a whole lot to the table with numerical math. There's not many types for one. You basically just use matrices and vectors of floats most of the time. What would really be a bigger deal is some limited dependent typing to avoid errors from mismatched array sizes. Until then though, Julia is a bit more practical choice for me.
- yawaramin 6y agoOCaml can provide that: https://github.com/Octachron/tensority https://github.com/Octachron/tensority
- octachron 6y agoSlap (https://github.com/akabe/slap https://github.com/akabe/slap) is a more serious attempt. Tensority is still a prototype library that ventures very far into statistical safety, probably at a quite steep cost in usability.
- srean 6y agoThese old papers might pique your interest Shape in Computing [https://dl.acm.org/doi/10.1145/234528.234749 https://dl.acm.org/doi/10.1145/234528.234749 ] A Semantics for Shape [https://www.sciencedirect.com/science/article/pii/0167642395000151 https://www.sciencedirect.com/science/article/pii/0167642395... ] https://www.semanticscholar.org/paper/The-FISh-language-definition-Jay/cc414b98ba5e65f47c18470a5739195f4a63a209 https://www.semanticscholar.org/paper/The-FISh-language-defi... https://link.springer.com/article/10.1007/s100090050037 https://link.springer.com/article/10.1007/s100090050037 The page for FiSH used to be online. I cant find it now.
- cole-k 6y agoThis sort of thing (if I understand the abstracts correctly) can also be done with dependent types: https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/aplicative.pdf https://www.cs.ox.ac.uk/people/jeremy.gibbons/publications/a.... This paper was my introduction to dependent typing, so if you have a little Haskell background, you should be able to grok its gist too.
- vmchale 6y agoGood stuff! Love OCaml and functional programming in the spotlight.
- ihnorton 6y ago> Indexing, slicing, and broadcasting are three fundamental functions to manipulate multidimensional arrays. ... > Indexing and slicing is arguably the most important function in any numerical library. These statements are undoubtedly true. The first question any practitioner familiar with other systems will ask is: what does basic arithmetic, array manipulation, and linear algebra look like? But from what I can tell on a very quick skim, that question isn't really answered until the section starting with these sentences, on page 123. I've noticed this situation every time I look at the Owl docs webpage too, FWIW (have not looked recently though). I understand the need to be perceived as fully-capable for modern tasks -- and that's fine for a 2-4 page set of teaser examples up front -- but I think this book would become much more approachable if the basic mechanics of doing math were presented first.
- srean 6y agoIf anyone wants a quick access to see how slicing and indexing is done https://ocaml.xyz/book/slicing.html https://ocaml.xyz/book/slicing.html An older thread on HN on slicing https://news.ycombinator.com/item?id=20457884 https://news.ycombinator.com/item?id=20457884
- srean 6y agoOwl, the array/scientific computing library that this book introduces, has been discussed on HN before. Dropping those links here, in case people are curious about the comments. https://news.ycombinator.com/item?id=20449595 https://news.ycombinator.com/item?id=20449595 https://news.ycombinator.com/item?id=14751236 https://news.ycombinator.com/item?id=14751236
- UncleOxidant 6y agoLove OCaml. One of my favorite languages. But I'm using Julia for this kind of thing, it just seems much better suited. Likewise, I wouldn't use Julia to write a programming language implementation, OCaml is much better suited for that.
- dunefox 6y agoIt's the same for me, Julia is perfect for scientific programming - it has basically replaced Python, it's even starting to be used at my work. I don't think Ocaml has its place in this domain.
- jpz 6y agoI think the work the author has done is amazing. I Just looking at the commit history - the core contributor has definitely been super busy. https://github.com/owlbarn/owl/graphs/contributors https://github.com/owlbarn/owl/graphs/contributors
- smabie 6y agoAs someone that likes and uses OCaml a lot but uses Julia for scientific computing, it's not worth it, just use Julia. The Julia code is going to be shorter, faster, and more elegant. The libraries will be sooo much better. The static typing of OCaml doesn't really help in this area and sometimes actually hurts (statically typed DataFrames don't work so well).
- gnufx 6y agoWhat makes Julia code shorter and more elegant generally? I strongly disagree with static typing not being useful in scientific computing. Most of that I see isn't dealing with data frames, though I've seen confusion with types in those with R users.
- smabie 6y agoI would say the biggest difference is broadcasting. So let's say I have an array of returns, r: r = [0.0001; -0.00002...] I might be interested to find the cumulative returns: cumprod(1 .+ r) .- 1 Or just apply a function f to it: f.(r) In Ocaml I can't vectorize any notation: List.map ~f:(fun x -> x - 1)) @@ List.cumprod(List.map r ~f:((+) 1)) List.map r ~f:f Julia allows you to write code in a very vectorized, array language style. OCaml does not. This is big big issue, imo. Also with multi-dimensional arrays and slice notation, Julia is just very convenient for working with higher dimensional data. OCaml, to put it mildly, is not very good at this. Scientific computing and array languages go hand in hand. Also the lack of polymorphic functions is a big problem in OCaml. For example it would be impossible to define an addition function in OCaml that transparently worked with arrays: 1 .+ [1; 2; 3] == [2; 3; 4] 1+1 == 2 [1; 2; 3] .+ 1 == [2; 3; 4] [1; 2; 3] .+ [1; 2; 3] == [2; 4; 6] Julia makes this easy. A real array language like kdb+/q or J is even better. This is great for linear algebra (matrices and tensors). You can write very math-like equations using very high level functions. With OCaml you will always be burdened with the nitty gritty of mapping and folding over the lists/arrays.
- mseri 6y agoowl ndarrays come with operations that support broadcasting though