Let’s now introduce this thing called linear algebra. When I first heard about it, it seemed a bit intimidating, but it turned out not to be that scary, in my opinion. You know, sometimes when we encounter a new subject, we develop preconceived notions, and I was no different. But once I started, I realized that it’s just a more mathematical and organized version of concepts we already know.
First, let’s talk about what is at its core. Scalars! These are numbers as you know them. Quantities that can be expressed with a single number, such as length, temperature, weight. For example, an apple weighs 500 grams, or a room’s temperature is 22 degrees Celsius. Simple, right? Most quantities we use in daily life are scalars.
But things get more interesting here. Vectors! Vectors are quantities that not only have a magnitude but also a direction. Sometimes, when describing a movement, saying “move 5 kilometers” isn’t enough, you need to specify “move 5 kilometers north.” The “north” part indicates the vector’s direction. You can think of it like an arrow. The length of the arrow shows the magnitude, and the arrow’s direction indicates the direction.
So, why do we work with vectors? For example, think of a car’s speed. Saying only 100 km/h isn’t enough. We need to know which direction the car is heading. That speed becomes a vector. Or think of a force. When you push somewhere, not only how much force you apply, but also the direction matters. These are often encountered in engineering, physics, and computer graphics. In game development, for example, vectors determine character movements and camera directions. Sometimes, characters jump from their position, and vectors are involved in these calculations.
What can we do with vectors? One basic operation is addition. Adding two vectors means connecting the end of one vector to the start of the other, thus creating a new vector. You can think of it like this: you start from point A to point B, then from point B to point C. The total movement from A to C can be seen as a single move. This new movement is the sum of the two vectors. Nice, isn’t it?
There’s also scalar multiplication. It means changing the magnitude of a vector by multiplying it with a number. If the scalar is positive, the direction stays the same, but the magnitude changes. If negative, both the magnitude and the direction invert. You can imagine it like a rubber band; stretching it makes it longer, pulling it in the opposite direction shortens it and reverses the direction.
By the way, expressing vectors in a coordinate system is very helpful. Usually, we use 2D (x, y) or 3D (x, y, z) systems. When we write a vector this way, we see its components. For example, the vector (3, 4) represents moving 3 units along the x-axis and 4 units along the y-axis. Like when we say “take 3 steps right, 4 steps up” on a map, these are the components of a vector.
Let’s do a simple example. Suppose we have two vectors: Vector A = (2, 1) and Vector B = (1, 3). Adding them component-wise: A + B = (2+1, 1+3) = (3, 4).
Now, let’s try scalar multiplication. Say Vector C = (2, 5) and multiply by 3. Then, 3 * C = (6, 15). The magnitude is tripled, and the direction stays the same. What if we multiply by -2? Let Vector D = (1, -2). -2 * D = (-2, 4). As you see, both the magnitude and the direction change. Strange, isn’t it?
These basic operations are the building blocks of linear algebra. Understanding these concepts makes it much easier to grasp more complex topics later. It might seem difficult at first, but with practice, you’ll get used to it. Of course, there is also how these are implemented in code. For example, in Python, the numpy library simplifies vector operations greatly. But to reinforce this knowledge, let’s look at a simple C# example.
Imagine a scenario: you’re programming a game, and you’re setting up the movement of a character. There’s an initial position, and you add a movement vector to find the new position. Let’s write a simple vector class:
<pre>public class Vector {
public double X { get; set; }
public double Y { get; set; }
public Vector(double x, double y) {
X = x;
Y = y;
}
// Vector Addition
public static Vector operator +(Vector v1, Vector v2) {
return new Vector(v1.X + v2.X, v1.Y + v2.Y);
}
// Scalar Multiplication
public static Vector operator *(double scalar, Vector v) {
return new Vector(scalar * v.X, scalar * v.Y);
}
public override string ToString() {
return $”({X}, {Y})”;
}
} </pre>
Now, let’s use this class in an example. Suppose our character starts at (5, 3) and we want to move it by the vector (2, -1). Then, we multiply this movement vector by 3 to apply a stronger effect. Here, it gets a bit tricky. First, define a movement vector, then add it to the initial position. Then multiply that movement vector by a scalar and add again.
The initial incorrect code was:
<pre>Vektor startingPosition = new Vektor(5, 3);
Vektor movement = new Vektor(2, -1);
Vektor newPosition = startingPosition + movement; // Result (7, 2)
// Now, applying three times the movement
Vektor effect = 3 * movement;
// Adding this effect directly to newPosition // This causes an error: the effect is added twice rather than calculating combined movement
Vektor finalPosition = newPosition + effect; // Incorrect output
Console.WriteLine($”Final Position: {finalPosition}”); // Will output (10, -1), which is not the expected result. </pre>
The mistake here is that the movement was added twice, once from the initial position and movement, then again from the joint movement effects. What I actually wanted was to consider the total of the initial movement and the scaled effect, starting from the initial position. So, the correct approach would be:
<pre>Vektor startingPosition = new Vektor(5, 3);
Vektor initialMovement = new Vektor(2, -1);
Vektor effect = 3 * initialMovement; // Three times the initial movement
Vektor totalMovement = initialMovement + effect; // Combined movement
Vektor finalPosition = startingPosition + totalMovement;
Console.WriteLine($”Final Position: {finalPosition}”); // Should output (13, -1) </pre>
Now, it’s much more logical. We’ve summed up all the movements, starting from the initial position, applying the total movement. That is, (5, 3) + (2, -1) + (6, -3) = (13, -1). Honestly, these simple mistakes can be quite frustrating initially, but I suppose this is part of the learning process.
By the way, to illustrate the importance of vectors, consider a 3D modeling software where you rotate an object. Rotation is essentially a series of vector transformations. Or in a physics simulation, tracking particles’ movement… All of these rely on this vector mathematics. I read somewhere that nowadays, even many graphics cards contain millions of vector processors. Think about what powerful tools those are!
In conclusion, scalars and vectors are the fundamental building blocks of linear algebra. These seemingly simple concepts are encountered in many fields of mathematics and computer science. Dedicating some time at the beginning to understand these fundamentals will significantly ease your later work. Like they say, a solid foundation makes everything more durable, and this is no different.