r/ProgrammingLanguages • • Jan 24 '26

Discussion Why don't any programming languages have vec3, mat4 or quaternions built in?

Shader languages always do, and they are just heaven to work with. And tasty tasty swizzles, vector.xz = color.rb it's just lovely. Not needing any libraries or operator overloading you know? Are there any big reasons?

110 Upvotes

132 comments sorted by

141

u/pheristhoilynenysis Jan 24 '26 edited Jan 24 '26

Probably because their use cases are too niche to be built into most of the languages. The fact that a feature is nice to have doesn't mean that it should be there. That's exactly what we have libraries for - you choose what you want to use. Remember that each feature for language is another component for language author to maintain which might be relatively high cost in comparison to gains. Edit: typos

18

u/Luroqa Jan 24 '26

Yeah lol now when I think of it, I'm just too focused on gamedev programming, where they are used constantly. It's not like web devs feel like they are missing. Very reasonable thank you

3

u/Tamschi_ Jan 24 '26

Vectors would be nice to have in JS sometimes, to be honest.

Probably would have to be constrained to 2 32-bit floats though, as you only get limited space for value types in dynamic languages.

4

u/shponglespore Jan 24 '26

I've used complex numbers in JS before for geometry calculations. For my purposes it didn't matter that the performance was garbage, but the lack of operator overloading made the code very ugly.

51

u/HaMMeReD Jan 24 '26

Vectors/Matrix math is low hanging fruit for low level optimizations at the compiler. I.e. SIMD instructions and vectorization.

The fact is that the language having the semantics means it can optimize more effectively at the hardware level to use those features.

If it's just an add-on to the language, the semantics are not there for the compiler that it can use the enhanced instructions available to operate on those types.

31

u/alphaglosined Jan 24 '26

Optimising backends do a lot of pattern matching to determine what optimisations to do.
And yes that also includes things like matrix multiplication.

You as a compiler developer working on a frontend, are unlikely to beat what backend developers can do here. They have target-specific information, and they are constantly changing the backend to improve codegen.

For mathematics like this, the context is in the expression/IR, you don't need to attribute anything to it for the backend to recognise it.

Library types that represent mathematics like linear algebra are very likely to optimise to pretty close to the maximum for the target these days. Auto vectorisation has improved a lot over the last 15 years and is no longer awful.

I have been able to in the past, match the codegen of handwritten assembly and intrinsics by simply writing D code more or less naively.

24

u/WittyStick Jan 24 '26

Auto-vec still has a lot of missed opportunities and some of these could be resolved if the programmer were explicit about using vectors rather than arrays of scalars. Obviously we can use intrinsics manually, but the APIs are typically awful and non-portable.

GCC & Clang have a nice approach where we can attach __attribute__((__vector_size__(N))) to the usual "primitives" and the arithmetic, logic and comparison operators are promoted to work element-wise on the vector, using SIMD instructions. It's more portable than using platform-specific intrinsics, but as you state, that's due to a huge effort by the compiler developers to make that possible.

IMO the main reason to include vectors as part of your standard library is to prevent a proliferation of (potentially incompatible) implementations, but having built-in language support opens up more opportunities to optimize.

12

u/dcpugalaxy Jan 24 '26

This is very limited. Compilers can't assume your float[4][4] is properly aligned for example. They often can't deal with arrays etc very well because of aliasing. Autovectorisation is very fickle and easy to break.

All serious mathematical libraries have handwritten SIMD. Often assembly but intrinsics are used too.

5

u/alphaglosined Jan 24 '26

Alignment of arrays is a big issue, since you can't really guarantee that at a language level.

But static arrays are different, those can be aligned correctly by the language, with the assumption that their container is also aligned correctly. At least in D this isn't really an issue.

There is restrict annotation that handles aliasing in terms of optimization but that isn't backed by any guarantees.

As for mathematical libraries, things like ffmpeg, they want to codegen out to be the best for a given target, regardless of compiler flags. They use cpuid to pick the function to call. They are a very different beast from something like linear algebra for general usage.

But yeah, if you care about exact codegen, I'd recommend intrinsics instead of writing assembly itself. Intrinsics inline as they don't conflict with other optimisations.

→ More replies (1)

12

u/HaMMeReD Jan 24 '26

While I'm in no way downplaying the amount of work that goes into backends, semantics is semantics. Having it up front, clearly defined, takes out most of the guess work from the equation.

