TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Type-checked matrix operations in Rust

79 pointsby jcla1almost 10 years ago

4 comments

jarrettcalmost 10 years ago
Lately I&#x27;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&lt;Newtons&gt; or Vec3&lt;Meters&gt;.<p>Dividing and multiplying units statically is where I&#x27;ve had trouble so far. I think I&#x27;ve found a way, but it would depend on negative trait bounds, as discussed here:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rfcs&#x2F;issues&#x2F;1053" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;rust-lang&#x2F;rfcs&#x2F;issues&#x2F;1053</a><p>Ideally, I&#x27;d like to be able to do something like this:<p><pre><code> let a: Scalar&lt;Joules&gt; = Scalar::new(2.0); let b: Scalar&lt;Seconds&gt; = Scalar::new(3.0); let c: Scalar&lt;Watts&gt; = a &#x2F; b; &#x2F;&#x2F; Watts is a type synonym for Over&lt;Joules, Seconds&gt;. &#x2F;&#x2F; Other derived units would use Times&lt;U, V&gt;. E.g.: type Pascals = Times&lt;Newton, Times&lt;Meter, Meter&gt;&gt;.</code></pre>
评论 #10029297 未加载
评论 #10027910 未加载
andreaferrettialmost 10 years ago
For an example of a similar technique in Nim: <a href="https:&#x2F;&#x2F;github.com&#x2F;unicredit&#x2F;linear-algebra&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;unicredit&#x2F;linear-algebra&#x2F;</a><p>If I understand correctly the author remark about the lack of Rust support for value parameters, it seems that it should be equivalent to the Nim feature (static[int]) that has allowed me to write type-checked matrix operations there
anon4almost 10 years ago
Ok this is seriously amazing (coming from a C++ guy). However, one thing I don&#x27;t like with putting matrix dimensions in templates is that then you can&#x27;t construct them at runtime. I do understand the obvious - that you can&#x27;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 compilation errors. So you could write, e.g.<p><pre><code> template &lt;int m, int n, int l&gt; Matrix&lt;m, l&gt; mul(Matrix&lt;m, n&gt; lhs, Matrix &lt;n, l&gt; rhs) {...impl...} </code></pre> And then be able to call it like<p><pre><code> int m, n, k, l = ... read from file or whatever Matrix &lt;m, n&gt; m1 = ...; Matrix &lt;k, l&gt; m2 = ...; try { Matrix &lt;int o, int p&gt; m3 = m1 * m2; &#x2F;&#x2F; ^ code compiled dynamically &#x2F;&#x2F; or loaded from cache, based on &#x2F;&#x2F; runtime types of m1 and m2. &#x2F;&#x2F; o and p set to the result &#x2F;&#x2F; of type inference. &#x2F;&#x2F; I could imagine even having &#x2F;&#x2F; specialised versions with inline &#x2F;&#x2F; assembly for specific dimensions. } catch (DynamicCompilationException e) { print(&quot;dimensions not compatible&quot;); } </code></pre> Java could be it, if it had reified generics. You&#x27;d create an implementation of Num, or load one from cache, then instantiate the template and attempt to call the mul function.<p>Or you could abuse the invoke dynamic feature - create specialised functions matrixMultiply$m$n$l and classes Matrix$m$n from some other templating language as needed, then do an invoke dynamic based on type. But this would be very cumbersome to use, I think.
评论 #10029705 未加载
评论 #10028976 未加载
评论 #10029266 未加载
adwhitalmost 10 years ago
For assorted type system&#x2F;PhantomData madness, see also:<p><a href="http:&#x2F;&#x2F;maniagnosis.crsr.net&#x2F;2015&#x2F;07&#x2F;abstracted-algebra-in-rust.html" rel="nofollow">http:&#x2F;&#x2F;maniagnosis.crsr.net&#x2F;2015&#x2F;07&#x2F;abstracted-algebra-in-ru...</a><p><a href="http:&#x2F;&#x2F;maniagnosis.crsr.net&#x2F;2015&#x2F;07&#x2F;more-abstracted-algebra-in-rust.html" rel="nofollow">http:&#x2F;&#x2F;maniagnosis.crsr.net&#x2F;2015&#x2F;07&#x2F;more-abstracted-algebra-...</a>