Live data from Hacker News

Type-checked matrix operations in Rust

jadpole.github.io

11–20 of 20 posts

Re: Type-checked matrix operations in Rust

#11
post #10

Ok this is seriously amazing (coming from a C++ guy). However, one thing I don't like with putting matrix dimensions in templates is that then you can't construct them at runtime. I do understand the obvious - that you can't have both static type checks on all operations and runtime-determined matrix sizes. Though I would kill for a language which would specialise my code at runtime and throw an exception for compila…

statically checked, but still runtime-determined, matrix sizes can be done in Scala. Doing specialized implementations would require bytecode magic, though.

  case class Dim(size: Int)

  trait Matrix[X 

Re: Type-checked matrix operations in Rust

#12
post #8
post #6

Earlier quoted context omitted.

Canonicalization is tough, and requires you to define some common ordering on your units. Systems will use an array of unit powers, so that if the array were defined as , then acceleration would be and watts would be . Addition and subtraction require that your arrays are equal, and multiplication and division are pairwise additive/subtractive.

Will the Rust compiler ever understand numbers in types? I.e. will the numbers ever be more than just part of the string that is the type name? If not, then I don't know how possible powers will be. Maybe the solution would be to define some types for commonly-used powers, e.g. PowN4, PowN3, PowN2, PowN1, Pow2, Pow3, Pow4. Users who needed higher powers could define those types themselves, I guess.

We do desire type-level integers, yes. There hasn't been an RFC yet, though.

Re: Type-checked matrix operations in Rust

#13
post #9
post #6

Earlier quoted context omitted.

Canonicalization is tough, and requires you to define some common ordering on your units. Systems will use an array of unit powers, so that if the array were defined as , then acceleration would be and watts would be . Addition and subtraction require that your arrays are equal, and multiplication and division are pairwise additive/subtractive.

Is it tough? There are only seven fundamental units. https://en.wikipedia.org/wiki/SI_base_unit Just represent every unit in terms of them then it's good. One problem with the array you came up is that the units are not orthogonal, since Joules = Newtons * Meters.

Actually, there's more "kinds" of units: radians (versus degrees) and steradians come to mind.

Re: Type-checked matrix operations in Rust

#14
post #10

Ok this is seriously amazing (coming from a C++ guy). However, one thing I don't like with putting matrix dimensions in templates is that then you can't construct them at runtime. I do understand the obvious - that you can't have both static type checks on all operations and runtime-determined matrix sizes. Though I would kill for a language which would specialise my code at runtime and throw an exception for compila…

I believe dependent typing is what you're after. Have you looked at Idris?

http://www.idris-lang.org/example/

Re: Type-checked matrix operations in Rust

#15
post #2

Lately I've been playing around with static dimensional analysis in Rust. The overall idea is similar: Use PhantomData to add a type parameter and define an empty struct for each unit. So you might end up with, say, Scalar or Vec3 . Dividing and multiplying units statically is where I've had trouble so far. I think I've found a way, but it would depend on negative trait bounds, as discussed here: https://github.com/r…

If you aren't aware of it, the dimensional library for Haskell does this. https://hackage.haskell.org/package/dimensional

Re: Type-checked matrix operations in Rust

#16
post #15
post #2

Lately I've been playing around with static dimensional analysis in Rust. The overall idea is similar: Use PhantomData to add a type parameter and define an empty struct for each unit. So you might end up with, say, Scalar or Vec3 . Dividing and multiplying units statically is where I've had trouble so far. I think I've found a way, but it would depend on negative trait bounds, as discussed here: https://github.com/r…

If you aren't aware of it, the dimensional library for Haskell does this. https://hackage.haskell.org/package/dimensional

Yep! That was my inspiration. I'd love to be able to do this in Rust. Soon, hopefully!

Re: Type-checked matrix operations in Rust

#17
post #9
post #6

Earlier quoted context omitted.

Canonicalization is tough, and requires you to define some common ordering on your units. Systems will use an array of unit powers, so that if the array were defined as , then acceleration would be and watts would be . Addition and subtraction require that your arrays are equal, and multiplication and division are pairwise additive/subtractive.

Is it tough? There are only seven fundamental units. https://en.wikipedia.org/wiki/SI_base_unit Just represent every unit in terms of them then it's good. One problem with the array you came up is that the units are not orthogonal, since Joules = Newtons * Meters.

Sorry! I never really spent much time in the physical sciences, so I didn't know that. You would obviously not want to pick Joules as a fundamental unit.

Re: Type-checked matrix operations in Rust

#18
post #15

Earlier quoted context omitted.

If you aren't aware of it, the dimensional library for Haskell does this. https://hackage.haskell.org/package/dimensional

Yep! That was my inspiration. I'd love to be able to do this in Rust. Soon, hopefully!

You should take a look at its source code. You'll see that the path you're taking is not the path that library takes.

Re: Type-checked matrix operations in Rust

#19
post #10

Ok this is seriously amazing (coming from a C++ guy). However, one thing I don't like with putting matrix dimensions in templates is that then you can't construct them at runtime. I do understand the obvious - that you can't have both static type checks on all operations and runtime-determined matrix sizes. Though I would kill for a language which would specialise my code at runtime and throw an exception for compila…

Julia does this at runtime. You can create an arbitrarily sized and typed array and call a function. If no implementation exists specialized for that type, it will automatically compile one using LLVM.

(The function that creates the array will be slower since it's not type-stable, but it can generate a type-stable function that's fast.)

Re: Type-checked matrix operations in Rust

#20
post #9

Earlier quoted context omitted.

Is it tough? There are only seven fundamental units. https://en.wikipedia.org/wiki/SI_base_unit Just represent every unit in terms of them then it's good. One problem with the array you came up is that the units are not orthogonal, since Joules = Newtons * Meters.

Actually, there's more "kinds" of units: radians (versus degrees) and steradians come to mind.

These are dimensionless derived units in SI equivalence (respectively m/m and m2/m2)
Post reply on HN