→ More replies (5)

11

u/shponglespore Jan 24 '26

This isn't really true. Compilers for languages like Rust and C++ give you access to things like SIMD instructions through intrinsic functions. Nothing stops library authors from using them to write high performance math operations the compiler can understand and optimize in context. This is a big part of why C++ is so popular for math-heavy code like game engines.

Also, in practical terms, most of the optimization that needs to happen is inside the individual operations, which is why Python and Julia—which basically can't optimize user code at all—are also popular choices for heavy math. Python in particular is a great example of a language that is good for high performance math even though it only offers advanced math operators through 3rd party libraries like numpy.

3

u/SwingOutStateMachine Jan 24 '26

I think you've misunderstood the other commentor. The point is that language semantics for vector/matrix maths gives compiler authors the ability to compile more code to vectorised or SIMD instructions.

3

u/ExplodingStrawHat Jan 24 '26

At least in my experience using rust, different libraries use different linear algebra dependencies, requiring additional runtime conversion. Not only this, but a library disabling the simd feature flag for the linear algebra dependency (macroquad did this last I checked in order to share a layout with the GPU) suddenly turns off simd for the rest of your codebase (at least, assuming it also relies on the same linear algebra dependency). This leads to even more confusion when hot reloading code where the types on the two sides are the same, yet only one side depends on the package that disables SIMD. 

Having rewritten said project in Odin (which has built in supports for vectors/matrices/complex numbers/quaternions, together with standard library modules for interacting with glsl/hlsl), I've encountered zero of the issues above.

→ More replies (2)

23

u/backwrds Jan 24 '26

shader languages are programming languages.

6

u/Luroqa Jan 24 '26

Well of course ya, but I mean regular old general purpose ones

1

u/arthurno1 Jan 26 '26

Because you would have hundreds of various mathematical and other objects if they included every possible conceivable object one can need. Watch the video I linked to in the other comment, and you will understand it.

2

u/arthurno1 Jan 26 '26

They are DSLs, not general purpose languages. DSLs usually have domain specific types and objects as intrinsic for convenience. General purpose languages usually give you a limited set of intrinsic objects, which you can combine in some way to produce more domain-specific objects.

68

u/baby_shoGGoth_zsgg Jan 24 '26 edited Jan 24 '26

Odin does. Even supports swizzling on xyzw/rgba.

edit: odin specifically implements these because the author finds it convenient and that it’s 90% of why people want operator overloading (and 100% of the valid reasons for it) so rather than add operator overloading to the language we just get these concepts fully supported.

11

u/lfnoise Jan 25 '26

Yet if you want rationals, interval arithmetic, sparse matrices, signals as values, symbolic algebra expressions, and Odin doesn’t provide them, then you still want operator overloading.

0

u/baby_shoGGoth_zsgg Jan 25 '26

Have you seen what happens when programmers are given unfettered access to operator overloading? it’s… not fucking pretty.

You can always just use functions :) Or try to convince gingerbill that your pet use case should be elevated to supported in the compiler. Trying to argue for generic operator overloading probably won’t get you far, though.

9

u/tmzem Jan 27 '26

"Language feature X bad because developers can abuse X". Yeah. Ban all language features.

I'll never understand this logic. Programmers should have training, like any other profession. Teach people how to do it right. Programming languages should have better resources with concrete examples on how to use (and not use) their features, beyond just the toy examples provided in tutorials.

7

u/ExplodingStrawHat Jan 25 '26

Have you seen what happens when programmers are given unfettered access to operator overloading? it’s… not fucking pretty.

One could argue the same about manual memory management, yet Odin's stance on that is the opposite of the stance it has regarding operator overloading.

21

u/Potterrrrrrrr Jan 24 '26

Some do, e.g C3 has built in support for vectors and swizzling

31

u/JeffB1517 Jan 24 '26

