0Pricing
Mojo Academy · Lesson

Element-Wise SIMD Math

Add and multiply whole vectors at once.

Element-Wise SIMD Math is a free Mojo Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Mojo Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Math on Whole Vectors

The best part of SIMD is that arithmetic works on the entire vector at once. One operation touches every lane together.

Adding Two Vectors

Add two SIMD values and each lane is added to its matching lane. This is called element-wise addition.

var a = SIMD[DType.int32, 4](1, 2, 3, 4)
var b = SIMD[DType.int32, 4](10, 20, 30, 40)
var c = a + b

Lane Lines Up with Lane

In a + b, lane 0 adds to lane 0 and lane 1 adds to lane 1. The lanes never mix, they stay aligned.

print(c[0])

Multiplying Vectors

Multiplication is element-wise too. Each lane of one vector is multiplied by the same lane of the other vector.

var p = a * b

Subtract and Divide

Subtraction and division follow the same rule. Every lane is handled independently and in parallel across the pack.

var d = b - a

Scalar Meets Vector

Add a single number to a vector and Mojo applies it to every lane. The scalar spreads across all positions at once.

var bumped = a + 100

Widths Must Match

To combine two SIMD values they need the same DType and the same width. Mismatched lane counts will not line up.

Comparisons Go Lane by Lane

Comparing two vectors gives a SIMD of Bools, one True or False per lane. Each lane reports its own result.

var mask = a > SIMD[DType.int32, 4](2)

Reducing to One Number

Sometimes you want a single answer. reduce_add sums all lanes into one scalar, perfect for totals.

var total = a.reduce_add()
print(total)

Built-in Math Functions

Standard math like sqrt also works lane-wise on SIMD, computing the result for every element in one call.

from math import sqrt
var roots = sqrt(a.cast[DType.float32]())

Same Code, Many Results

Writing a + b looks like scalar math but produces many results at once. That readability with speed is Mojo's promise.

Quick Check

You compute a + b where both are SIMD[DType.int32, 4]. How is the addition done?

Recap

Element-wise SIMD math runs +, -, *, and / on every lane at once, scalars spread to all lanes, and reduce_add folds a vector into one number. ✨

Frequently asked questions

Is the “Element-Wise SIMD Math” lesson free?

Yes — the full text of “Element-Wise SIMD Math” is free to read here on the web, and the Mojo Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Mojo Academy course, upgrade to CoddyKit PRO.

What will I learn in “Element-Wise SIMD Math”?

Add and multiply whole vectors at once. You practise Mojo Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Mojo Academy?

No prior experience is required. Mojo Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Element-Wise SIMD Math” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Mojo Academy lesson?

Yes. Every Mojo Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Meet the SIMD Type
  2. Element-Wise SIMD Math
  3. Choosing a SIMD Width
  4. Vectorizing a Loop
← Back to Mojo Academy