concept scalar in category python

appears as: scalar, scalars, The scalars, scalars, scalar
Math for Programmers: 3D graphics, machine learning, and simulations with Python

This is an excerpt from Manning's book Math for Programmers: 3D graphics, machine learning, and simulations with Python.

The operation of multiplying a vector by a number is called scalar multiplication. When working with vectors, ordinary numbers are often called scalars. It’s also an appropriate term because the effect of this operation is scaling the target vector by the given factor. It doesn’t matter if the scalar is a whole number; we can easily draw a vector that is 2.5 times the length of another (figure 2.17).

class Vec3(): 
      def __init__(self,x,y,z): #1 
          self.x = x 
          self.y = y 
          self.z = z 
      def add(self, other): 
          return Vec3(self.x + other.x, self.y + other.y, self.z + other.z) 
      def scale(self, scalar): 
          return Vec3(scalar * self.x, scalar * self.y, scalar * self.z) 
      def __eq__(self,other): 
          return (self.x == other.x 
                and self.y == other.y 
                and self.z == other.z) 
      def __add__(self, other): 
          return self.add(other) 
      def __mul__(self, scalar): 
          return self.scale(scalar) 
      def __rmul__(self,scalar): 
          return self.scale(scalar) 
      def __repr__(self): 
          return "Vec3({},{},{})".format(self.x,self.y, self.z)
  • Multiplying vectors by several scalars should be the same as multiplying by all the scalars at once. If a and b are scalars and v is a vector, then a (b v) should be the same as (a b) v.