MATLAB, Julia, HLSL (Cg, Asymptote. VEX all have 3D vectors built in. Programming languages choose a limited set of primitives for what they consider common use cases. More niche languages have more niche primitives. Same reason most programming languages don't have filenames as a primitive, but shell scripting languages do. Or some 4GL languages like MUMPS have key-value hierarchical database interactions as a primitive.

7

u/shponglespore Jan 24 '26

What shell scripting languages are you thinking of? All the ones I've used (Unix shells and PowesScript) just treat file names as strings. Even build systems like Make, Ninja, and Bazel lack a special data type for file names.

1

u/JeffB1517 Jan 25 '26

No they don't. They treat them as objects with properties

ls d/e is effectively a polymorphic function of d which is then polymorphic on e which is treated as an object i.e. d->e.ls in OO syntax. Note that e is a file ls polymorphism will handle this. Similarly cat d/e now treats e as a file and d->e.cat. It has elegant failure if e is a directory. Etc... that is way more than strings.

More importantly though, than all that power is the fact that they are a default argument type, a primitive, for countless operations that want a file name, not merely a string.

→ More replies (2)

6

u/ir_dan Jan 24 '26

Godot's scripting language, GDScript, has them for obvious reasons.

3

u/honey-pony Jan 25 '26

Strangely enough, GDScript's vectors feel (at least to me) more like a library-defined vector type than a language-defined one. In particular they don't support swizzling or certain GLSL-style constructors (e.g. Vector3(Vector2, float), which is occasionally inconvenient when writing scripts.

11

u/Timberfist Jan 24 '26

vec3, mat4 and quaternions is what shader languages are for but, in the context of general purpose languages, they’re pretty niche. Niche is what libraries are for.

3

u/chibuku_chauya Jan 24 '26

Sometimes these specialist features are added to general purpose languages after the fact and end up not integrating well with existing features.

For example, C99 gained several language (as opposed to library) features like built-in complex number support, variable-length arrays (VLAs), restricted pointers, etc., as a way to compete with languages like Fortran for the use-cases that language was built from the get-go to directly support.

But much of the complex number machinery and VLA support was later marked as optional for implementers; excluding them still made your implementation conformant to the language standard. They were either removed or modified in subsequent versions of C due to the complexities and infelicities they introduced into the language in hindsight, as well as the fact that not all implementations supported them many years after their introduction. As such they could not be relied upon to be universally available.

All this is to say that some specialised features are better off provided as libraries in languages not designed around the use cases that would justify native support for those features. Implementers of general purpose languages often optimise for the most common use-cases reflected by most users of the language.

Whereas if you’re a more specialised language then you optimise for features that make common operations specific to your domain easier and more convenient to accomplish. Every engineering decision has tradeoffs.

3

u/WittyStick Jan 24 '26

Kawa has built-in Quaternion support, which extends Scheme's numerical tower (which ends at Complex).

3

u/ivancea Jan 24 '26

For every question like "why don't languages have X", always answer with a "why should it?". No, quaternions have nothing to do with programming. They're not inherently related in any way, so just use a library, which is the same.

Shader languages are inherently related to those concepts, so obviously that have them. But they're not generalist languages. They're for shaders, for GPU execution. Very niche kind of language

3

u/igors84 Jan 24 '26

Odin lang sort of has them.

3

u/Lubricus2 Jan 24 '26

Odin has most of that stuff.
quaternions, Swizzling, vector math all out of the box.
One reason is that the language lacks operator overloading so you can't make the datatypes yourself in an ergonomic way.

3

u/binarycow Jan 25 '26

.NET / C# has support for all of that.

4

u/[deleted] Jan 24 '26

To all of the people who are saying "it's too niche," I'd like to point out that every GUI library could benefit from using vectors to bundle up coordinates. Imagine if the web's DOM API were written to use vectors instead of "top" and "left" everywhere. I believe this would simplify a lot of GUI programming to a certain degree.

2

u/B_A_Skeptic Jan 24 '26

In CSS, you can specify things numerically, i.e. number of pixels, percent of size, etc. But you do not really do vector math for web design.

4

u/dontyougetsoupedyet Jan 25 '26

Sort of. You can transform by whatever arbitrary transformation matrix you want in CSS, if you choose to.

2

u/Digot Jan 24 '26

C# does have such types built-in with SIMD support

2

u/tb5841 Jan 25 '26

GDScript does. Because it's a language designed for game programming, there are used pretty often.

2

u/koflerdavid Jan 25 '26 edited Jan 26 '26

They are nic[h]e, and if the language gives you all the language features to build it yourself then there is no reason to include it in the language's core.

2

u/marshaharsha Jan 25 '26

A few people have suggested that there are multiple ways to lay out a vector that’s only a few numbers long. What are these ways, what are their uses, and are there few enough that a language could support all of them, with built-in conversions? I am especially interested in u/ExplodingStrawHat’s experience with a scenario in which two layouts were required for compatibility with SIMD and GPU. 

1

u/ExplodingStrawHat Jan 25 '26

I think the main difference comes from alignment of the elements (at least that seems to be the main difference in the case of glam, the library macroquad depends on). From their readme:

The Vec3A, Vec4, Quat, Mat2, Mat3A, Mat4, Affine2 and Affine3A types use 128-bit wide SIMD vector types for storage on x86, x86_64 and wasm32 architectures. As a result, these types are all 16 byte aligned and depending on the size of the type or the type's members, they may contain internal padding.

When it comes to matrices, one also has the column-major vs row-major distinction.

2

u/spvky_io Jan 25 '26

Odin lang does, but that's largely due to the fact that it supports array programming and Vec3 is just an alias for [3]f32

2

u/stumpychubbins Feb 18 '26

What's the problem with operator overloading? I think it got a bad reputation because of how much it's abused in C++, but that's an issue with the culture around the language. Rust has operator overloading and I never see that kind of overloading abuse in Rust, same for Haskell. I've seen some pretty bad uses in Python, but very rarely. It's easier for shader languages to build the types into the language because they're not designed for general-purpose use, so a full general-purpose system isn't necessary. Besides, GPUs have a native concept of vectors so you'd need an even more complex generalised system in order to make sure that the library type maps to the GPU's native type. I don't think that non-GPU languages need them, unless they specifically don't want to implement operator overloading. Odin is designed for games, graphics and maths but considers overloading a non-feature for simplicity reasons, and as a result they have native vector and matrix types. More complex languages don't need them.

6

u/huywall Jan 24 '26

because they are implementable by yourself

17

u/WittyStick Jan 24 '26

They are, but when they're not provided as part of the language, every library implements its own. If you depend on LibA and LibB, which provide their own vectors, quaternions etc, then you now need to convert between their representations, which with any luck is a simple no-op cast, but only if they're equivalent. We end up having to write a third implementation which abstracts over LibA and LibB's implementation so that we have a common interface to the types, and depending on the language this may add overhead.

So these should at least be part of a standard library so that there's not a proliferation of potentially incompatible implementations.

But it's still preferable to implement vectors as part of the language if you want to best leverage the capabilities of a modern CPU. These are effectively "primitives" like int or float on virtually every commodity CPU from the past decade (or two).

13

u/Shurane Jan 24 '26

I kinda wish developers understood that by leaving something unimplemented means you might end up having to support many different versions of it in third party code. It's fine to have it unimplemented, but would make sense to at least have input/expectation on how it should be designed so you don't have to go around correcting or supporting 3+ versions.

I think this is definitely the issue for Pairs/Tuples/Functors in languages that don't have them. Java for example doesn't have a default Pair class (so you end up with options like Apache Commons' versions, Map.Entry, etc etc). And it didn't have a Tuple class either until Java Records.

I think there was a similar issue in JavaScript with callbacks/promises and module systems until they got relatively standardized.

3

u/Inconstant_Moo 🧿 Pipefish Jan 24 '26

But adding a core feature places a permanent maintenance burden on the development team. (And will do so even if almost everyone hates their API and uses a 3PL instead.) So you have to pick and choose.

You'll be pleased to know my language has tuples that you construct like "foo", 2, true, and also a pair type that you construct like "foo"::42. But there's also a long list of things it will never have.

2

u/Weak-Doughnut5502 Jan 25 '26

They are, but when they're not provided as part of the language, every library implements its own.

This is a library ecosystem and culture issue.

Open source central library ecosystems and build systems have improved over the decades.  NIH was a big problem back in the early days of C or even earlier Java.

But it's possible to have a library ecosystem where there's an 90% chance libA and libB use the vec3 implementation from some widely used libC instead of rolling their own.

2

u/protestor Jan 24 '26

Swizzles are not

4

u/initial-algebra Jan 24 '26

There's nothing special about swizzles. They're just getters/setters or properties (or lenses!), and it's easy to generate their implementations with a macro.

1

u/protestor Jan 24 '26

What's special is the syntax, something.xy and something.yx having the same fields but reversed (in such a way that something.xy = .. and something.yx .. work).

There's some languages that can implement them though: you need a method to resolve any missing field. Such method would receive "xy" or "yx" as a string, and decide what to do with this. I think Javascript is one of those languages (with proxy obects). Ruby probably is too.

However swizzles are often used in performance-sensitive code, and it's challenging to make the above implementation fast, unless the code that decides what to do with arbitrary field names runs at compile time

3

u/initial-algebra Jan 24 '26

For a vector with a finite number of components, and assuming you don't allow duplicate components (for which assignment would be nonsense anyway), you can enumerate all possible swizzles up front and generate a getter/setter or whatever for each of them.

→ More replies (1)

4

u/Kaisha001 Jan 24 '26

They should! I mean we all make our own but still, it'd be nice if that sort of stuff was just default.

2

u/slicxx Jan 24 '26

I see two very clear problems, first is minor but the second one is i think of more importance.

When we implement vec3 everywhere, people are going to ask for more niche things. I used vec4 and vec5 too in my job, but somewhere is a limit in terms of what a language/library should support.

But what do these types actually do? Exactly nothing. It all boils down to the functions we want to apply to those types. Add? Substract? Easy in 17 dimensions, but how about complex vector funktions? Do we want to do this inplace or return a copy? Will the memory layout be efficient for operation XYZ? Once you put your hands on a vec3/mat type, i bet efficiency is of importance, so it's best to have all the tooling for your use case in one place, without bursting the scope of standard lib.

Some languages offer more, some offer literally nothing and just provide base tooling, because you can build anything anyways with it. Of course it's also design philosophy, but types with 3 or more dimensions are more likely than not an overkill for built-ins

5

u/dcpugalaxy Jan 24 '26

vec2, vec3, and vec4 are reasonable to support. Anything else is unreasonably niche. They aren't useless but anything outside 2/3/4 is niche in comparison. Seems like a pretty clear natural limit to me. I would hardly say vec4/mat4 is niche. They're widely used in computer graphics.

I think the best reason to have them built in is that they're natural vocabulary types. Lots of different libraries and types of libraries want to use and talk about vectors. Having a separate type in each library which are all essentially the same kinda sucks.

5

u/WittyStick Jan 24 '26

I don't think it's unreasonably niche to support what the CPU supports: vec2, vec4, vec8 - for doubles and 64-bit integers and for the smaller types: vec16, vec32 and vec64.

A use case besides geometry is to optimize data structures - and the larger the vector the better.

vec64 of uint8 for example can be used to significantly improve the performance of decoding UTF-8, which is used everywhere - far from niche - but most of the software running on your computer is probably decoding one byte at a time because it's dependant on some low-level string APIs which don't use SIMD.

2

u/[deleted] Jan 25 '26

At this point you're just asking for standard library support for SIMD vectors, which is much more reasonable than spatial linear algebra vectors in my opinion.

3

u/WittyStick Jan 25 '26

Well yeah, if you have SIMD availability provided by the language you can implement linear algebra libraries efficiently.

But I would still recommend providing the basic types for linear algebra in the standard library if only to prevent a proliferation of competing implementations. Advanced linear algebra libraries can build atop the standard lib. I wouldn't expect language authors to implement the equivalent of eigen in their standard library.

→ More replies (9)

1

u/dcpugalaxy Jan 24 '26

I think this is less necessary because these aren't vocabulary types.

1

u/slicxx Jan 24 '26

You got a point with "natural limits" being vec4/mat4 BUT computer graphics underlined my point in being something special and therefore these types may best placed in their own libs. Yes, it's a pain to not have any standards over every package, but a few libs are just "the standard".

Think about the highly optimized package numpy in python. It's not part of the standard library but basically everyone installs numpy whenever they setup their environment because it's so widely used.

A different example would be golang - nothing that is somewhat special is built in and standard packages are also very slim. And then again comes the memory layout problem, vec4 is simple, but a very "standard" implementation will hardly be usable in computer graphics, high performance computing or anything that's outside of standard calculations.

This definitely sucks like you're saying, but i think it would be worse if you have to deal with "standard" types and library types representing the same thing. In many cases standards wouldn't be any more useful than e.g. DTOs, which are already kinda painful in many languages if you can't autogenerate them.

7

u/Kaisha001 Jan 24 '26

The same could be said of any standard library... or even any language. Perhaps we should all be programming in asm?

At some point if something is common and stable enough (ie. there is really only one proper implementation most of the time), then there doesn't seem to be any reason not to support it.

The idea that we can't support vec3/4 because someone might want vec19 seems silly to me.

1

u/arthurno1 Jan 26 '26

The idea that we can't support vec3/4 because someone might want vec19 seems silly to me.

That is exactly the case because game dev is a niche. A chemist would want their own types, and a mechanical engineer would want their own types, and a natural language researcher would want theirs, and everyone would want their own types. Guess what? Sometimes, these types would collide with each other and be similar but slightly different. Whom will you cater?

The answer is exactly: none. You give them tooling so they can introduce they own types as they need them. This talk is by the one of the most famous language designers or our time and deals exactly with these questions. Highly suggested.

1

u/Kaisha001 Jan 26 '26

Whom will you cater?

Well first vec3/4 are not 'gamedev' types, they are linear algebra types. Linear algebra is found everywhere. There's a reason reason why CPUs/GPUs have native linear algebra types and instructions is that is it near ubiquitous across computing.

The answer is exactly: none.

Linear algebra is used far more than any other field of mathematics apart from basic arithmetic. x86 has SSE/AVX, arm has NEON/SVE/SVE2, PowerPC has VMX/VSX, even the old MIPs line of chips and tiny MCUs like the ESP32 S3 (Tensilica Xtensa vector instructions). They're all basically the same thing, because linear algebra is near universal.

Even GPUs, which are used for FAR MORE than just graphics, and their associated languages, which have wildly different architectures, still have vec3/4 ect... as their base types.

For something so universal and stable, I don't see any good reason for languages to not support them.

→ More replies (3)

1

u/slicxx Jan 24 '26

I see where you're going, but more often than not, things are semi-built-in. The title asks why it's not built-in. I assume my definition of built-in is too literal, meaning usable without import. But standard libs (or standardized libs) do often exist and have big functionality.

I think a great example is the C language here. They want to stay minimal and portable, and of course stay close to the machine, only one hop away from ASM. No dynamic vectors exist, because growth handling, memory and error management, performance and so on do matter but in different ways for different use cases. But a simple "type" is one makro or a few lines of header files.

And for the n-D types? Different types (int, float32/64..) can be easily implemented but they would at least need different paddings inside to be efficient in cache.. A Vector3 is used in a widespread use, but in no way are they universal. They are highly domain specific, so let's just keep it in the math library, game engine or shading language which will hopefully be "optimally" compiled for the use case. And these environments all come with the type.

All about hitting the sweet spot between implementing nothing and delivering it all.

3

u/Kaisha001 Jan 24 '26

I assume my definition of built-in is too literal, meaning usable without import.

Fair enough, it was rather ambiguous. I assumed standard library type stuff.

And for the n-D types? Different types (int, float32/64..) can be easily implemented but they would at least need different paddings inside to be efficient in cache.. A Vector3 is used in a widespread use, but in no way are they universal.

There are SSE/AVX types on near every platform now. It's not an impossible task.

2

u/slicxx Jan 24 '26

You got a very strong point with SSE/AVX, so the reason might just be legacy for many languages nowadays, but it still makes sense for some hard typed languages to focus on the minimum.

Interesting discussion, now i wonder where the limits are and why we aren't pushing further in this direction, especially since a lot of things can be hidden from the user anyways or rather aggressive, maybe be compiled out if it isn't necessary.

→ More replies (3)

2

u/WittyStick Jan 24 '26

The language only needs to provide sufficient support to leverage the capabilities of the CPU. It doesn't need to handle every case.

That would typically mean providing vectors of each power of 2 up to at least 256-bits (though 512-bits preferable, and 1024-bits is an option), for each of the primitive types: signed/unsigned int8, int16, int32, int64 and single/double floats.

vec3 might also be worthy of inclusion due to widespread use, and could be implemented as vec4 with masking.

For obscure cases like vec5 it obviously makes no sense to include in a general purpose language. It can make sense to expose masking intrinsics so we can use a vec8 with mask 00011111, and wrap this in an API for handling vec5.

3

u/AsIAm New Kind of Paper Jan 24 '26

Why to stop at vec3 or mat4 when you can have tensors of any rank and functions on them are differentiable?

https://mlajtos.github.io/fluent/?code=RG9jdW1lbnRhdGlvbg

1

u/PurpleYoshiEgg Jan 24 '26

Depending on what you consider the language, some Smalltalk systems have quaternions.

1

u/Prestigious_Boat_386 Jan 24 '26

Idk importing them in your base environment has always worked for me for working with geometry

1

u/SFJulie Jan 24 '26

I have implemented vecn on top of mutable mappings (dict) for python making dict behave as an indefinite size vector that haz dot/cos/+/-/*/div as normal behaviour following strictly linear algebra. And the fun is your vector can be fractal without losing linear algebra property.

I agree with OP that these kinds of features are nice to have in the language, as well as with those saying "it can be implemented (not that easily)". :D

https://pypi.org/project/archery/

1

u/WittyStick Jan 24 '26

Are there any big reasons?

A big reason is they're not standardized in C.

Most languages are written with C, or written in another language which itself was written in C. Since there's no standard API for handling vectors, quaternions in C, they tend to get overlooked.

If the C standard provided a <stdvec.h> and <quaternion.h>, language authors would more likely provide the means to expose the capabilities in their own languages.

The C committee are reluctant to include such things as they consider the embedded use-case as equally important to the PC or server use case, and low power processors lack SIMD support. They could at least provide such thing as optional as part of an annex in the standard - if only to provide a portable way to utilize SIMD instructions, as currently each architecture has it's own set of incompatible intrinsics.

But there's also the issue of an explosion of types and functions due to C's lack of proper generic support. Suppose we want to support at least int8_t, uint8_t, int16_t, uint16_t, int32_t, uint32_t, int64_t, uint64_t, float, double, bool, and we want vectors of sizes 2, 3, 4 for each as a minimum: That's 33 types which each need a bunch of operations on them. If we consider the lowest common denominators: +, -, *, /, %, <, >, <=, >=, ==, !=, <<, >>, &, |, ^, that's 16 ops * 33 types = 528 operations.

That's before we include complex numbers, quaternions, decimal floating point, etc. into the equation.

So the main reason they're not included elsewhere is simply because it's a huge effort to implement, and language authors are preoccupied with other concerns.

1

u/zweiler1 🔥 Flint Jan 24 '26

Mine has vector types where every operation with them is always a SIMD operation (here) and it supports swizzling natively through the concept of groups (here and here).

I mean my language is far from being mainstream lol, bit i always hated when languages do not have a concept of vector types and every library kinda creates it's own version and then you end up with 3 different struct types for the same thing (Vec3, Vector3, V3, Vector3f, Vector3i and what other naming things exist, idk).

I don't have matrices though, since they really are too nieche i would say, but vectors? I would say they are relatively common in comparison.

1

u/OwlProfessional1185 Jan 24 '26

Swizzling is really cool, but if you're implementing a general-purpose programming language, what do they mean more broadly?

Presumably, you don't just want to special-case it just for vectors and stuff, so you need to think about what it looks like and whether it makes sense for anything else.

2

u/[deleted] Jan 24 '26

You can do swizzling that is much more useful and general than typical swizzling with an extended property access syntax:

x.y // access y
x.(y, z) // create a tuple of x.y and x.z
x.[y, z] // create an array of x.y and x.z
x.{y, z} // create an object that has x.y and x.z as self-named properties
x.(x + y * z) // do some math with the properties (and methods) of x
x.do
   ...
end // you get the idea

This acts sort of like a scoped import. This syntax doesn't have the downside of limiting swizzleable properties to one-letter variables.

2

u/OwlProfessional1185 Jan 24 '26

What about something like:

x.(y, z) = w.(y, z)

1

u/cmontella 🤖 mech-lang Jan 24 '26

Some do — Mech does!

1

u/lightmatter501 Jan 24 '26

C and C++? Clang and GCC have vector types.

The special syntax for accesses is something that’s really annoying to special case in a general purpose language so you won’t see it done much. Instead you do either masked ops if you can lean on modern simd or some fun interleaving if not for that specific case.

1

u/[deleted] Jan 24 '26

Firstly, it is very easy to implement these data structures. Secondly, there are some programming languages or proof assistants (Maybe Lean) specifically for mathematics have such 3-vector, 4-matrix or quaternions built in as their primitive types.

3

u/[deleted] Jan 24 '26

It's easy to implement, yes. But then you end up with a bunch of libraries that all rely on different implementations, and you now need an explosion of conversion functions to make them all talk to each other, which costs CPU cycles, and programmer productivity.

1

u/Compux72 Jan 24 '26

It can be a library lol

2

u/ExplodingStrawHat Jan 24 '26

So can multi-threading primitives, yet a lot of languages still include them.

1

u/Compux72 Jan 24 '26

That doesn’t justify anything 

1

u/[deleted] Jan 25 '26

Because it's trivial to implement as a library.

1

u/teo-tsirpanis Jan 25 '26

.NET has all three, and also fp16, and (u)int128. They work very much like the other numeric types, just without built-in language support. Which is not very important if you ask me.

1

u/KC918273645 Jan 25 '26

Try C3 language.

1

u/uglycaca123 Jan 25 '26

odin does, actually

1

u/arthurno1 Jan 26 '26

Watch the entire video. The questipn ypu asked is used as an example, not exsctly but cery cöosely. He also gives a direct answer why specialized types are not part of Java, and usually, any other general-purpose programming language.

1

u/SnooCalculations7417 Jan 26 '26

Making your own data primitives is software 101. You can make your own modules you don't have to pip/npm/cargo install them all 

1

u/catbrane Jan 27 '26

g++ and clang have these all built in, and generate nice SIMD for you automatically. For example:

/* A vector of four floats.
 */
typedef float v4f __attribute__((vector_size(4 * sizeof(float)), aligned(16)));

Now you can do:

v4f A = { 0.0, 1.0, 0.0, 0.0 };
A *= 1.1;

etc. etc. It generates a no-SIMD fallback as well and uses it at runtime if the host CPU is missing the necessary instructions.

However, it has some serious problems: it's fragile, not very portable, dependant on the compiler version, will not get the best from your vector units, etc. etc.

For high performance portable SIMD you're probably better off with something like Highway:

https://github.com/google/highway

ie. do it in a library, not in the language.

1

u/Critical_Ad_8455 Jan 28 '26

gdscript has vec3s, and rust has them in the form of tuples

1

u/kwan_e tonal-lang Feb 04 '26

Because: what's wrong with libraries or overloading? That is the big reason. If a library can take care of it, why go through the process of trying to get something into a language, perhaps through a standardization process?

Yes, libraries are a pain because of dependency management. But it's a small cost to overcome, compared to standardization.

Shader languages can have them because they were a new language with no such frictions. The proprietary shader languages will also have fewer frictions if their owners add new features. The open shader languages will have similar frictions to getting new additions into the language.

1

u/JollyJuniper1993 Feb 18 '26

Wolfram Mathematica has pretty much anything niche, including a quaternions package in its standard library. Sadly it’s paid software though.

-2

u/PurepointDog Jan 24 '26

Python has a built-in json library. I avoid it like the plague - every json library does it better.

i can think of more similar examples. Sometimes though, having a separate dedicated team maintain things like graphics libs, linear algebra tools, etc. is way better than baking it into a language statically and with strong backwards-compat requirements.

Have you ever tried using C++'s std datetime stuff? It's a strong example of why sometimes baking stuff into a language is a bad idea.

5

u/dcpugalaxy Jan 24 '26

Python's json library is good. I can't think of any reason why you wouldn't want to use it and having to download and install a third party library for a little script using one of the most common data formats would be super annoying.

1

u/PurepointDog Jan 25 '26

Serializing dates is the big one.

Its speed is attrocious.

I'm not talking about simple scripts - I'm talking about web apps and data pipelines, where JSON is a hot-path performance limitter.

0

u/dcpugalaxy Jan 25 '26

Okay but if you are doing something where performance really matters you likely aren't using Python in the first place. If you are writing a "high performance data pipeline" then you might want to use a special library but the Python standard library is general-purpose. It doesn't need to be "blazing fast" at serialising dates.

1

u/PurepointDog Jan 26 '26

I didn't say it was high-performance. I just said that I don't have time to wait for the built-in JSON library. A 20x speedup is serious - it's the difference between waiting 10 minutes or 30 seconds. Rinse and repeat 3 times during debugging, and the benefit is obvious.

Modern Python data pipelines make heavy use of tools like Polars which move data processing out of Python, and just use it for controlling the steps basically.

Python becomes somewhat of a project manager, yelling demands to Rust, where the compute and memory management exists. JSON is one of the rare places where it make sense to wrangle data in Python, as long as it's fast.

-3

u/wahnsinnwanscene Jan 24 '26

A list of 4 vectors is vec4, etc.