Solving simultaneous equations

## Contents

* 1 Introduction
* 2 Solving 2 by 3
* 2.1 Reversing the process
* 2.1.1 Linear Function
* 2.1.2 Wrong assumptions
* 3 Solving 3 by 4
* 3.1 Reversing the Process
* 3.1.1 Quadratic function
* 3.1.2 Wrong assumptions
* 4 Solving M by (M+1)
* 4.1 Sphere
* 4.2 Quintic function
* 4.3 Resistive network
* 4.3.1 Solve for 8 Branch Currents
* 4.3.2 Make all Branch Currents positive
* 4.3.3 Compute R
* 5 Solving big matrices
* 5.1 Testing results
* 5.2 Cubic Resistive Network
* 5.2.1 Solve for 12 Branch Currents
* 5.2.1.1 Create matrix 12 by 13
* 5.2.1.2 Try again
* 5.2.2 Compute R
* 5.2.3 Review
* 5.2.4 Checking
* 5.2.4.1 Examples of current nodes
* 5.2.4.2 Examples of voltage loops
* 5.2.4.3 Python code

## Introduction[edit | edit source]

The word "simultaneous" implies that two or more equations exist in the same context at the same time.

For example:

3 x + 2 y − 13 = 0 … ( 1 ) {\displaystyle 3x+2y-13=0\ \dots \ (1)}

2 x − y − 4 = 0 … ( 2 ) {\displaystyle 2x-y-4=0\ \dots \ (2)}

Equations  ( 1 ) {\displaystyle (1)} and  ( 2 ) {\displaystyle (2)} are 2 simultaneous equations.

When we see two simultaneous equations, it's natural to ask the question: What are the values of  x {\displaystyle x} and  y {\displaystyle y} that satisfy both equations  ( 1 ) {\displaystyle (1)} and  ( 2 ) {\displaystyle (2)} ?

To calculate  ( x , y ) {\displaystyle (x,y)} common to both  ( 1 ) {\displaystyle (1)} and  ( 2 ) {\displaystyle (2)} :

( 2 ) ∗ 2 … 4 x − 2 y − 8 = 0 … ( 3 ) {\displaystyle (2)*2\ \dots \ 4x-2y-8=0\ \dots \ (3)}

( 3 ) + ( 1 ) … 7 x − 21 = 0 ; x = 3. {\displaystyle (3)+(1)\ \dots \ 7x-21=0;\ x=3.}

Substitute  3 {\displaystyle 3} for  x {\displaystyle x} in  ( 2 ) : 2 ( 3 ) − y − 4 = 0 ; y = 2. {\displaystyle (2):\ 2(3)-y-4=0;\ y=2.}

The values  ( x , y ) = ( 3 , 2 ) {\displaystyle (x,y)=(3,2)} satisfy both equations  ( 1 ) {\displaystyle (1)} and  ( 2 ) . {\displaystyle (2).}

In cartesian coordinate geometry of 2 dimensions, point  ( 3 , 2 ) {\displaystyle (3,2)} is the point of intersection of two lines  ( 1 ) {\displaystyle (1)} and  ( 2 ) . {\displaystyle (2).} See Figure 1 below.

If the 2 lines are parallel, there is no point of intersection. See Figure 2 below.

The trivial solution  ( 0 , 0 ) {\displaystyle (0,0)} is a valid solution. See Figure 3 below.

* Figure 1. 2 lines that intersect at point (3,2).

* Figure 2. When 2 lines are parallel, there is no point of intersection.

* Figure 3. The trivial solution x=y=0 is a valid solution.

The information contained in equations  ( 1 ) {\displaystyle (1)} and  ( 2 ) {\displaystyle (2)} may constitute a matrix:

[3,  2, -13],
[2, -1,  -4]

In fact, in the python programming language, the following statement is valid code:

