Linear Functions

Learn about linear functions using different examples.

Linear algebra uses linear functions to realistically approximate real-world functions. Furthermore, linear functions also reduce the complexity of a system.

Linear function definition

If x\bold{x} and y\bold{y} are any two objects(scalars, vectors, matrices, tensors) and α\alpha and β\beta are scalars, a linear function can be defined using two properties:

Additivity: f(x+y)=f(x)+f(y)f(\bold{x}+\bold{y}) = f(\bold{x})+f(\bold{y})

Homogeneity: f(αx)=αf(x)f(\alpha \bold{x}) = \alpha f(\bold{x})

We can combine these two properties in a single statement and say that a linear function must fulfill the following:

f(αx+βy)=αf(x)+βf(y)f(\alpha \bold{x}+\beta \bold{y}) = \alpha f(\bold{x})+\beta f(\bold{y})

Furthermore, we can extend these properties to more than two objects and scalars.

The additivity property above says that whether we apply a linear function to a sum of objects or apply that linear function to the objects individually, subsequently adding them gives us the same result. Meanwhile, the homogeneity property says that we may first multiply an object with a scalar, then apply a linear function, or otherwise apply that linear function to an object and then multiply it with the scalar. Either way, our result is the same.

Simulation for verifying linearity

The code below tries to test the linearity of the two functions, f1 and f2. Although the code that implements f1 and f2 is available, the test works even if we only have access to f1 and f2 but not their implementation. The code randomly generates two inputs, x1 and x2, and then tests the additivity and homogeneity properties on 10000 randomly generated values of alpha and beta.

Press + to interact
import numpy as np
def f1(x):
return 2*x
def f2(x):
return x**2
#Initializing variables
x1, x2, isf1Linear, isf2Linear = np.random.randn(), np.random.randn(), True, True
for i in range(10000):
#Generating random scalars for linearity test
alpha, beta = np.random.randn(), np.random.randn()
#Verifying additivity and homogeneity for f1
if f1(alpha*x1+beta*x2) != alpha*f1(x1)+beta*f1(x2):
isf1Linear = False
#Verifying additivity and homogeneity for f2
if f2(alpha*x1+beta*x2) != alpha*f2(x1)+beta*f2(x2):
isf2Linear = False
print('In 10000 random tries: f1 is ', ['Non Linear', 'Linear'][isf1Linear],' and f2 is ', ['Non Linear', 'Linear'][isf2Linear])

Although the code above seems to work well for the f1 and f2 functions, this approach is generally slow and occasionally impossible (when we can’t loop over all real scalars, for example). Luckily, we have analytical ways to do this as well when the definition of a function is available. Let’s go through some examples to learn how to verify a function’s linearity.

Examples

In this section, we present multiple examples to clarify the concept of a linear function. Although the linear function may be applied to any object, we use vectors in our examples. A vector is an ordered array of numbers, which is usually expressed as v=[321]\bold{v}=\begin{bmatrix}3\\2\\1\end{bmatrix} We may also express the same vector as v=(3,2,1)\bold{v}=(3, 2, 1)

Linearity verification

Let’s solve a problem. You’re given the function f(v)=(v1+v2,v2+v3,2v3)f(\bold{v})=(v_1+v_2, v_2+v_3, 2v_3) ...

Access this course and 1400+ top-rated courses and projects.