Ask coding questions

← Back to all posts
How often do you use vectorized operations?
xxpertHacker (861)

Vectorized operations are operations that apply to vectors.

Pseudo code:

vector<integer> x = [ 1 2 3 4 5 ];
x = x + 3;

# x : [ 4 5 6 7 8 ]

They're often used in languages like R, that provide first-class support for them.

They're also often used in multi-dimentional programs, ex: one might use a vector of 4 integers to represent the four corners of a square, to move it up, they perform a vectorized addition on all corners at once.

And in languages that don't provide first class support for them, ex: JavaScript, one might find someone making something like this:

class Vector {
    constructor(x, y, z) {
        this.x = x;
        this.y = y;
        this.z = z;
    add(rhs) {
        return new Vector(
            this.x + rhs.x,
            this.y + rhs.y
            this.z + rhs.z
// or

[ 1, 2, 3, 4, 5 ].map(i => i + 3);

But I've only seen that ^ done in code that creates and manipulates 2D/3D objects.

I'd like to survey Replers on the following 4 things:

  • have you had any use for vectorized math outside of graphics-related code?
  • do you commonly use arbitrarily long vectors, or do you more commonly use vectors that have a staticially known length? (ex: that JS class could be seen as a vector of 3 elements)
  • how often have you needed to perform vectorized division? (ex: [ 2 4 8 ] / 2 = [ 1 2 4 ])
  • ^ but for division remainder? (ex: [ 1 2 3 ] % 2 = [ 1 0 1 ])
    • For the last two, can you provide actual code on when you have, or what situations it's useful for?
programmeruser (576)

Not really, since I don't do a lot of graphics and math related code

xxpertHacker (861)

@programmeruser Got any friends who write math-heavy code?

programmeruser (576)

@xxpertHacker I'm more interested in 3D graphics now, so
1. Not really
2. Vectors with a static length
3. I haven't really written any code that uses vector division
4. Why

xxpertHacker (861)

@programmeruser 4) because, as I presumed, vector division is rare, as opposed to multiplication, addition, subtraction, exponentation, etc.

If anyone really used division, I'd have liked to see where in the world someone would need it, or where it could genuinely prove useful.

xxpertHacker (861)

@programmeruser Btw, it's actually kinda funny, since I was able to rewrite the code from your recent post to use vector division...

But in that particular case, it could be * 0.5, eliminating the division entirely.

tussiez (1515)
  1. Not really
  2. If I read the post correctly, yes
  3. A lot
  4. Not really

Finally - I do an unhealthy amount of vector math in SortaCraft (voxel engine/data processing)
Do not look at the code, thank you
Also, here is the module I wrote to make this easier: Methods (ignore WASM)

xxpertHacker (861)

Ohh, oops, I should clarify, I meant "arbitrarily long vectors" vs "staticially known length," which one did you end up with more often?

Also, we'd best to move it elsewhere if you want to discuss it, but
a) Baconman is making that code execute a lot slower by using Wasm.
b) I'm legitametely making a small vector lib, so I wanted to know what people want and often use,, but it's too bleeding-edge for use today.

tussiez (1515)

Ah, statically known length.
Yee, he noted that LOL

Coder100 (17018)

Well vectors are usually used for when you need to abstract well, a vector. Vectors are pertaining to 2D and 3D objects in themselves so this is in a way a really dumb question.

Anyways maybe matrix addition?

xxpertHacker (861)

@Coder100 I've updated the question, to those who've used matrix/vector arithmetic, I honestly only care about the last three bullet points, see the bottom of the post.

And yes, this was a dumb question, and I couldn't make it not sound dumb.

Matrix operations might've fit better here, a lot better.
In what situations have you found yourself using them?

xxpertHacker (861)

@Coder100 Lmao; math vector =/= programming vector =/= engineering vector

Coder100 (17018)

in your use case, it appears you were using math vector <1, 2> @xxpertHacker