data = [
[3,  2, -13],
[2, -1,  -4]

For there to be a point of intersection, `data` must be valid.

Here are examples of invalid data:

data = [
[3,  2, -13],  # Both lines are parallel.
[6,  4,  -5]   # See Figure 2 above.

data = [
[3,  2, -13],
[0,  0,  -4] # Empty row, representing equation 0x + 0y - 4 = 0.

data = [
[3,  0, -13], # Empty column representing 2 parallel lines:
[2,  0,  -4]  # 3x = 13 and 2x = 4.

The matrix above with name "data" is described as matrix "2 by 3," meaning that it contains 2 rows with 3 columns or 3 members per row.

## Solving 2 by 3[edit | edit source]

Below is python code containing function `solve2by3 (input).` The function accepts as input a matrix like `data` above.

After the code are examples of invocation with both good data and bad data.

import sys

def solve2by3 (input) :
'''
input represents system:
a1*x + b1*y + c1 = 0
a2*x + b2*y + c2 = 0

input = (
(a1,b1,c1),
(a2,b2,c2),

To invoke:
x,y = solve2by3 (input)
Function solve2by3 () may return None.

input is not changed by this function.
'''
def solve2by3_(input) :
row1,row2 = input
a1,b1,c1 = row1
a2,b2,c2 = row2

if ( (a1 == b1 == 0) or (a2 == b2 == 0) ) :
print ('solve2by3() : empty row in input.')
return None
if (a1 == a2 == 0) or (b1 == b2 == 0) :
print ('solve2by3() : empty column in input.')
return None
if a1*b2 == a2*b1 :
print ('solve2by3() : parallel input.')
return None
if c1 == c2 == 0 :
# Column is empty. However, trivial solution is valid.
return 0,0

# Arrange input, if necessary, to ensure that a1 is non-zero.
if a1 : pass
else :
row1,row2 = row2,row1
a1,b1,c1 = row1
a2,b2,c2 = row2

# The matrix is:
# a1, b1, c1 with a1 non-zero.
# a2, b2, c2
# Process row2, if necessary,  so that a2 becomes 0.

if a2 :
if a2 == a1 :
b2_,c2_ =  b1-b2, c1-c2
else:
L1 = [a2*v for v in row1]
L2 = [a1*v for v in row2]
zero,b2_,c2_ = [ L1[p]-L2[p] for p in (0,1,2) ]
if zero :
# Theoretically this should not happen.
# It may happen if, for example, limited precision
# of floats causes small rounding errors.
print ('solve2by3() : Internal error 1.')
return None
else : b2_,c2_ =  b2,c2

# The matrix is:
# a1, b1,  c1  with a1 non-zero
# 0,  b2_, c2_ and a2 zero.

# 0*x + b2_*y + c2_ = 0
# b2_*y = - c2_
y = -c2_/b2_

# a1*x + b1*y + c1 = 0
# a1*x = -( b1*y + c1 )
x = -(c1 + b1*y)/a1 # Here is why a1 must be non-zero.

return x,y

# Following is python code used for trapping errors
# that may occur in function solve2by3_() above.
output = error = ''
try : output = solve2by3_(input)
except : error = sys.exc_info()[:2]
if (output == None) or error :
if error :
print ('solve2by3() :')
print ('  error detected in processing.')
print ('   ', error)
print ('  input =')
print ('   ', input[0])
print ('   ', input[1])
return None
return output

Invocation with good data:

output = solve2by3([
[3,  2, -13],
[2, -1,  -4]

print (output)

(3.0, 2.0)

Invocation with bad or invalid data:

output = solve2by3([
[3,  2, -13],  # Both lines are parallel.
[6,  4,  -5]   # See Figure 2 above.

print (output)

solve2by3() : parallel input.
input =
[3, 2, -13]
[6, 4, -5]
None

output = solve2by3([
[3,  2, -13, 7], # Input matrix not exactly 2 by 3.
[6,  4,  -5]

print (output)

solve2by3() :
error detected in processing.
(<class 'ValueError'>, ValueError('too many values to unpack (expected 3)',))
input =
[3, 2, -13, 7]
[6, 4, -5]
None

output = solve2by3([
[3,  0, 2],
[6,  0, 4]

print (output)

solve2by3() : empty column in input.
input =
[3, 0, 2]
[6, 0, 4]
None

### Reversing the process[edit | edit source]

#### Linear Function[edit | edit source]

Figure 1. Diagram of line defined by 2 points.
2 points are:  ( − 5 , 4 ) , ( 3 , 1 ) . {\displaystyle (-5,4),(3,1).}
Equation of line is:  3 x + 8 y − 17 = 0. {\displaystyle 3x+8y-17=0.}

Examples above calculate the point of intersection of 2 lines.

An example of reversing the process is this task:

Given two points  ( x 1 , y 1 ) , ( x 2 , y 2 ) {\displaystyle (x_{1},y_{1}),(x_{2},y_{2})} calculate equation of line containing these 2 points.

Equation of line is of format  A x + B y + C = 0 , {\displaystyle Ax+By+C=0,}

For example:  3 x + 2 y + 10 = 0 … ( 1 ) . {\displaystyle 3x+2y+10=0\ \dots \ (1).}

Equation  ( 1 ) {\displaystyle (1)} can be expressed as  6 x + 4 y + 20 = 0 {\displaystyle 6x+4y+20=0} or  − 9 x − 6 y − 30 = 0. {\displaystyle -9x-6y-30=0.}

Also, there appear to be 3 unknowns, but only two sets of values are required to define the line.

Assume  C = 1 {\displaystyle C=1} and the 2 relevant equations are:

A x 1 + B y 1 + 1 = 0 {\displaystyle Ax_{1}+By_{1}+1=0}

A x 2 + B y 2 + 1 = 0 {\displaystyle Ax_{2}+By_{2}+1=0}

To solve for  A , B : {\displaystyle A,B:}

x 1 A + y 1 B + 1 = 0 {\displaystyle x_{1}A+y_{1}B+1=0}

x 2 A + y 2 B + 1 = 0 {\displaystyle x_{2}A+y_{2}B+1=0}

Suppose the line is defined by points  ( 3 , 1 ) , ( − 5 , 4 ) : {\displaystyle (3,1),(-5,4):}

# python code:
input = [
[3,1,1],
[-5,4,1],

result = solve2by3 (input)
print (result)

(-0.17647058823529413, -0.47058823529411764)

Desired equation is:  − 0.17647058823529413 ⋅ x − 0.47058823529411764 ⋅ y + 1 = 0 … ( 2 ) . {\displaystyle -0.17647058823529413\cdot x-0.47058823529411764\cdot y+1=0\ \dots \ (2).}

To improve appearance of  ( 2 ) : {\displaystyle (2):}

# python code:

print (A/B)

0.37500000000000006

Let  A , B = 3 , 8 {\displaystyle A,B=3,8} and check:

# python code

A,B = 3,8

p1 = x1,y1 = 3,1
p2 = x2,y2 = -5,4

for v in (p1,p2) :
x,y = v
C = -(A*x + B*y)
s1 = 'C'
print (s1, eval(s1))

C -17
C -17

Desired equation  ( 2 ) {\displaystyle (2)} becomes  3 x + 8 y − 17 = 0 … ( 3 ) . {\displaystyle 3x+8y-17=0\ \dots \ (3).}

#### Wrong assumptions[edit | edit source]

Figure 2. Diagram of line defined by 2 points and passing through origin.
2 points are:  ( 3 , 2 ) , ( 6 , 4 ) . {\displaystyle (3,2),(6,4).}
Equation of line is:  2 x − 3 y = 0. {\displaystyle 2x-3y=0.}
Non-zero value for coefficient  C {\displaystyle C} is impossible.

Sometimes you create a matrix that seems correct, but it fails to produce a result. Here is such an example:

What is equation of line defined by points  ( 3 , 2 ) , ( 6 , 4 ) ? {\displaystyle (3,2),(6,4)?}

As above assume that  C = 1. {\displaystyle C=1.}

# python code:

p1 = x1,y1 = 3,2
p2 = x2,y2 = 6,4
input = [
[x1,y1,1],
[x2,y2,1],

result = solve2by3 (input)
print (result)

This assumption failed:

solve2by3() : parallel input.
input =
[3, 2, 1]
[6, 4, 1]
None

Assume that  A = 1. {\displaystyle A=1.}

# python code:

# 1(x1) + B(y1) + 1(C) = 0
# 1(x2) + B(y2) + 1(c) = 0

# y1(B) + 1(C) + x1 = 0
# y2(B) + 1(C) + x2 = 0

input = [
[y1,1,x1], # Notice that input has been
[y2,1,x2], # rotated left by one column.

result = solve2by3 (input)
print (result)

(-1.5, 0.0)

Equation of line is:  1 ⋅ x + ( − 1.5 ) ⋅ y + 0 = 0 {\displaystyle 1\cdot x+(-1.5)\cdot y+0=0} or  2 x − 3 y = 0 {\displaystyle 2x-3y=0}

This line includes the origin. Therefore constant  C {\displaystyle C} cannot be non-zero.

Generally:

* Without rotation,  A , B = result {\displaystyle A,B={\text{result}}} and  C = 1. {\displaystyle C=1.}

* With rotation left by one column,  B , C = result {\displaystyle B,C={\text{result}}} and  A = 1. {\displaystyle A=1.}

In this case:

# python code:
>>> B,C = result ; A = 1
>>> A,B,C
>>> 1,-1.5,0

For example of big matrix that failed the first time, but was successful the second time, see cubic resistive network below.

## Solving 3 by 4[edit | edit source]

Below is python code containing function `solve3by4 (input).` The function accepts as input a 3 by 4 matrix like that described in the code comments.

In 3d coordinate geometry the equations `(1), (2), (3)` are the equations of 3 planes and the solution of the system is the point of intersection of the 3 planes.

Given 3 random planes, there are 2 conditions that do not produce a point of intersection:

* If 2 of the planes are parallel. See Figure 1 below.

* If the 3 planes form a "tent" or "delta." See Figure 2, 3 or 4 below. A special case of this condition occurs when size of "tent" is zero, in which case all three planes intersect on a common line and there is an infinite number of solutions. See Figure 5 below.

The trivial solution  ( 0 , 0 , 0 ) {\displaystyle (0,0,0)} is valid. See Figure 6 below. `` ``

* Figure 1. When 2 planes are parallel, there is no point of intersection.

* Figure 2. Triangular tube, 3 planes that never intersect.

* Figure 3. 3 planes that form a "tent," view 1. There is no point of intersection.

* Figure 4. 3 planes that form a "tent," view 2. There is no point of intersection.

* Figure 5. 3 planes that intersect on a common line. Points  ( 6 , 6 , 1 ) , {\displaystyle (6,6,1),} ( 7 , 7 , 4 ) , {\displaystyle (7,7,4),} ( 8 , 8 , 7 ) {\displaystyle (8,8,7)} are on this line and are common to all 3 planes.

* Figure 6. 3 planes that intersect at origin (0, 0, 0).

def solve3by4 (input) :
'''
input represents system:
a1*x + b1*y + c1*z + d1 = 0 ... (1)
a2*x + b2*y + c2*z + d2 = 0 ... (2)
a3*x + b3*y + c3*z + d3 = 0 ... (3)

input = (
(a1,b1,c1,d1),
(a2,b2,c2,d2),
(a3,b3,c3,d3),

To invoke:
x,y,z = solve3by4 (input)
Function solve3by4 () may return None.

input is not changed by this function.
'''
def checkDirectionNumbers (input) :
row1,row2,row3 = input
a1,b1,c1,d1 = row1
a2,b2,c2,d2 = row2
a3,b3,c3,d3 = row3
# The direction numbers of the lines of intersection.
dn1 = [b3*c2-b2*c3, a2*c3-a3*c2, b2*a3-b3*a2]
if dn1[0] == dn1[1] == dn1[2] == 0 :
print ('checkDirectionNumbers() : 2 rows parallel, 2 and 3.')
return
dn2 = [b3*c1-b1*c3, a1*c3-a3*c1, b1*a3-b3*a1]
if dn2[0] == dn2[1] == dn2[2] == 0 :
print ('checkDirectionNumbers() : 2 rows parallel, 1 and 3.')
return
dn3 = [b1*c2-b2*c1, a2*c1-a1*c2, b2*a1-b1*a2]
if dn3[0] == dn3[1] == dn3[2] == 0 :
print ('checkDirectionNumbers() : 2 rows parallel, 1 and 2.')
return
# Are 2 lines of intersection parallel?
# If so, and no 2 planes are parallel, the 3 planes form a tent.
a2,b2,c2 = dn2
a3,b3,c3 = dn3
dn1 = [b3*c2-b2*c3, a2*c3-a3*c2, b2*a3-b3*a2]
if dn1[0] == dn1[1] == dn1[2] == 0 :
print ('checkDirectionNumbers() : 3 rows form a tent.');
return

def solve3by4_(input) :
row1,row2,row3 = input
a1,b1,c1,d1 = row1
a2,b2,c2,d2 = row2
a3,b3,c3,d3 = row3

if (a1 == b1 == c1 == 0) or (a2 == b2 == c2 == 0) or (a3 == b3 == c3 == 0) :
print ('solve3by4() : empty row in input.')
return None
if (a1 == a2 == a3 == 0) or (b1 == b2 == b3 == 0) or (c1 == c2 == c3 == 0) :
print ('solve3by4(). empty column in input.')
return None
if (d1 == d2 == d3 == 0) :
# This empty column means that trivial solution is valid.
return 0,0,0

# Sort input if necessary so that a1 is non-zero.
if a1 : pass
elif a2 :
row1,row2 = row2,row1
a1,b1,c1,d1 = row1
a2,b2,c2,d2 = row2
else :
row1,row3 = row3,row1
a1,b1,c1,d1 = row1
a3,b3,c3,d3 = row3

# The matrix is:
# a1, b1, c1, d1 with a1 non-zero.
# a2, b2, c2, d2
# a3, b3, c3, d3

# Process rows, if necessary, so that a2,a3 become zero.

if a2 :
L1 = [a2*v for v in row1]
L2 = [a1*v for v in row2]
zero,b2_,c2_,d2_ = [ L1[p]-L2[p] for p in (0,1,2,3) ]
else : zero,b2_,c2_,d2_ =  a2,b2,c2,d2

if a3 :
L1 = [a3*v for v in row1]
L2 = [a1*v for v in row3]
zero,b3_,c3_,d3_ = [ L1[p]-L2[p] for p in (0,1,2,3) ]
else : zero,b3_,c3_,d3_ =  a3,b3,c3,d3

# The matrix is:
# a1, b1,  c1,  d1 with a1 non-zero.
# 0,  b2_, c2_, d2_
# 0,  b3_, c3_, d3_
data1 = (
(b2_, c2_, d2_),
(b3_, c3_, d3_),

data2 = solve2by3(data1)
if data2 == None :
checkDirectionNumbers (input) # If solve2by3() fails, this line shows why.
return None
y,z = data2
# a1*x + b1*y + c1*z + d1 = 0
# a1*x = -( b1*y + c1*z + d1 )
x = -(b1*y + c1*z + d1)/a1 # Here is why a1 must be non-zero.

return x,y,z

output = error = ''
try : output = solve3by4_(input)
except : error = sys.exc_info()[:2]

if (output == None) or error :
if error :
print ('solve3by4() :')
print ('  error detected in processing.')
print ('   ', error)
print ('  input of solve3by4() =')
print ('   ', input[0])
print ('   ', input[1])
print ('   ', input[2])
return None
return output

Below are examples of invocation with both good data and bad data.

Invocation with good data:

output = solve3by4([
[2,  -1, 3, -17],
[1,  3, 2, -25] ,
[3,  2, -1, -12] ,

print (output)

(3.0, 4.0, 5.0)

output = solve3by4([
[12,  -12, -5, -24],
[-6,   15,  8,  12] ,
[ 0,   10,  3,   0] ,

print (output)

(2.0, 0.0, -0.0)

Invocation with bad or invalid data:

output = solve3by4([
[20, 16, 2, 17],
[-77, -27, -25, -24],
[81, 45, 18, 43]

print (output)

solve2by3() : parallel input.
input =
(-692, 346, -829)
(396, -198, 517)
checkDirectionNumbers() : 3 rows form a tent.
input of solve3by4() =
[20, 16, 2, 17]
[-77, -27, -25, -24]
[81, 45, 18, 43]
None

output = solve3by4([
[-34, -6, 34, -32],
[82, 14, -82, 72],
[-59, -45, 59, -88]

print (output)

solve2by3() : empty column in input.
input =
(-16, 0, -176)
(-1176, 0, -1104)
checkDirectionNumbers() : 3 rows form a tent.
input of solve3by4() =
[-34, -6, 34, -32]
[82, 14, -82, 72]
[-59, -45, 59, -88]
None

output = solve3by4([
[34, -5, -21, 30],
[-54, -33, 63, 62],
[18, 11, -21, 38]

print (output)

solve2by3() : parallel input.
input =
(1392, -1008, -3728)
(-464, 336, -752)
checkDirectionNumbers() : 2 rows parallel, 2 and 3.
input of solve3by4() =
[34, -5, -21, 30]
[-54, -33, 63, 62]
[18, 11, -21, 38]
None

output = solve3by4([
[20, 16, 2, 17],
[-77, -27, '-25', -24],
[81, 45, 18, 43]

print (output)

solve3by4() :
error detected in processing.
(<class 'TypeError'>, TypeError("unsupported operand type(s) for -: 'int' and 'str'",))
input of solve3by4() =
[20, 16, 2, 17]
[-77, -27, '-25', -24]
[81, 45, 18, 43]
None

### Reversing the Process[edit | edit source]

#### Quadratic function[edit | edit source]

Figure 1. Graph of quadratic function defined by 3 points.

A quadratic function  y = f ( x ) = A x 2 + B x + C {\displaystyle y=f(x)=Ax^{2}+Bx+C} is defined by 3 points:  ( − 2 , 9.0 ) , ( 5 , 4.8 ) , ( 10 , 13.8 ) . {\displaystyle (-2,9.0),(5,4.8),(10,13.8).}

What is  f ( x ) ? {\displaystyle f(x)?}

y = A x 2 + B x + C {\displaystyle y=Ax^{2}+Bx+C}

A , B , C {\displaystyle A,B,C} are 3 unknowns.

x 2 ( A ) + x ( B ) + 1 ( C ) − y = 0 {\displaystyle x^{2}(A)+x(B)+1(C)-y=0}

# python code:

x1,y1 = -2, 9
x2,y2 =  5, 4.8
x3,y3 = 10, 13.8

input = [
[x1**2, x1, 1, -y1],
[x2**2, x2, 1, -y2],
[x3**2, x3, 1, -y3],

A, B, C = solve3by4 (input)
s1 = 'A,B,C'
print (s1, eval(s1))

A,B,C (0.2, -1.2, 5.8)

y = f ( x ) = 0.2 x 2 − 1.2 x + 5.8 {\displaystyle y=f(x)=0.2x^{2}-1.2x+5.8}

#### Wrong assumptions[edit | edit source]

Figure 2. Graph of plane containing X axis.
Because plane passes through origin, non-zero value for coefficient  D {\displaystyle D} is impossible.
Because plane is parallel to  X {\displaystyle X} axis, non-zero value for coefficient  A {\displaystyle A} is impossible.

In 3 dimensions a plane ( A x + B y + C z + D = 0 {\displaystyle Ax+By+Cz+D=0} ) is defined by 3 points:  ( 5 , 3 , − 2 ) , ( − 1 , 12 , − 8 ) , ( 37 , 15 , − 10 ) . {\displaystyle (5,3,-2),(-1,12,-8),(37,15,-10).}

What is equation of plane?

A x + B y + C z + D = 0 {\displaystyle Ax+By+Cz+D=0}

x A + y B + z C + D = 0 {\displaystyle xA+yB+zC+D=0}

Assume that  D = 1. {\displaystyle D=1.}

x A + y B + z C + 1 = 0 {\displaystyle xA+yB+zC+1=0}

# python code

pt1 = x1,y1,z1 = (5, 3, -2)
pt2 = x2,y2,z2 = (-1, 12, -8)
pt3 = x3,y3,z3 = (37, 15, -10)

input = [
[ 5,  3,  -2, 1],
[-1, 12,  -8, 1],
[37, 15, -10, 1],

result = solve3by4 (input)
s1 = 'result'
print (s1, eval(s1))

solve2by3() : parallel input.
input =
(-63, 42, -6)
(36, -24, 32)
checkDirectionNumbers() : 3 rows form a tent.
input of solve3by4() =
(5, 3, -2, 1)
(-1, 12, -8, 1)
(37, 15, -10, 1)
result None

Input was invalid. Rotate input left and try again.

# python code.

input = [
[ 3,  -2, 1,  5],
[12,  -8, 1, -1],
[15, -10, 1, 37],

result = solve3by4 (input)
s1 = 'result'
print (s1, eval(s1))

solve2by3() : empty column in input.
input =
(0, 9, 63)
(0, 12, -36)
checkDirectionNumbers() : 3 rows form a tent.
input of solve3by4() =
(3, -2, 1, 5)
(12, -8, 1, -1)
(15, -10, 1, 37)
result None

Input was invalid. Rotate input left and try again.

input = [
[ -2, 1,  5,  3],
[ -8, 1, -1, 12],
[-10, 1, 37, 15],

result = solve3by4 (input)
s1 = 'result'
print (s1, eval(s1))

result (1.5, 0, 0)

Without rotation:  A , B , C = result {\displaystyle A,B,C={\text{result}}} and  D = 1. {\displaystyle D=1.}

With rotation left:  B , C , D = result {\displaystyle B,C,D={\text{result}}} and  A = 1. {\displaystyle A=1.}

With another rotation left:  C , D , A = result {\displaystyle C,D,A={\text{result}}} and  B = 1. {\displaystyle B=1.}

# python code:
C, D, A = (1.5, 0, 0) ; B = 1
A, B, C, D

(0, 1, 1.5, 0)

Equation of plane is:  0 x + 1 y + 1.5 z + 0 = 0 {\displaystyle 0x+1y+1.5z+0=0} or  2 y + 3 z = 0 {\displaystyle 2y+3z=0}

## Solving M by (M+1)[edit | edit source]

Below is python code containing function `solveMbyN (input).` The function accepts as input a 4 by 5 matrix (or greater) like that described in the code comments.

This function is recursive, calling itself with M being reduced by 1 with each recursive invocation until the matrix is size 3 by 4 in which case function solve3by4() is called.

def reduceRow (row) :
'''
This function calculates the max(abs) of all values in row,
divides all values by the max(abs) and returns the result.
[27, -15, 42] becomes [27/42, -15/42, 42/42].
Very small values are converted to zero.

To invoke:
output = reduceRow (row)
output is list or None
'''
if False in [ isinstance(v,Decimal) for v in row ] :
print ('reduceRow: non Decimal in input.', { type(v) for v in row })
return None
max = sorted([ abs(v) for v in row ])[-1]
row = [ v/max for v in row ]
almostZero = Decimal('1e-' + str( getcontext().prec ))
row = [ (v, Decimal(0))[abs(v) < almostZero] for v in row ]
return row

def solveMbyN (input) :
'''
input represents system:
a0*w + b0*x + c0*y + d0*z + e0 = 0
a1*w + b1*x + c1*y + d1*z + e1 = 0
a2*w + b2*x + c2*y + d2*z + e2 = 0
a3*w + b3*x + c3*y + d3*z + e3 = 0
or greater.
This solves a matrix of M rows by N columns where N = M+1 and M >= 2

input = [
(a0,b0,c0,d0,e0),
(a1,b1,c1,d1,e1),
(a2,b2,c2,d2,e2),
(a3,b3,c3,d3,e3),

To invoke:
w,x,y,z = solveMbyN (input)
Function solveMbyN () may return None.

If input is size 15 by 16:
v1,v2,v3, .... ,v14,v15 = solveMbyN (input)

input is not changed by this function.
'''
def solveMbyN_(input) :
input = list(input)
# input is now local, hard copy of original input.
# This ensures that original input is not changed by this function.

numberOfRows = len(input)
numberOfColumns = numberOfRows+1

if numberOfRows < 2 :
print ('solveMbyN()! len(input) < 2.')
return None
if numberOfRows == 2 : return solve2by3(input)
if numberOfRows == 3 : return solve3by4(input)

for row in range (0, numberOfRows) :
if len(input[row]) != numberOfColumns :
print ('solveMbyN()! row with bad length.')
print ('    row number',row,'of',numberOfRows,'rows has length',len(input[row]),'.')
return None
if True not in [ bool(v) for v in input[row][:-1] ] :
print ('solveMbyN()! empty row in input, row number',row,'of',numberOfRows,'rows.')
return None

for column in range (0, numberOfRows) :
sum = 0
for row in range (0, numberOfRows) :
sum = bool(input[row][column])
if sum : break
if not sum :
print ('solveMbyN()! empty column in input. column number',column,'of',numberOfRows,'columns.')
return None

for column in (numberOfRows,) :
sum = 0
for row in range (0, numberOfRows) :
sum = bool(input[row][column])
if sum : break
if not sum :
# Empty column. Trivial solution is valid.
return (0,) * numberOfRows

if 0 : # For matrices greater than 10 by 11 enable this piece of code.
for row in range (0, numberOfRows) :
data = reduceRow (input[row])
if isinstance(data,list) :
input[row] = data

# Arrange input if necessary so that a0 is non-zero.
if not input[0][0] :
for p in range (1, numberOfRows) :
if input[p][0] :
input[p], input[0] = input[0], input[p]
break
# The matrix is:
# [a0,b0,c0,d0,e0], with a0 non-zero,
# [a1,b1,c1,d1,e1],
# [a2,b2,c2,d2,e2],
# [a3,b3,c3,d3,e3],

# Process rows so that a1, a2, a3, .... become zero.
a0 = input[0][0]
for p in range (1, numberOfRows) :
ap = input[p][0]
if ap :
L0 = [ input[0][q]*ap for q in range (0, numberOfColumns) ]
Lp = [ input[p][q]*a0 for q in range (0, numberOfColumns) ]
L_ = [ L0[q]-Lp[q] for q in range (0, numberOfColumns) ]
if L_[0] :
# This should not happen.
print ('solveMbyN()! internal error 1.')
return None
input[p] = L_

# The matrix is:
# [a0, b0, c0, d0, e0], with a0 non-zero,
# [ 0,b1_,c1_,d1_,e1_],
# [ 0,b2_,c2_,d2_,e2_],
# [ 0,b3_,c3_,d3_,e3_],
data1 = [ input[p][1:] for p in range (1, numberOfRows) ]
# data1 = [
# [b1_,c1_,d1_,e1_],
# [b2_,c2_,d2_,e2_],
# [b3_,c3_,d3_,e3_] ]

data2 = solveMbyN(data1) # Here is where function solveMbyN() calls itself.
if data2 == None : return None
topRow = input[0][1:]
# b0, c0, d0, e0 topRow
#  x,  y,  z     data2
listOfProducts = [ topRow[p]*data2[p] for p in range (0, len(data2)) ] + topRow[-1:]
# listOfProducts = [ b0*x, c0*y, d0*z, e0 ]
sumOfProducts = [ sum for sum in (0,) for p in listOfProducts for sum in (sum+p,) ][-1]
# sumOfProducts = [ b0*x, b0*x+c0*y, b0*x+c0*y+d0*z, b0*x+c0*y+d0*z+e0 ][-1]
# sumOfProducts =  b0*x + c0*y + d0*z + e0
#           a0*w + b0*x + c0*y + d0*z + e0 = 0
#           a0*w + sumOfProducts = 0

w = -sumOfProducts/a0 # Here is why a0 must be non-zero.

return (w,) + data2

output = error = ''
try : output = solveMbyN_(input)
except : error = sys.exc_info()[:2]
if (output == None) or error :
if error :
print ('solveMbyN()!')
print ('  error detected in processing.')
print ('   ', error)
if len(input) >= 4 :
print ('  input of solveMbyN() with',len(input),'rows =')
for row in input : print ('   ', row)
return None
return output

Below are examples of invocation with both good data and bad data.

Invocation with good data:

L1 = [
[Decimal('-870442133162'), Decimal('239132906684'), Decimal('397660499756'), Decimal('788825405004'), Decimal('156830886652'), Decimal('-87096140169')],
[Decimal('-112130420352'), Decimal('-93605595828'), Decimal('-185262083472'), Decimal('-93369903383'), Decimal('-497973505065'), Decimal('-766710012513')],
[Decimal('-142983492168'), Decimal('619713461197'), Decimal('-608752759383'), Decimal('-448477443042'), Decimal('-281161661751'), Decimal('-799463977245')],
[Decimal('572854919541'), Decimal('-148673693908'), Decimal('341291756040'), Decimal('362403188059'), Decimal('-202136092949'), Decimal('-429932229578')],
[Decimal('157632861972'), Decimal('330790263276'), Decimal('-836474627955'), Decimal('644295395386'), Decimal('-595590967129'), Decimal('-392835045727')]

output = solveMbyN(L1)
print (output)

The following results were produced with decimal precision set at 50:

Decimal('-0.06331410760212760088313271882866553044211194694576'), Decimal('1.0969089506763419155148060427588009931309074487534'), Decimal('1.0837038580207682607126722016017867670831200359767'), Decimal('-0.42989566124678842791674322699930461340596671792215'), Decimal('-2.0541601084281682582146753421426610779297051535362')

Invocation with bad or invalid data:

output = solveMbyN([
[-4, -6, 3, -4, -8, -8],
[1, 0, 2, 5, 1, 7],
[9, -3, -8, 4, 7, -5],
[1, -9, 5, -2, 5, -2],
[1, -3, -6, -3, -6, 6]

print (output)

# With each step towards solve2by3() the length of each number in digits can theoretically double.
solve2by3() : parallel input.
input =
(-181440, 181440, -101088)
(90720, -90720, 406944)
checkDirectionNumbers() : 3 rows form a tent.
input of solve3by4() =
[-756, -1176, 0, -1872]
[-324, -744, 240, -936]
[-324, -384, -120, -264]
input of solveMbyN() with 4 rows =
[-6, 11, 16, -4, 20]
[-66, -5, -20, -44, -92]
[-42, 23, -12, 12, -16]
[-18, -21, -16, -32, 16]
input of solveMbyN() with 5 rows =
[-4, -6, 3, -4, -8, -8]
[1, 0, 2, 5, 1, 7]
[9, -3, -8, 4, 7, -5]
[1, -9, 5, -2, 5, -2]
[1, -3, -6, -3, -6, 6]
None

Provided that numbers are small, integers, floats and Decimal objects produce the same results:

output = solveMbyN([
[-4.0, -6.0, 3.0, -4.0, -8.0, -8.0],
[1.0, 0.0, 2.0, 5.0, 1.0, 7.0],
[9.0, -3.0, -8.0, 4.0, 7.0, -5.0],
[1.0, -9.0, 5.0, -2.0, 5.0, -2.0],
[1.0, -3.0, -6.0, -3.0, -6.0, 6.0]

print (output)

solve2by3() : parallel input.
input =
(-181440.0, 181440.0, -101088.0)
(90720.0, -90720.0, 406944.0)
checkDirectionNumbers() : 3 rows form a tent.
input of solve3by4() =
[-756.0, -1176.0, 0.0, -1872.0]
[-324.0, -744.0, 240.0, -936.0]
[-324.0, -384.0, -120.0, -264.0]
input of solveMbyN() with 4 rows =
[-6.0, 11.0, 16.0, -4.0, 20.0]
[-66.0, -5.0, -20.0, -44.0, -92.0]
[-42.0, 23.0, -12.0, 12.0, -16.0]
[-18.0, -21.0, -16.0, -32.0, 16.0]
input of solveMbyN() with 5 rows =
[-4.0, -6.0, 3.0, -4.0, -8.0, -8.0]
[1.0, 0.0, 2.0, 5.0, 1.0, 7.0]
[9.0, -3.0, -8.0, 4.0, 7.0, -5.0]
[1.0, -9.0, 5.0, -2.0, 5.0, -2.0]
[1.0, -3.0, -6.0, -3.0, -6.0, 6.0]
None

output = solveMbyN([
[Decimal('-4'), Decimal('-6'), Decimal('3'), Decimal('-4'), Decimal('-8'), Decimal('-8')],
[Decimal('1'), Decimal('0'), Decimal('2'), Decimal('5'), Decimal('1'), Decimal('7')],
[Decimal('9'), Decimal('-3'), Decimal('-8'), Decimal('4'), Decimal('7'), Decimal('-5')],
[Decimal('1'), Decimal('-9'), Decimal('5'), Decimal('-2'), Decimal('5'), Decimal('-2')],
[Decimal('1'), Decimal('-3'), Decimal('-6'), Decimal('-3'), Decimal('-6'), Decimal('6')]

print (output)

solve2by3() : parallel input.
input =
(Decimal('-181440'), Decimal('181440'), Decimal('-101088'))
(Decimal('90720'), Decimal('-90720'), Decimal('406944'))
checkDirectionNumbers() : 3 rows form a tent.
input of solve3by4() =
[Decimal('-756'), Decimal('-1176'), Decimal('0'), Decimal('-1872')]
[Decimal('-324'), Decimal('-744'), Decimal('240'), Decimal('-936')]
[Decimal('-324'), Decimal('-384'), Decimal('-120'), Decimal('-264')]
input of solveMbyN() with 4 rows =
[Decimal('-6'), Decimal('11'), Decimal('16'), Decimal('-4'), Decimal('20')]
[Decimal('-66'), Decimal('-5'), Decimal('-20'), Decimal('-44'), Decimal('-92')]
[Decimal('-42'), Decimal('23'), Decimal('-12'), Decimal('12'), Decimal('-16')]
[Decimal('-18'), Decimal('-21'), Decimal('-16'), Decimal('-32'), Decimal('16')]
input of solveMbyN() with 5 rows =
[Decimal('-4'), Decimal('-6'), Decimal('3'), Decimal('-4'), Decimal('-8'), Decimal('-8')]
[Decimal('1'), Decimal('0'), Decimal('2'), Decimal('5'), Decimal('1'), Decimal('7')]
[Decimal('9'), Decimal('-3'), Decimal('-8'), Decimal('4'), Decimal('7'), Decimal('-5')]
[Decimal('1'), Decimal('-9'), Decimal('5'), Decimal('-2'), Decimal('5'), Decimal('-2')]
[Decimal('1'), Decimal('-3'), Decimal('-6'), Decimal('-3'), Decimal('-6'), Decimal('6')]
None

### Sphere[edit | edit source]

In cartesian coordinate geometry of three dimensions a sphere is represented by equation:

x 2 + y 2 + z 2 + A x + B y + C z + D = 0. {\displaystyle x^{2}+y^{2}+z^{2}+Ax+By+Cz+D=0.}

On the surface of a certain sphere there are 4 known points:

# python code

point1 = (13,7,20)
point2 = (13,7,4)
point3 = (13,-17,4)
point4 = (16,4,4)

What is equation of sphere?

Rearrange equation of sphere to prepare for creation of input matrix:

( x ) A + ( y ) B + ( z ) C + ( 1 ) D + ( x 2 + y 2 + z 2 ) = 0. {\displaystyle (x)A+(y)B+(z)C+(1)D+(x^{2}+y^{2}+z^{2})=0.}

Create input matrix of size 4 by 5:

# python code

input = []
for (x,y,z) in (point1, point2, point3, point4) :
input += [ ( x, y, z, 1, (x**2 + y**2 + z**2) ) ]

print (input)

[ (13,   7, 20, 1, 618),
(13,   7,  4, 1, 234),
(13, -17,  4, 1, 474),
(16,   4,  4, 1, 288), ] # Matrix containing 4 rows with 5 members per row.

# python code

result = solveMbyN(input)
print (result)

(-8.0, 10.0, -24.0, -104.0)

Equation of sphere is :

x 2 + y 2 + z 2 − 8 x + 10 y − 24 z − 104 = 0 {\displaystyle x^{2}+y^{2}+z^{2}-8x+10y-24z-104=0}

### Quintic function[edit | edit source]

Figure 4. Graph of quintic function defined by 6 points.

A quintic function  y = f ( x ) = A x 5 + B x 4 + C x 3 + D x 2 + E x + F {\displaystyle y=f(x)=Ax^{5}+Bx^{4}+Cx^{3}+Dx^{2}+Ex+F} is defined by 6 points:  ( − 8 , 0 ) , ( − 6 , 0 ) , ( − 3 , 0 ) , ( 1 , 0 ) , ( 3 , 0 ) , ( 5 , 10 ) . {\displaystyle (-8,0),(-6,0),(-3,0),(1,0),(3,0),(5,10).}

What is  f ( x ) ? {\displaystyle f(x)?}

y = f ( x ) = A x 5 + B x 4 + C x 3 + D x 2 + E x + F {\displaystyle y=f(x)=Ax^{5}+Bx^{4}+Cx^{3}+Dx^{2}+Ex+F}

A , B , C , D , E , F {\displaystyle A,B,C,D,E,F} are 6 unknowns.

x 5 ( A ) + x 4 ( B ) + x 3 ( C ) + x 2 ( D ) + x ( E ) + 1 ( F ) − y = 0 {\displaystyle x^{5}(A)+x^{4}(B)+x^{3}(C)+x^{2}(D)+x(E)+1(F)-y=0}

# python code:

pt1 = x1,y1 = -8,0
pt2 = x2,y2 = -6,0
pt3 = x3,y3 = -3,0
pt4 = x4,y4 = 1,0
pt5 = x4,y5 = 3,0
pt6 = x6,y6 = 5,10

input = []

for pt in (pt1,pt2,pt3,pt4,pt5,pt6) :
x,y = pt
input += [[ x**5, x**4, x**3, x**2, x, 1, -y  ]]

print (input)

[[-32768, 4096, -512, 64, -8, 1,   0],
[ -7776, 1296, -216, 36, -6, 1,   0],
[  -243,   81,  -27,  9, -3, 1,   0],
[     1,    1,    1,  1,  1, 1,   0],
[   243,   81,   27,  9,  3, 1,   0],
[  3125,  625,  125, 25,  5, 1, -10]]

result = solveMbyN (input)

s1 = 'result'
print (s1, eval(s1))

result (0.0010926573426573423, 0.014204545454545447, 0.027316433566433533,
-0.18028846153846156,  -0.3343531468531468,   0.47202797202797203)

From these results the following ratios may be deduced:

1 A = 915.2 ; {\displaystyle {\frac {1}{A}}=915.2;\ } B A = 13 ; {\displaystyle {\frac {B}{A}}=13;\ } C A = 25 ; {\displaystyle {\frac {C}{A}}=25;\ } D A = − 165 ; {\displaystyle {\frac {D}{A}}=-165;\ } E A = − 306 ; {\displaystyle {\frac {E}{A}}=-306;\ } F A = 432. {\displaystyle {\frac {F}{A}}=432.}

y = f ( x ) = x 5 + 13 x 4 + 25 x 3 − 165 x 2 − 306 x + 432 915.2 {\displaystyle y=f(x)={\frac {x^{5}+13x^{4}+25x^{3}-165x^{2}-306x+432}{915.2}}}

### Resistive network[edit | edit source]

Figure 1. Diagram of resistive network containing 8 resistors.

A resistive network containing 8 resistors is connected as shown in Figure 1.

What is resistance  R {\displaystyle R} between points  P 1 , P 3 ? {\displaystyle P_{1},P_{3}?}

#### Solve for 8 Branch Currents[edit | edit source]

Figure 2. Diagram of resistive network showing all branch currents.

At any point to which 3 or more conductors are connected, sum of currents = 0.
At point  P 4 {\displaystyle P_{4}} currents  i 1 , i 7 {\displaystyle i_{1},i_{7}} flow towards point; current  i 4 {\displaystyle i_{4}} flows away from point.
i 1 + i 7 − i 4 = 0. {\displaystyle i_{1}+i_{7}-i_{4}=0.}

In any closed loop sum of voltages = 0.
In loop containing points  P 1 , P 2 {\displaystyle P_{1},P_{2}} voltages  e 2 , e 8 {\displaystyle e_{2},e_{8}} are clockwise; voltage  e 3 {\displaystyle e_{3}} is counter-clockwise.  {\displaystyle }
e 2 + e 8 − e 3 = 0. {\displaystyle e_{2}+e_{8}-e_{3}=0.} {\displaystyle } {\displaystyle }

Direction of flow through  r 7 , r 8 {\displaystyle r_{7},r_{8}} is assumed and is subject to correction.

Voltage across  r 1 : {\displaystyle r_{1}:}

Using  E = I R , e 1 = i 1 ⋅ r 1 = r 1 ⋅ a . {\displaystyle E=IR,\ e_{1}=i_{1}\cdot r_{1}=r_{1}\cdot a.}

Similarly:

e 2 = r 2 ⋅ b . {\displaystyle e_{2}=r_{2}\cdot b.}

e 3 = r 3 ⋅ c . {\displaystyle e_{3}=r_{3}\cdot c.}

… … {\displaystyle \dots \dots }

e 8 = r 8 ⋅ h . {\displaystyle e_{8}=r_{8}\cdot h.}

Build a matrix size 8 by 9 which will be input to function solveMbyN().

# Python code:

r1,r2,r3,r4,r5,r6,r7,r8 = 10,20,30,40,50,60,70,80

input = []

# e1 = e2 + e7
# 10a = 20b + 70g
#          10a - 20b + 0c + 0d + 0e + 0f - 70g + 0h + 0 = 0
input += [[r1, - r2,   0,   0,   0,   0, - r7,   0,   0]]

# e3 = e2 + e8
# 30c = 20b + 80h
#          0a - 20b + 30c + 0d + 0e + 0f + 0g - 80h + 0 = 0
input += [[0, - r2,   r3,   0,   0,   0,   0, - r8,   0]]

# e5 = e7 + e4
# 50e = 70g + 40d
#          0a + 0b + 0c - 40d + 50e + 0f - 70g + 0h + 0 = 0
input += [[0,   0,   0, - r4,   r5,   0, - r7,   0,   0]]

# e5 = e8 + e6
# 50e = 80h + 60f
#          0a + 0b + 0c + 0d + 50e - 60f + 0g - 80h + 0 = 0
input += [[0,   0,   0,   0,   r5, - r6,   0, - r8,   0]]

# i7 + i5 + i8 = i2
# g + e + h = b
#          0a - 1b + 0c + 0d + 1e + 0f + 1g + 1h + 0 = 0
input += [[0 ,- 1,   0,   0,   1,   0,   1,   1,   0]]

# i4 = i1 + i7
# d = a + g
#          -1a + 0b + 0c + 1d + 0e + 0f - 1g + 0h + 0 = 0
input += [[-1,   0,   0,   1,   0,   0, - 1,   0,   0]]

# i6 = i3 + i8
# f = c + h
#          0a + 0b - 1c + 0d + 0e + 1f + 0g - 1h + 0 = 0
input += [[0,   0, - 1,   0,   0,   1,   0, - 1,   0]]

# Total input current = 1
# a + b + c = 1
#          1a + 1b + 1c + 0d + 0e + 0f + 0g + 0h - 1 = 0
input += [[1,   1,   1,   0,   0,   0,   0,   0, - 1]]

This last entry can contain almost any arbitrary values. For example:

# Python code:

# i7 = 2.3
input += [[0,   0,   0,   0,   0,   0,   1,   0, - 2.3]]

# e5 = 1.9
input += [[0,   0,   0,   0,   r5,   0,   0,   0, - 1.9]]

Changing this last entry changes all values of  e x , i x . {\displaystyle e_{x},i_{x}.} However, the ratio  e x i x = r x {\displaystyle {\frac {e_{x}}{i_{x}}}=r_{x}} remains constant.

input is matrix of size 8 by 9:

[10, -20,  0,   0,  0,   0, -70,   0,  0],  # e1 = e2 + e7,      or r1(i1) - r2(i2) - r7(i7) = 0
[ 0, -20, 30,   0,  0,   0,   0, -80,  0],  # e3 = e2 + e8,      or r3(i3) - r2(i2) - r8(i8) = 0
[ 0,   0,  0, -40, 50,   0, -70,   0,  0],  # e5 = e4 + e7,      or r5(i5) - r4(i4) - r7(i7) = 0
[ 0,   0,  0,   0, 50, -60,   0, -80,  0],  # e5 = e6 + e8,      or r5(i5) - r6(i6) - r8(i8) = 0
[ 0,  -1,  0,   0,  1,   0,   1,   1,  0],  # i2 = i5 + i7 + i8, or 1(i5) + 1(i7) + 1(i8) - 1(i2) = 0
[-1,   0,  0,   1,  0,   0,  -1,   0,  0],  # i4 = i1 + i7,      or 1(i4) - 1(i1) - 1(i7) = 0
[ 0,   0, -1,   0,  0,   1,   0,  -1,  0],  # i6 = i3 + i8,      or 1(i6) - 1(i3) - 1(i8) = 0
[ 1,   1,  1,   0,  0,   0,   0,   0, -1]   # i1 + i2 + i3 = 1,  or 1(i1) + 1(i2) + 1(i3) - 1 = 0

i1,i2,i3,i4,i5,i6,i7,i8 = values_of_i = solveMbyN (input)
s1 = 'values_of_i'
print(s1,eval(s1))

values_of_i (0.45727389222364606,  0.3065353746543468,  0.23619073312200717,
0.4350171983543536,   0.31685438726647336, 0.24812841437917316,
-0.022256693869292507, 0.011937681257165982)

i 7 {\displaystyle i_{7}} has negative value. Reverse  i 7 {\displaystyle i_{7}} to make all branch currents positive.

#### Make all Branch Currents positive[edit | edit source]

Figure 3. Diagram of resistive network showing all branch currents positive.

Make  i 7 {\displaystyle i_{7}} positive and check results.

# Python code

i7 *= -1

values_of_e = e1,e2,e3,e4,e5,e6,e7,e8 = [
eval(s1)
for s in '12345678'
for s1 in [ 'i' + s + '*r' + s ]

s1 = 'values_of_e'
print(s1,eval(s1))

values_of_e [ 4.572738922236461,  6.130707493086937,
7.085721993660215, 17.400687934174144,
15.842719363323669, 14.88770486275039,
1.5579685708504756, 0.9550145005732785]

# python code

t1 = (
'e1+e7-e2',              # e1 + e7 should equal e2
'e2+e8-e3',              # e2 + e8 should equal e3
'e7+e5-e4',              # e7 + e5 should equal e4
'e8+e6-e5',              # e8 + e6 should equal e5

'e1+e4-(e2+e5)',         # e1 + e4 should equal e2 + e5
'e1+e4-(e3+e6)',         # e1 + e4 should equal e3 + e6

'e1+e7+e8-e3',           # e1 + e7 + e8 should equal e3
'e7+e8+e6-e4',           # e7 + e8 + e6 should equal e4

'i1-i7-i4',              # i1 should equal i7 + i4
'i6-i3-i8',              # i6 should equal i3 + i8
'i2+i7-(i8+i5)',         # i2 + i7 should equal i8 + i5
'i1+i2+i3 - (i4+i5+i6)', # Total input current should
# equal total output current.

for s1 in t1 :
print(s1,eval(s1))

e1+e7-e2             -8.881784197001252e-16
e2+e8-e3              0.0
e7+e5-e4              0.0
e8+e6-e5              0.0

e1+e4-(e2+e5)         0.0
e1+e4-(e3+e6)         0.0

e1+e7+e8-e3          -8.881784197001252e-16
e7+e8+e6-e4           0.0

i1-i7-i4             -5.551115123125783e-17
i6-i3-i8              6.938893903907228e-18
i2+i7-(i8+i5)         0.0
i1+i2+i3 - (i4+i5+i6) 0.0

#### Compute R[edit | edit source]

# Python code

E = e3+e6
I = i1 + i2 + i3
R = E/I
s1 = 'R'
print(s1,eval(s1))

R 21.973426856410605

## Solving big matrices[edit | edit source]

A big matrix is one of size greater than 10 by 11.

With each recursive call of solveMbyN() the size of each value in decimal digits can theoretically double. Python's integer math enjoys infinite precision. If your original matrix is of size 32 by 33, and each value in the matrix is an int of 1 decimal digit, function solve2by3() can be called with ints of size 2**30 (1,073,741,824) decimal digits.

Floats can be used instead of ints, but the limited precision of floats may not provide a desirable level of accuracy in the result.

Python's decimal objects are a good compromise and function reduceRow() above is an attempt to keep the size of each member manageable.

Function reduceRow() works well. However, as with all operations involving python's decimal objects, you need to pay careful attention to precision and accuracy of the results.

On my Mac the following python code generates a matrix of size 100 by 101 and calculates the result containing 100 values in about 1.5 seconds, including the time to generate 10,100 random numbers, each containing up to 12 decimal digits with the decimal point in a random position, and including the time to test output over 100 rows to verify that all calculated values of the solution satisfy each row.

In function solveMbyN() above find line:

if 0 : # For matrices greater than 10 by 11 enable this piece of code.

Change it to:

if 1 : # For matrices greater than 10 by 11 enable this piece of code.

Then:

from random import getrandbits
from decimal import *
import sys
getcontext().prec=50 # Set precision to 50.

numRows=100

def randomNumber():
# 1 invocation in 256 returns 0.
if not (getrandbits(10) & 0xFF) : return Decimal(0)
return [ w
for q in [ getrandbits(10) & 1]
for sign in [ (-1,1)[q] ]
for numberOfDecimalPlaces in [ getrandbits(10) & 0xF ]
for s1 in [ str(getrandbits(50))[-12:] ]
for s2 in [ '0'*12+s1 ]
for posn in [ len(s2)-numberOfDecimalPlaces ]
for w in [ sign*Decimal(s2[:posn]+'.'+s2[posn:]) ]
][0]

data = [[ randomNumber() for p in range(numRows+1) ]
for p in range (numRows)

datan = sorted([ v  for row in data for v in row if v < 0 ])
datap = sorted([ v  for row in data for v in row if v >= 0 ])
print ('A small sample of the random numbers in data:')
print ('+ve values:',len(datap), ',', datap[:2],'\n   ',datap[-2:])
print ('-ve values:',len(datan), ',', datan[:2],'\n   ',datan[-2:])

output = solveMbyN(data)

print ('A small sample of the solutions:')
print ('output[0]  =', output[0])
print ('output[19] =', output[19])
print ('output[59] =', output[59])
print ('output[99] =', output[99])

A small sample of the random numbers in data:
+ve values: 5021 , [Decimal('1.16871265E-7'), Decimal('1.73721245E-7')]
[Decimal('998523464710'), Decimal('999768605679')]
-ve values: 5079 , [Decimal('-991079944209'), Decimal('-989786083110')]
[Decimal('-4.10272824E-7'), Decimal('-2.38664537E-7')]

A small sample of the solutions:
output[0]  = 0.43689400018920695253771832432651757885163362928745
output[19] = 0.19867480862268284151047885891372314311902973127033
output[59] = 0.46169681824826309421940885550155275946668017604646
output[99] = -0.020598220802233733529683040085023443421113627790464

### Testing results[edit | edit source]

Data input to function solveMbyN() is an array of 100 rows, each row containing 101 numbers.

[v0_0, v0_1, v0_2, ..... v0_98,v0_99,v0_100],
.......
[v99_0,v99_1,v99_2, ...... v99_98,v99_99,v99_100],]

Data received from function solveMbyN() is an array of 100 numbers.

[ s0,s1,s2, ...... s98,s99 ]

The first row is tested with the solutions by;

L2 = [ v0_0*s0, v0_1*s1, v0_2*s2, ..... v0_98*s98,v0_99*s99,v0_100 ]

sum is the sum of all items in L2.

sum is appended to L1 and the process is repeated for all 100 rows.

L1 = []
for row in data :
# a, b, c, d, e, f, g ....... # row
# u, v, w, x, y, z            # output
L2 = [ row[p]*t1[p] for t1 in (output+(1,),) for p in range (0, len(row)) ]
# L2 = [a*u , b*v , c*w , d*x , e*y , f*z , g]
sum = [ sum for sum in [0] for p in L2 for sum in [sum+p] ][-1]
# sum = [a*u, a*u + b*v, a*u + b*v + c*w, a*u + b*v + c*w + d*x, a*u + b*v + c*w + d*x + e*y,
#        a*u + b*v + c*w + d*x + e*y + f*z, a*u + b*v + c*w + d*x + e*y + f*z + g][-1]
# sum = a*u + b*v + c*w + d*x + e*y + f*z + g
# sum is the sum of all items in L2
L1 += [abs(sum)]

L1a = sorted(L1)
print (len(L1a), L1a[0], L1a[-1])

Ideally sum should be zero.

In practice it's extremely unlikely that sum will be zero. My results typically gave values close to

100 4.1065149609E-38 9.6425614873452398368906E-25

The worst of the results above was 23 zeroes after the decimal point.

Better results can be achieved by increasing precision.

### Cubic Resistive Network[edit | edit source]

Figure 1. Diagram of resistive network containing 12 resistors.
Blue arrows show assumed direction of current.

A resistive network containing 12 resistors is connected as shown in Figure 1.

The network is called "cubic" because, in 3 dimensions, each resistor is located on one edge of a cube.

What is resistance  R {\displaystyle R} between points  P 1 , P 2 ? {\displaystyle P_{1},P_{2}?}

# Python code:

values_of_r = r1,r2,r3,r4,r5,r6,r7,r8,r9,r10,r11,r12 = 10,20,30,40,50,60,70,80,90,100,110,120

#### Solve for 12 Branch Currents[edit | edit source]

##### Create matrix 12 by 13[edit | edit source]

# Python code:
input = [
[10, 20, -30, -40,   0,  0,   0,   0,  0,    0,   0,    0,    0],  # e1 + e2 = e3 + e4
[ 0,  0,   0,   0,  50, 60, -70, -80,  0,    0,   0,    0,    0],  # e5 + e6 = e7 + e8
[ 0,  0,   0,  40,   0,  0,   0, -80,  0,    0, 110, -120,    0],  # e11 + e4 = e8 + e12
[10,  0,   0,   0, -50,  0,   0,   0, 90, -100,   0,    0,    0],  # e9 + e1 = e5 + e10
[ 0,  0,   0,   0,   1, -1,   0,   0,  0,   -1,   0,    0,    0],  # i5 = i10 + i6
[ 0,  0,   0,   0,   0,  0,   1,  -1,  0,    0,  -1,    0,    0],  # i7 = i11 + i8
[-1,  0,  -1,   0,   0,  0,   0,   0,  1,    0,   0,    0,    0],  # i9 = i1 + i3
[ 0,  0,  -1,   1,   0,  0,   0,   0,  0,    0,  -1,    0,    0],  # i4 = i3 + i11
[-1,  1,   0,   0,   0,  0,   0,   0,  0,   -1,   0,    0,    0],  # i2 = i1 + i10
[ 0,  0,   0,   0,   0, -1,   0,  -1,  0,    0,   0,    1,    0],  # i12 = i6 + i8
[ 0, -1,   0,  -1,   1,  0,   1,   0,  1,    0,   0,   -1,    0],  # i9 + i5 + i7 = i2 + i4 + i12
[ 0,  0,   1,   0,   0,  0,   0,   0,  0,    0,   0,    0, -1.6]  # i3 = 1.6

i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12 = values_of_i = solveMbyN (input)
s1 = 'values_of_i'
print(s1,eval(s1))

This matrix failed:

solve2by3() : empty row in input.
input =
(0, 0, 0)
(90533116137774317568000000000000000000000000000000000000000000, -84449318308603494400000000000000000000000000000000000000000000, 1.4228848407261846e+61)
checkDirectionNumbers() : 2 rows parallel, 1 and 2.
input of solve3by4() =
[38760268800000000000000000000, 593753233920000000000000000000, -569385344000000000000000000000, 0]
[620164300800000000000000000000000000, 9500051742720000000000000000000000000, -9110165504000000000000000000000000000, 0]
[153839206400000000000000000000000, 20882470400000000000000000000000, -81126144000000000000000000000000, -3.67098806272e+32]

##### Try again[edit | edit source]

# Python code:
input = [
[10, 20, -30, -40,   0,  0,   0,   0,  0,    0,   0,    0,    0],  # e1 + e2 = e3 + e4
[ 0,  0,   0,   0,  50, 60, -70, -80,  0,    0,   0,    0,    0],  # e5 + e6 = e7 + e8
[ 0,  0,   0,  40,   0,  0,   0, -80,  0,    0, 110, -120,    0],  # e11 + e4 = e8 + e12
[10,  0,   0,   0, -50,  0,   0,   0, 90, -100,   0,    0,    0],  # e9 + e1 = e5 + e10
[ 0,  0,   0,   0,   1, -1,   0,   0,  0,   -1,   0,    0,    0],  # i5 = i10 + i6
[ 0,  0,   0,   0,   0,  0,   1,  -1,  0,    0,  -1,    0,    0],  # i7 = i11 + i8
[-1,  0,  -1,   0,   0,  0,   0,   0,  1,    0,   0,    0,    0],  # i9 = i1 + i3
[ 0,  0,  -1,   1,   0,  0,   0,   0,  0,    0,  -1,    0,    0],  # i4 = i3 + i11
[-1,  1,   0,   0,   0,  0,   0,   0,  0,   -1,   0,    0,    0],  # i2 = i1 + i10
[ 0,  0,   0,   0,   0, -1,   0,  -1,  0,    0,   0,    1,    0],  # i12 = i6 + i8
#[ 0, -1,   0,  -1,   1,  0,   1,   0,  1,    0,   0,   -1,   0],  # i9 + i5 + i7 = i2 + i4 + i12 DELETED.
[10, 20,   0,   0,   0,  0, -70, -80, 90,    0,   0, -120,    0],  # e9 + e1 + e2 = e7 + e8 + e12 ADDED.
[ 0,  0,   1,   0,   0,  0,   0,   0,  0,    0,   0,    0, -1.6]  # i3 = 1.6

i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12 = values_of_i = solveMbyN (input)
s1 = 'values_of_i'
print(s1,eval(s1))

values_of_i
(5.502717149220487, 9.380927597389922,  1.600000000000003, 4.866143086000081,
6.129013402102139, 2.250802953932719,  4.685268628140509, 1.419125542140431,
7.102717149220491, 3.8782104481694213, 3.266143086000078, 3.669928496073145)

#### Compute R[edit | edit source]

# python code

values_of_e = e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12 = [ v[0]*v[1] for v in zip(values_of_i, values_of_r) ]
E = e7 + e8 + e12
I = i9 + i5 + i7
R = E/I
s1 = 'R'
print(s1,eval(s1))

R 49.22086885401488

#### Review[edit | edit source]

In matrix all values  i 1 , i 2 , … , i 11 , i 12 {\displaystyle i_{1},i_{2},\dots ,i_{11},i_{12}} and  e 1 , e 2 , … , e 11 , e 12 {\displaystyle e_{1},e_{2},\dots ,e_{11},e_{12}} are included.

Testing was done with the following lines substituted for last line of matrix:

# python code
[ 0,  0,   0,    0,   0,  0,   1,    0,  0,    0,   0,    0, -3.15]  # i7 = 3.15
[ 0,  0,   0,   r4,   0,  0,   0,    0,  0,    0,   0,    0, -6   ]  # e4 = 6
[ 0,  0,   r3,   0,   0,  0,   0,   r8,  0,    0,   0,    0, -5   ]  # e3 + e8 = 5

All tests produced a consistent value of  R . {\displaystyle R.}

#### Checking[edit | edit source]

##### Examples of current nodes[edit | edit source]

* i 1 + i 10 = i 2 {\displaystyle i_{1}+i_{10}=i_{2}}

* i 6 + i 8 = i 12 {\displaystyle i_{6}+i_{8}=i_{12}}

##### Examples of voltage loops[edit | edit source]

* Top and Bottom. e 1 + e 2 − e 4 − e 3 = 0 {\displaystyle e_{1}+e_{2}-e_{4}-e_{3}=0} e 5 + e 6 − e 8 − e 7 = 0 {\displaystyle e_{5}+e_{6}-e_{8}-e_{7}=0}

* Two sides in background. e 5 + e 10 − e 1 − e 9 = 0 {\displaystyle e_{5}+e_{10}-e_{1}-e_{9}=0} e 6 + e 12 − e 2 − e 10 = 0 {\displaystyle e_{6}+e_{12}-e_{2}-e_{10}=0}

* Two sides in foreground. e 7 + e 11 − e 3 − e 9 = 0 {\displaystyle e_{7}+e_{11}-e_{3}-e_{9}=0} e 8 + e 12 − e 4 − e 11 = 0 {\displaystyle e_{8}+e_{12}-e_{4}-e_{11}=0}

##### Python code[edit | edit source]

t1 = (
# 3 nodes on top
'i1 + i10 - i2',
'i1 + i3 - i9',
'i3 + i11 - i4',

# 3 nodes on bottom
'i10 + i6 - i5',
'i11 + i8 - i7',
'i6 + i8 - i12',

# Total input current equals total output current.
'i5 + i7 + i9 - i2 - i4 - i12',

# Voltage loops
# top
'e1 + e2 - e4 - e3',

# bottom
'e5 + e6 - e8 - e7',

# 2 sides in background
'e9 + e1 - e10 - e5',
'e10 + e2 - e12 - e6',

# 2 sides in foreground
'e9 + e3 - e11 - e7',
'e11 + e4 - e12 - e8',

# Some elaborate loops.
'- e6 + e10 + e2 - e4 - e3 - e9 + e7 + e8',
'e10 + e2 - e12 - e8 + e11 - e3 - e9 + e5',

for s1 in t1 : print(s1,eval(s1))

i1 + i10 - i2     1.2434497875801753e-14
i1 + i3 - i9     -1.509903313490213e-14
i3 + i11 - i4    -1.687538997430238e-14

i10 + i6 - i5     0.0
i11 + i8 - i7     0.0
i6 + i8 - i12     4.440892098500626e-15

i5 + i7 + i9 - i2 - i4 - i12    1.4210854715202004e-14

e1 + e2 - e4 - e3      -1.4210854715202004e-14
e5 + e6 - e8 - e7       0.0

e9 + e1 - e10 - e5     -5.684341886080802e-14
e10 + e2 - e12 - e6    -6.821210263296962e-13

e9 + e3 - e11 - e7     -6.821210263296962e-13
e11 + e4 - e12 - e8    -2.842170943040401e-14

- e6 + e10 + e2 - e4 - e3 - e9 + e7 + e8   -2.842170943040401e-14
e10 + e2 - e12 - e8 + e11 - e3 - e9 + e5    5.684341886080802e-14