Jump to content

Solving simultaneous equations

From Wikiversity

Introduction

[edit | edit source]

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

For example:

3x+2y−13=0 … (1)

2x−y−4=0 … (2)

Equations (1) and (2) are 2 simultaneous equations.

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

To calculate (x,y) common to both (1) and (2):

(2)∗2 … 4x−2y−8=0 … (3)

(3)+(1) … 7x−21=0; x=3.

Substitute 3 for x in (2): 2(3)−y−4=0; y=2.

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

In cartesian coordinate geometry of 2 dimensions, point (3,2) is the point of intersection of two lines (1) and (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) is a valid solution. See Figure 3 below.

The information contained in equations (1) and (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]

Implementation

[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

import decimal
dD = decimal.Decimal

def solve2by3 (input_, level=0) :
    '''
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.
    '''
    if level : thisName = 'solve2by3 (input_, level = {}) :'.format(level)
    else : thisName = 'solve2by3 (input_) :'
    try :
        error = 0
        try :
            input = [ list(v) for v in input_ ]
            (a1,b1,c1), (a2,b2,c2) = input
        except : input = 0
        input or {}['input_ not recognized.']
        #
        six_values = a1,b1,c1, a2,b2,c2
        for v in six_values :
            isinstance(v, (int, float, dD)) or {}['Non-numeric value in input.']
        set1 = { isinstance(v, dD) for v in six_values }
        if len(set1) == 2 :
            # Mix of Decimal and non-Decimal in input. Make all type Decimal.
            (a1,b1,c1, a2,b2,c2) = [ dD(str(v)) for v in six_values ]
        #
        a1 or b1 or {}['Row 1 empty.'] ; a1 or a2 or {}['Column 1 empty.']
        a2 or b2 or {}['Row 2 empty.'] ; b1 or b2 or {}['Column 2 empty.']
        #
        if not a1 :
            # Make a1 non-zero.
            (a1,b1,c1),(a2,b2,c2) = (a2,b2,c2), (a1,b1,c1)
        #
        if a2 :
            # Make a2 zero.
            if   a2 ==  a1 : a2,b2,c2 = a1-a2, b1-b2, c1-c2
            elif a2 == -a1 : a2,b2,c2 = a1+a2, b1+b2, c1+c2
            else :
                list1 = [ a2*v for v in (a1,b1,c1) ]
                list2 = [ a1*v for v in (a2,b2,c2) ]
                a2,b2,c2 = [ (v1-v2) for v1,v2 in zip (list1, list2) ]
            a2 and {}['Internal error. a2 should be 0.']
        b2 or {}['Parallel input.']
# The matrix is:
# a1, b1, c1  with a1 non-zero
#  0, b2, c2  and  b2 non-zero.
        # 0 x + b2 y + c2 = 0
        y = -c2/b2
        # a1 x + b1 y + c1 = 0
        x = -(b1*y + c1)/a1 # Here is why a1 must be non-zero.
        output = x,y
    except : error = str(sys.exc_info())[1:].split(',') ; error[1:] = [ ','.join(error[1:-1]) ]
    #
    if error :
        print (thisName)
        for v in error : print (' ', v.strip())
        if input :
            print ('  input of solve2by3() =')
            print ('   ', input[0])
            print ('   ', input[1])
        return None
    #
    return output

Examples

[edit | edit source]

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 (input_) :
  <class 'KeyError'>
  KeyError('Parallel input.')
  input of solve2by3() =
    [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 (input_) :
  <class 'KeyError'>
  KeyError('input_ not recognized.')
None

output = solve2by3([
  [3,  0, 2],
  [6,  0, 4]
])
print (output)
solve2by3 (input_) :
  <class 'KeyError'>
  KeyError('Column 2 empty.')
  input of solve2by3() =
    [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).
Equation of line is: 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 (x1,y1),(x2,y2) calculate equation of line containing these 2 points.

Equation of line is of format Ax+By+C=0,

For example: 3x+2y+10=0 … (1).

Equation (1) can be expressed as 6x+4y+20=0 or −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 and the 2 relevant equations are:

Ax1+By1+1=0

Ax2+By2+1=0


To solve for A,B:

x1A+y1B+1=0

x2A+y2B+1=0

Suppose the line is defined by points (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).

To improve appearance of (2):

# python code:

print (A/B)
0.37500000000000006

Let 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) becomes 3x+8y−17=0 … (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).
Equation of line is: 2x−3y=0.
Non-zero value for coefficient 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)?

As above assume that 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.

# 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 or 2x−3y=0

This line includes the origin. Therefore constant C cannot be non-zero.


Generally:

  • Without rotation, A,B=result and C=1.
  • With rotation left by one column, B,C=result and 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 unique 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) is valid. See Figure 6 below.

Auxiliary functions

[edit | edit source]
def two_planes_parallel (plane1, plane2) :
    '''
status = two_planes_parallel (plane1, plane2)
status = True if parallel.
    '''
    A1,B1,C1 = plane1[:3]
    A2,B2,C2 = plane2[:3]
    
    A3 = B1*C2 - B2*C1
    B3 = C1*A2 - C2*A1
    C3 = A1*B2 - A2*B1

    return ({ A3,B3,C3 } == {0})


def check_three_rows (input) :
    '''
result = check_three_rows (input)
result is None or string. 
    '''
    row0,row1,row2 = input

    if two_planes_parallel (row0,row1) : return 'Two rows parallel: 0,1'
    if two_planes_parallel (row0,row2) : return 'Two rows parallel: 0,2'
    if two_planes_parallel (row1,row2) : return 'Two rows parallel: 1,2'

    A0,B0,C0 = row0[:3] ; A1,B1,C1 = row1[:3] ; A2,B2,C2 = row2[:3]
    # Line at intersection of planes 1,2 has direction numbers:
    A3 = B1*C2 - B2*C1 ; B3 = C1*A2 - C2*A1 ; C3 = A1*B2 - A2*B1
    # Line at intersection of planes 0,2 has direction numbers:
    A4 = B0*C2 - B2*C0 ; B4 = C0*A2 - C2*A0 ; C4 = A0*B2 - A2*B0
    # Are 2 lines of intersection parallel?
    if two_planes_parallel ((A3,B3,C3), (A4,B4,C4)) :
        return 'Three rows form tent.'
    return None

Implementation

[edit | edit source]
def solve3by4 (input_, level=0) :
    '''
input_ is :
    [ [a0,b0,c0,d0],
      [a1,b1,c1,d1],
      [a2,b2,c2,d2] ]
    
representing system:
    a0*x + b0*y + c0*z + d0 = 0
    a1*x + b1*y + c1*z + d1 = 0
    a2*x + b2*y + c2*z + d2 = 0

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

input is not changed by this function.
    '''
    if level : thisName = 'solve3by4 (input_, level = {}) :'.format(level)
    else : thisName = 'solve3by4 (input_) :'
    try :
        error = 0
        try :
            input = [ list(v) for v in input_ ]
            (a0,b0,c0,d0), (a1,b1,c1,d1), (a2,b2,c2,d2) = input
        except : input = 0
        input or {}['input_ not recognized.']
        #
        twelve_values = a0,b0,c0,d0, a1,b1,c1,d1, a2,b2,c2,d2
        for v in twelve_values :
            isinstance(v, (int, float, dD)) or {}['Non-numeric value in input.']
        set1 = { isinstance(v, dD) for v in twelve_values }
        if len(set1) == 2 :
            # Mix of Decimal and non-Decimal in input. Make all type Decimal.
            (a0,b0,c0,d0, a1,b1,c1,d1, a2,b2,c2,d2) = [ dD(str(v)) for v in twelve_values ]
        #
        a0 or b0 or c0 or {}['Row 0 empty.'] ; a0 or a1 or a2 or {}['Column 0 empty.']
        a1 or b1 or c1 or {}['Row 1 empty.'] ; b0 or b1 or b2 or {}['Column 1 empty.']
        a2 or b2 or c2 or {}['Row 2 empty.'] ; c0 or c1 or c2 or {}['Column 2 empty.']
        #
        input = [ [a0,b0,c0,d0], [a1,b1,c1,d1], [a2,b2,c2,d2] ]
        result = check_three_rows (input) ; result and {}[result]
        #
        if not a0 :
            # Make a0 non-zero.
            if a1 : input[0],input[1] = input[1],input[0]
            else  : input[0],input[2] = input[2],input[0]
        #
# The matrix is:
# a0, b0, c0, d0 with a0 non-zero.
# a1, b1, c1, d1
# a2, b2, c2, d2
        # Process rows so that a1, a2 become zero.

        row0 = input[0] ; a0 = row0[0]
        for p in (1,2) :
            rowp = input[p] ; ap = rowp[0]
            if ap :
                if   ap == a0 : input[p] = [ (v1-v2) for (v1,v2) in zip(row0, rowp) ]
                elif ap == -a0: input[p] = [ (v1+v2) for (v1,v2) in zip(row0, rowp) ]
                else : input[p] = [ (v0*ap-vp*a0) for (v0,vp) in zip(row0, rowp) ]
                if input[p][0] : {}['Internal error. input[{}][0] should be 0.'.format(p)] 

        (a0,b0,c0,d0), (a1,b1,c1,d1), (a2,b2,c2,d2) = input
# The matrix is:
# a0, b0, c0, d0 with a0 non-zero.
#  0, b1, c1, d1
#  0, b2, c2, d2
        two_rows = (b1,c1,d1), (b2,c2,d2) 
        output = solve2by3 (two_rows, level-1)
        if output :
            y,z = output
            # a0 x + b0 y + c0 z + d0 = 0
            x = -(b0*y + c0*z + d0)/a0 # Here is why a0 must be non-zero.
            output = x,y,z
    except : error = str(sys.exc_info())[1:].split(',') ; error[1:] = [ ','.join(error[1:-1]) ]
    #
    if error :
        print (thisName)
        for v in error : print (' ', v.strip())
        if input :
            print ('  input of solve3by4() =')
            print ('   ', input[0])
            print ('   ', input[1])
            print ('   ', input[2])
        return None
    #
    return output

Examples

[edit | edit source]

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)
solve3by4 (input_) :
  <class 'KeyError'>
  KeyError('Three rows form tent.')
  input of solve3by4() =
    [20, 16, 2, 17]
    [-77, -27, -25, -24]
    [81, 45, 18, 43]
None

output = solve3by4([
    [34, -5, -21, 30],
    [-54, -33, 63, 62],
    [18, 11, -21, 38]
])
print (output)
solve3by4 (input_) :
  <class 'KeyError'>
  KeyError('Two rows parallel: 1,2')
  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 (input_) :
  <class 'KeyError'>
  KeyError('Non-numeric value in input.')
  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)=Ax2+Bx+C is defined by 3 points: (−2,9.0),(5,4.8),(10,13.8).


What is f(x)?


y=Ax2+Bx+C

A,B,C are 3 unknowns.

x2(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.2x2−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 is impossible.
Because plane is parallel to X axis, non-zero value for coefficient A is impossible.

In 3 dimensions a plane (Ax+By+Cz+D=0) is defined by 3 points: (5,3,−2),(−1,12,−8),(37,15,−10).

What is equation of plane?


Ax+By+Cz+D=0

xA+yB+zC+D=0

Assume that D=1.

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 and D=1.

With rotation left: B,C,D=result and A=1.

With another rotation left: C,D,A=result and 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: 0x+1y+1.5z+0=0 or 2y+3z=0

A better method
[edit | edit source]

There are four unknown values: A,B,C,D.

Create matrix 4 by 5:

# python code.

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

input = []

for (x_,y_,z_) in (pt1,pt2,pt3) :
    x,y,z = [ dD(v) for v in (x_,y_,z_) ]
    input += [ [ x,y,z,1,0 ] ]

# For valid plane at least one of A,B,C must be non-zero.
# For last condition let A + B + C = 5.
input += [ [ 1,1,1,0,-5 ] ]

A,B,C,D = solveMbyN(input)

print (input)
print ( '({})x + ({})y + ({})z + ({}) = 0'.format(A,B,C,D) )
[[ 5,  3,  -2, 1,  0], #  5A +  3B -  2C + 1D + 0 = 0
 [-1, 12,  -8, 1,  0], # -1A + 12B -  8C + 1D + 0 = 0
 [37, 15, -10, 1,  0], # 37A + 15B - 10C + 1D + 0 = 0
 [ 1,  1,   1, 0, -5]] #  1A +  1B +  1C + 0D - 5 = 0
 
(0)x + (2)y + (3)z + (0) = 0

Equation of plane is: 2y+3z=0.

Solving M by (M+1)

[edit | edit source]

Implementation

[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.

# python code

enable_reduceRow = 0
FPE = FloatingPointError # Not used by python. Used here as target of 'goto'.


def solveMbyN (input_, level=0) :
    '''
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 [, level])
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.
    '''
    thisName = 'solveMbyN (input_, level = {}) :'.format(level)

    try :
        error = 0
        try: input = [ list(v) for v in input_ ]
        except : input = 0
        input or {}['input_ not recognized.']

        number_of_rows = len(input)
        (number_of_rows > 1) or {}['number_of_rows must be > 1.']

        if number_of_rows == 2 :
            output = solve2by3(input, level)
            raise FPE # Equivalent to goto.
        if number_of_rows == 3 :
            output = solve3by4(input, level)
            raise FPE # Equivalent to goto.

        number_of_columns = number_of_rows + 1
        if level == 0 :
            dD_count = other_count = 0
            for row_number in range (number_of_rows-1,-1,-1) :
                row = input[row_number]
                # Check that each row is correct length.
                if (len(row) != number_of_columns) :
                    error = [ 'row # {} has bad length: {}'.format(row_number, len(row))]
                    error += [ 'Correct length is : {}'.format(number_of_columns)]
                    raise FPE
                for v in row :
                    # Check that all data is numeric.
                    isinstance(v, (int,float,dD)) or {}['Non-numeric value in input.']
                    # Record if type dD or not.
                    if isinstance(v, dD) : dD_count += 1
                    else : other_count += 1

            if dD_count and other_count :
                # Mix of types Decimal and non-Decimal in input. Make all values type Decimal.
                input = [ [ dD(str(p)) for p in row  ] for row in input ]

        # Each row must not be empty.
        for row_number in range (number_of_rows-1,-1,-1) :
            found = 0
            for v in input[row_number][:-1]:
                if v :
                    found = 1 ; break
            if not found :
                {}['Empty row in input: # {} of {} rows.'.format(row_number, number_of_rows)]

        # Arrange input if necessary so that a0 is non-zero.
        if not input[0][0] :
            for p in range (1, number_of_rows) :
                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],
        
        # If abs(level) is large, following test can produce error:
        # OverflowError('int too large to convert to float')
        # Solution is to convert values in input to type float or Decimal.
        dD(input[0][0]) ; float(input[0][0])

        # Each column must not be empty.
        for column_number in range (number_of_columns-2,-1,-1) :
            found = 0
            for row in input :
                if row[column_number] :
                    found = 1 ; break
            if not found :
                {}['Empty column in input: # {} of {} columns.'.format(column_number, number_of_columns)]
            
        if enable_reduceRow : # global variable
            # For matrices greater than 10 by 11 enable this piece of code.
            # This code calculates the max(abs) of all values in row,
            # divides all values by the max(abs) and updates row with result.
            # [27, -15, 42] becomes [27/42, -15/42, 42/42].
            # Without this code you may see:
            # solveMbyN (input, level = -52) :
            #   <class 'decimal.Overflow'>
            #   Overflow([<class 'decimal.Overflow'>])
            for row in input :
                max = 0
                for v in row :
                    if abs(v) > max : max = abs(v)
                if max < 1e100 : continue
                for column_number in range (number_of_columns-1,-1,-1) :
                    if row[column_number] : row[column_number] /= max

        # Process rows so that a1, a2, a3, .... become zero.

        row0 = input[0] ; a0 = row0[0]
        for p in range (1, number_of_rows) :
            rowp = input[p] ; ap = rowp[0]
            if ap :
                if   ap == a0 : input[p] = [ (v1-v2) for (v1,v2) in zip(row0, rowp) ]
                elif ap == -a0: input[p] = [ (v1+v2) for (v1,v2) in zip(row0, rowp) ]
                else : input[p] = [ (v0*ap-vp*a0) for (v0,vp) in zip(row0, rowp) ]
                if input[p][0] :
                    try: str1 = str(float(input[p][0]))
                    except : str1 = 'Error'
                    {}['Internal error. input[{}][0] = {}, should be 0.'.format(p,str1)] 

# 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],
        next_input = [ row[1:] for row in input[1:] ]
# next_input is :
# [ b1, c1, d1, e1],
# [ b2, c2, d2, e2],
# [ b3, c3, d3, e3],
        output = solveMbyN (next_input, level-1) # Here is where function solveMbyN() calls itself.
        if output :
            # input[0] = a0, b0, c0, d0, e0
            # output   =      x,  y,  z
            (len(input[0][1:-1]) == len(output)) or {}["Internal error. Inputs to zip() don't match."]
            listOfProducts = [ v1*v2 for v1,v2 in zip(input[0][1:-1], output) ]
            # listOfProducts = [ b0*x, c0*y, d0*z ]
            sumOfProducts = input[0][-1]
            for v in listOfProducts : sumOfProducts += v
# 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.
            output = (w,) + output

    except FPE : pass # Target of 'goto'.
    except : error = str(sys.exc_info())[1:].split(',') ; error[1:] = [ ','.join(error[1:-1]) ]
    #
    if error :
        print (thisName)
        for v in error : print (' ', v.strip())
        if input :
            print ('  Length of input =', len(input)) 
            for p in range (0, len(input)) :
                if p >= 8 : break
                try : row = [ float(v) for v in input_[p] ]
                except : row = 'Error in this row.'
                print ('  input_[{}] ='.format(p), row)
        return None
    #
    return output

Examples

[edit | edit source]

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:

x2+y2+z2+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+(x2+y2+z2)=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 :

x2+y2+z2−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)=Ax5+Bx4+Cx3+Dx2+Ex+F is defined by 6 points: (−8,0),(−6,0),(−3,0),(1,0),(3,0),(5,10).


What is f(x)?


y=f(x)=Ax5+Bx4+Cx3+Dx2+Ex+F

A,B,C,D,E,F are 6 unknowns.

x5(A)+x4(B)+x3(C)+x2(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:

1A=915.2;  BA=13;  CA=25;  DA=−165;  EA=−306;  FA=432.


y=f(x)=x5+13x4+25x3−165x2−306x+432915.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 between points P1,P3?

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 P4 currents i1,i7 flow towards point; current i4 flows away from point.
i1+i7−i4=0.

In any closed loop sum of voltages = 0.
In loop containing points P1,P2 voltages e2,e8 are clockwise; voltage e3 is counter-clockwise.
e2+e8−e3=0.

Direction of flow through r7,r8 is assumed and is subject to correction.

Voltage across r1:

Using E=IR, e1=i1⋅r1=r1⋅a.


Similarly:

e2=r2⋅b.

e3=r3⋅c.

……

e8=r8⋅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 ex,ix. However, the ratio exix=rx 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))
s1 = 'i1 + i2 + i3' ; print(s1,eval(s1))
values_of_i (0.45727389222364606,  0.3065353746543468,  0.23619073312200717, 
             0.4350171983543536,   0.31685438726647336, 0.24812841437917316, 
            -0.022256693869292507, 0.011937681257165982)
i1 + i2 + i3 1.0

i7 has negative value. Reverse i7 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 i7 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

E1 = e3+e6
I1 = i1 + i2 + i3
R1 = E1/I1
s1 = 'R1' ; print(s1,eval(s1))

E2 = e1 + e7 + e5
I2 = i4 + i5 + i6
R2 = E2/I2
s1 = 'R2' ; print(s1,eval(s1))
R1 21.973426856410605
R2 21.973426856410605

R=R1=R2

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 between points P1,P2?


# 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
]

import decimal
dD = decimal.Decimal
dgt = decimal.getcontext()
Precision = dgt.prec = 30
Tolerance = dD("1e-" + str(Precision-2))

# Convert all values in input to decimal objects with precision of 30.

input = [ [ dD(str(p)) for p in v  ] for v in input ]

i1,i2,i3,i4,i5,i6,i7,i8,i9,i10,i11,i12 = values_of_i = solveMbyN (input)
# values_of_i

5.5027171492204899777282850777     9.3809275973899113038721525414     1.5999999999999999999999999999  # i3 should be 1.6.
4.8661430860000781463681475402     6.12901340210213730316883522816    2.25080295393271597702496776458
4.68526862814050716992927753670    1.41912554214042902356112999643    7.10271714922048997772828507766
3.87821044816942132614386746358    3.26614308600007814636814754027    3.66992849607314500058609776103

Compute R

[edit | edit source]
# python code

values_of_e = e1,e2,e3,e4,e5,e6,e7,e8,e9,e10,e11,e12 = [ (i*r) for (i,r) in zip(values_of_i, values_of_r) ]
values_of_e
55.0271714922048997772828507770    187.618551947798226077443050828    47.9999999999999999999999999970 
194.645723440003125854725901608    306.450670105106865158441761408    135.048177235962958621498065875
327.968803969835501895049427569    113.530043371234321884890399714    639.244543429844097995545656989
387.821044816942132614386746358    359.275739460008596100496229430    440.391419528777400070331731324

e3 should be 48, i3∗r3 or 1.6∗30.

E1 = e5 + e10 + e2
I1 = i9 + i5 + i7
R1 = E1/I1
s1 = 'R1' ; print(s1,eval(s1))

E2 = e7 + e8 - e6 + e10 - e1 + e3 + e4
I2 = i2 + i4 + i12
R2 = E2/I2
s1 = 'R2' ; print(s1,eval(s1))
R1 49.2208688540148837936367036806
R2 49.2208688540148837936367036803

Display results with precision of 28:

dgt.prec = 28
R1 += 0 ; R2 += 0
dgt.prec = Precision

s1 = 'R1' ; print(s1,eval(s1))
s1 = 'R2' ; print(s1,eval(s1))
R1 49.22086885401488379363670368 
R2 49.22086885401488379363670368

R=R1=R2.

Review

[edit | edit source]

In matrix all values i1,i2,…,i11,i12 and e1,e2,…,e11,e12 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.

Checking

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

Examples of voltage loops
[edit | edit source]

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.
'e8 - e6 + e10 + e2 - e4 - e3 - e9 + e7',
'e10 + e2 - e12 - e8 + e11 - e3 - e9 + e5',
)

Result of each of the above calculations should be 0.

Because of small rounding errors, result may not be exactly 0.

See values_of_i and values_of_e above.

This code verifies that result is close enough to 0 to be acceptable.

For example:

result=e10+e2−e12−e8+e11−e3−e9+e5.

v1=e10+e2+e11+e5, sum of positive values.

status = (abs(result)<(v1∗Tolerance)). status should be True.

# python code

import re

for str1 in t1 :
    str2 = re.sub ('\+', ';+', str1)
    str2 = re.sub ('\-', ';-', str2)
    L1 = str2.split(';')
    positives = ''.join([ v for v in L1 if '-' not in v ])
    result = eval(str1)
    v1 = eval( positives )
    status = (abs(result) < (v1*Tolerance))
    str3 = (str1+(' '*40))[:42]
    str4 = ((' '*10) + str(result))[-10:]
    str5 = '{}{}    {}'.format(str3,str4,status)
    print ( str5 )
Calculation Result Result Acceptable
i1+i10−i2 −1.2E−28 True
i1+i3−i9 −6E−29 True
i3+i11−i4 −3E−29 True
i10+i6−i5 0E−29 True
i11+i8−i7 0E−29 True
i6+i8−i12 −2E−29 True
i5+i7+i9−i2−i4−i12 −3E−29 True
e1+e2−e4−e3 0E−28 True
e5+e6−e8−e7 0E−27 True
e9+e1−e10−e5 0E−27 True
e10+e2−e12−e6 −1.3E−26 True
e9+e3−e11−e7 −1.3E−26 True
e11+e4−e12−e8 0E−27 True
e8−e6+e10+e2−e4−e3−e9+e7 0E−27 True
e10+e2−e12−e8+e11−e3−e9+e5 0E−27 True

Big Matrix 64 by 65

[edit | edit source]
Diagram of resistive network containing 64 resistors.
Positive current flow is assumed to be from bottom to top or from left to right.

A network containing 64 resistors is connected as shown in diagram.

What is resistance R between points P1, P2?

# python code
values_of_R = (
    R10, R11, R12, R13, R14, R15, R16, R17, R18, R19, 
    R20, R21, R22, R23, R24, R25, R26, R27, R28, R29, 
    R30, R31, R32, R33, R34, R35, R36, R37, R38, R39,
    R40, R41, R42, R43, R44, R45, R46, R47, R48, R49, 
    R50, R51, R52, R53, R54, R55, R56, R57, R58, R59, 
    R60, R61, R62, R63, R64, R65, R66, R67, R68, R69,
    R70, R71, R72, R73, 
) = [ v for v in range (100,731,10) ]

While this resistive circuit may be very theoretical, it lends itself well to the creation of a big matrix, 64 by 65, meaning that there are 64 resistors and 64 simultaneous equations to be solved.

Create matrix

[edit | edit source]

An examination of the circuit produces the following conditions:

# python code

iconditions = [
              "+ i10 + i70 - i44" ,        "+ i11 - i10 - i49" ,        "+ i12 - i11 - i54",
              "+ i13 - i12 - i59" ,        "+ i14 - i13 - i64" ,        "+ i15 + i44 - i43",
  "+ i16 + i49 - i15 - i48 - i70" ,  "+ i17 + i54 - i16 - i53" ,  "+ i18 + i59 - i17 - i58",
  "+ i19 + i64 + i71 - i18 - i63" ,        "+ i69 - i19 - i68" ,        "+ i20 + i43 - i42",
        "+ i21 + i48 - i20 - i47" ,  "+ i22 + i53 - i21 - i52" ,  "+ i23 + i58 - i22 - i57",
        "+ i24 + i63 - i23 - i62" ,        "+ i68 - i24 - i67" ,        "+ i25 + i42 - i41",
        "+ i26 + i47 - i25 - i46" ,  "+ i27 + i52 - i26 - i51" ,  "+ i28 + i57 - i27 - i56",
        "+ i29 + i62 - i28 - i61" ,        "+ i67 - i29 - i66" ,        "+ i30 + i41 - i40",
  "+ i31 + i46 - i30 - i45 - i72" ,  "+ i32 + i51 - i31 - i50" ,  "+ i33 + i56 - i32 - i55",
  "+ i34 + i61 + i73 - i33 - i60" ,        "+ i66 - i34 - i65" ,        "+ i36 + i45 - i35",
              "+ i37 + i50 - i36" ,        "+ i38 + i55 - i37" ,        "+ i39 + i60 - i38",
              "+ i65 - i39 - i73"
]

sx = 'len(iconditions)' ; print (sx, '=', eval(sx))
len(iconditions) = 34
econditions = [
  "+ e40 + e30 - e35 - e45" ,  "+ e41 + e25 - e30 - e46" ,  "+ e42 + e20 - e25 - e47",
  "+ e43 + e15 - e20 - e48" ,  "+ e44 + e10 - e15 - e49" ,  "+ e45 + e31 - e36 - e50",
  "+ e46 + e26 - e31 - e51" ,  "+ e47 + e21 - e26 - e52" ,  "+ e48 + e16 - e21 - e53",
  "+ e49 + e11 - e16 - e54" ,  "+ e50 + e32 - e37 - e55" ,  "+ e51 + e27 - e32 - e56",
  "+ e52 + e22 - e27 - e57" ,  "+ e53 + e17 - e22 - e58" ,  "+ e54 + e12 - e17 - e59",
  "+ e55 + e33 - e38 - e60" ,  "+ e56 + e28 - e33 - e61" ,  "+ e57 + e23 - e28 - e62",
  "+ e58 + e18 - e23 - e63" ,  "+ e59 + e13 - e18 - e64" ,  "+ e60 + e34 - e39 - e65",
  "+ e61 + e29 - e34 - e66" ,  "+ e62 + e24 - e29 - e67" ,  "+ e63 + e19 - e24 - e68",
  "+ e64 + e14 - e19 - e69" ,        "+ e44 + e70 - e15" ,        "+ e64 + e14 - e71",
        "+ e40 + e30 - e72" ,        "+ e60 + e73 - e39"
]

sx = 'len(econditions)' ; print (sx, '=', eval(sx))
len(econditions) = 29

In iconditions and econditions there are 63 conditions that include all values of i and e. These 63 conditions are used to create matrix.

The 64th condition is added manually.

The matrix is:

matrix = [
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,],
  [-1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,],
  [0,-1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,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,0,0,0,730,0,],
  [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,-1,],
]

sx = 'len(matrix)' ; print (sx, '=', eval(sx))
len(matrix) = 64

Bottom line of matrix is equivalent to: (1)i10+(−1)=0 or i10=1.

First result

[edit | edit source]

Calculate values_of_i :

print ('\nWhen ints become huge:')
values_of_i = solveMbyN (matrix)
sx = 'values_of_i' ; print (sx, '=', eval(sx))
When ints become huge:
solveMbyN (input_, level = -41) :
  <class 'OverflowError'>
  OverflowError('int too large to convert to float')
  Length of input = 23
  input_[0] = Error in this row.
  input_[1] = Error in this row.
  input_[2] = Error in this row.
  input_[3] = [-7.048390979785929e+176, 1.1003906499643917e+176, 1.5070856217200028e+174, 1.955699032422673e+173, ...... 0.0]
  input_[4] = [510.0, 0.0, 0.0, 0.0, 320.0, -1150.0, 270.0, 0.0, 0.0, 320.0, -590.0, 270.0, 0.0, 0.0, 320.0, -590.0, ...... 0.0]
  input_[5] = [0.0, 520.0, 0.0, 0.0, 0.0, 270.0, -1060.0, 220.0, 0.0, 0.0, 270.0, -490.0, 220.0, 0.0, 0.0, 270.0, ...... 0.0]
  input_[6] = [0.0, 0.0, 530.0, 0.0, 0.0, 0.0, 220.0, -970.0, 170.0, 0.0, 0.0, 220.0, -390.0, 170.0, 0.0, 0.0, 220.0, ...... 0.0]
  input_[7] = Error in this row.
values_of_i = None
print ('\nConvert input to floats:')
matrix1 = [ list(v) for v in matrix ]
matrix1[0][0] = float(matrix1[0][0])
values_of_i = solveMbyN (matrix1)
sx = 'values_of_i' ; print (sx, '=', eval(sx))
Convert input to floats:
solveMbyN (input_, level = -40) :
  <class 'KeyError'>
  KeyError('Internal error. input[1][0] = nan, should be 0.')
  Length of input = 24
  input_[0] = [-1.7360455505275518e+174, 1.4096781959571856e+174, -2.2007812999287836e+173, -3.014171243439967e+171, ...... 0.0]
  input_[1] = [-3.7974175914221006e+162, 3.309163202261842e+161, 2.4114748810711523e+162, -4.426635506031073e+161, ...... 0.0]
  input_[2] = [-2.4975968400753463e+139, 5.704494025143264e+138, -2.7146889822895123e+138, 1.4057733058789272e+139, ...... 0.0]
  input_[3] = [7.667237322891148e+182, -1.7692205437935546e+182, -2.341735229706114e+180, 4.1615580570562045e+181, ...... -0.0]
  input_[4] = [-500.0, 0.0, 0.0, 0.0, 0.0, 1240.0, -320.0, 0.0, 0.0, 0.0, 690.0, -320.0, 0.0, 0.0, 0.0, 690.0, -320.0, ...... 0.0]
  input_[5] = [0.0, 510.0, 0.0, 0.0, 0.0, 320.0, -1150.0, 270.0, 0.0, 0.0, 320.0, -590.0, 270.0, 0.0, 0.0, 320.0, -590.0, ...... 0.0]
  input_[6] = [0.0, 0.0, 520.0, 0.0, 0.0, 0.0, 270.0, -1060.0, 220.0, 0.0, 0.0, 270.0, -490.0, 220.0, 0.0, 0.0, 270.0, ...... 0.0]
  input_[7] = [0.0, 0.0, 0.0, 530.0, 0.0, 0.0, 0.0, 220.0, -970.0, 170.0, 0.0, 0.0, 220.0, -390.0, 170.0, 0.0, 0.0, ...... 0.0]
values_of_i = None
Success
[edit | edit source]
print ('\nSet global variable enable_reduceRow:')
enable_reduceRow = 1
values_of_i = solveMbyN (matrix1)

Success:

Set global variable enable_reduceRow:
i10 = 1.0000000000000049 ; i11 = 1.643236374952256  ; i12 = 2.316468657232291 ; i13 = 3.0507456069545116
i14 = 3.908085797412319  ; i15 = 0.5970301043016677 ; i16 = 0.8274774533758196; i17 = 1.225283946841065 
i18 = 1.5617922630400025 ; i19 = 0.369273540611037  ; i20 = 0.3858385105885722; i21 = 0.6270001063825295 
i22 = 0.7040607630623028 ; i23 = 0.6129764173251202 ; i24 = 0.298576896960036 ; i25 = 0.44960726241462035
i26 = 0.7812212615819403 ; i27 = 0.7165761045550649 ; i28 = 0.5186812635664733; i29 = 0.266770119423805
i30 = 0.5896680016803755 ; i31 = 1.475274068751303  ; i32 = 0.9632057010422588; i33 = 0.5801910619990465
i34 = 0.30799558703953794; i35 = 2.4693659910366916 ; i36 = 1.583773707944257 ; i37 = 1.0123878002551232
i38 = 0.6135963601029517 ; i39 = 0.3175434839291887 ; i40 = 2.7147355593758107; i41 = 2.125067557695435
i42 = 1.6754602952808149 ; i43 = 1.2896217846922426 ; i44 = 0.692591680390575 ; i45 = 0.8855922830924343
i46 = 1.7538676385971101 ; i47 = 1.4222536394297902 ; i48 = 1.181092043635833 ; i49 = 0.6432363749522512
i50 = 0.5713859076891339 ; i51 = 1.0834542753981782 ; i52 = 1.1480994324250535; i53 = 1.0710387757452802
i54 = 0.6732322822800348 ; i55 = 0.39879144015217155; i56 = 0.7818060791953839; i57 = 0.9797009201839757
i58 = 1.0707852659211583 ; i59 = 0.7342769497222208 ; i60 = 0.2960528761737631; i61 = 0.6419329634235106
i62 = 0.8938441075661789 ; i63 = 1.2082436279312632 ; i64 = 0.8573401904578074; i65 = 0.2438588716389498
i66 = 0.5518544586784879 ; i67 = 0.8186245781022927 ; i68 = 1.1172014750623287; i69 = 1.4864750156733657
i70 = -0.3074083196094299; i71 = 1.5434221599024214 ; i72 = 1.7538814225756034; i73 = -0.07368461229023889
# Notice that i70, i73 are negative.
Check all conditions
[edit | edit source]

The following conditions are included for testing:

more_econditions = [
  "+ e49 + e70 - e10",
  "+ e19 + e69 - e71",
  "+ e35 + e45 - e72",
  "+ e65 + e73 - e34",
] 

extra_test_conditions = [
  "+ e40 + e41 + e42 + e43 + e44 + e10 - e35 - e45 - e46 - e47 - e48 - e49",
  "+ e45 + e46 + e47 + e48 + e49 + e11 - e36 - e50 - e51 - e52 - e53 - e54",
  "+ e50 + e51 + e52 + e53 + e54 + e12 - e37 - e55 - e56 - e57 - e58 - e59",
  "+ e55 + e56 + e57 + e58 + e59 + e13 - e38 - e60 - e61 - e62 - e63 - e64",
  "+ e60 + e61 + e62 + e63 + e64 + e14 - e39 - e65 - e66 - e67 - e68 - e69",
  "+ e44 + e10 + e11 + e12 + e13 + e14 - e69 - e15 - e16 - e17 - e18 - e19",
  "+ e43 + e15 + e16 + e17 + e18 + e19 - e68 - e20 - e21 - e22 - e23 - e24",
  "+ e42 + e20 + e21 + e22 + e23 + e24 - e67 - e25 - e26 - e27 - e28 - e29",
  "+ e41 + e25 + e26 + e27 + e28 + e29 - e66 - e30 - e31 - e32 - e33 - e34",
  "+ e40 + e30 + e31 + e32 + e33 + e34 - e65 - e35 - e36 - e37 - e38 - e39",
  "+ i35 + i40 + i72 - i14 - i69 - i71",
  """+ e40 + e41 + e42 + e43 + e44 + e10 + e11 + e12 + e13 + e14
      = e35 + e36 + e37 + e38 + e39 + e65 + e66 + e67 + e68 + e69""",
  """+ e40 + e41 + e42 + e43 + e15 + e16 + e17 + e18 + e19 + e69
      = e35 + e36 + e37 + e38 + e60 + e61 + e62 + e63 + e64 + e14""",
  """+ e40 + e41 + e42 + e20 + e21 + e22 + e23 + e24 + e68 + e69
      = e35 + e36 + e37 + e55 + e56 + e57 + e58 + e59 + e13 + e14""",
  """+ e40 + e41 + e25 + e26 + e27 + e28 + e29 + e67 + e68 + e69
      = e35 + e36 + e50 + e51 + e52 + e53 + e54 + e12 + e13 + e14""",
  """+ e40 + e30 + e31 + e32 + e33 + e34 + e66 + e67 + e68 + e69
      = e35 + e45 + e46 + e47 + e48 + e49 + e11 + e12 + e13 + e14"""
]

Two Auxiliary Functions:

def sum_zero (values, tolerance = None) :
    '''
This function calculates sum of all values in input.
If sum is very close to zero, sum is returned as zero.
For example: sum_zero((2, -1.99999999999999999999999))
is returned as 0.
    '''
    if tolerance == None : tolerance = Tolerance

    sump = sumn = 0
    for v in values :
        if v > 0 : sump += v
        elif v < 0 : sumn -= v
    max = (sumn, sump)[sump > sumn]
    sum = sump - sumn
    if abs(sum) < tolerance: return type(sum)(0)
    if abs(sum) < tolerance*max : return type(sum)(0)
    return sum

def convert_str_to_tuple (input_str) :
    '''
input is str like "+ i35 + i40 + i72 - i14 - i69 - i71"
    or "+ i35 + i40 + i72 == i14 + i69 + i71"
This function returns tuple : (i35, i40, i72, - i14, - i69, - i71)
    '''
    str0 = ' '.join(input_str.split())
    if '=' in str0 :
        if '==' in str0 : splitter = '=='
        else : splitter = '='
        str1,str2 = str0.split(splitter)
        t1 = convert_str_to_tuple (str1) 
        t2 = convert_str_to_tuple (str2) 
        return t1 + tuple(-v for v in t2)
    str1 = ';+'.join(str0.split('+'))
    str2 = ';-'.join(str1.split('-'))
    str3 = ','.join([ v for v in str2.split(';') if v.strip() ])
    return eval(str3 + ',')

Check 83 conditions:

all_conditions = (iconditions + econditions + more_econditions + extra_test_conditions)

# Following string is executable python code.
exc_str_64ei = """
( i10, i11, i12, i13, i14, i15, i16, i17, i18, i19, 
  i20, i21, i22, i23, i24, i25, i26, i27, i28, i29, 
  i30, i31, i32, i33, i34, i35, i36, i37, i38, i39,
  i40, i41, i42, i43, i44, i45, i46, i47, i48, i49, 
  i50, i51, i52, i53, i54, i55, i56, i57, i58, i59, 
  i60, i61, i62, i63, i64, i65, i66, i67, i68, i69,
  i70, i71, i72, i73, 
) = values_of_i

values_of_e = [ i*R for i,R in zip ( values_of_i, values_of_R ) ]

( e10, e11, e12, e13, e14, e15, e16, e17, e18, e19, 
  e20, e21, e22, e23, e24, e25, e26, e27, e28, e29, 
  e30, e31, e32, e33, e34, e35, e36, e37, e38, e39,
  e40, e41, e42, e43, e44, e45, e46, e47, e48, e49, 
  e50, e51, e52, e53, e54, e55, e56, e57, e58, e59, 
  e60, e61, e62, e63, e64, e65, e66, e67, e68, e69,
  e70, e71, e72, e73, 
) = values_of_e
"""
exec(exc_str_64ei)

tolerance = 1e-15
count = errors = 0
for condition in all_conditions :
    t1 = convert_str_to_tuple (condition)
    sum = sum_zero(t1, tolerance)
    count += 1
    if sum :
        sx = 'sum, condition' ; print (sx, '=', eval(sx))
        errors += 1

print ('Number of tests =', count)
print ('Number of errors =', errors)

With tolerance of 1e−15 there are 21 errors:

sum, condition = (1.2505552149377763e-12, '+ e48 + e16 - e21 - e53')
sum, condition = (1.2505552149377763e-12, '+ e49 + e11 - e16 - e54')
sum, condition = (7.958078640513122e-13, '+ e52 + e22 - e27 - e57')
sum, condition = (1.1368683772161603e-12, '+ e53 + e17 - e22 - e58')
sum, condition = (2.2737367544323206e-12, '+ e54 + e12 - e17 - e59')
......      ......
Number of tests = 83
Number of errors = 21

With tolerance of 1e−14 there are no errors.

Calculate R

[edit | edit source]
matrix corrected
[edit | edit source]
Diagram of network containing 64 resistors
Diagram has been corrected to show positive direction of current through R70, R73.

Original assumptions were that currrent flow through 64 resistors would be from bottom to top or from left to right.

Examination of results shows that flow through R70, R73 is from right to left.

Corrected diagram conforms to assumption that all current flow is from bottom towards top or from left to right.

matrix1 = [ list(v) for v in matrix ] # Deep copy.
# e70, i70 are at position 60 in each row of matrix1.
# e73, i73 are at position 63 in each row of matrix1.
for row in matrix1 :
    for p in (60,63) :
        if row[p] : row[p] *= -1

matrix1 is corrected version of matrix.

Circuit parameters
[edit | edit source]
values_of_circuit_current = ('i40 + i72 + i35', 'i14 + i71 + i69') 

values_of_circuit_voltage = (
    'e72 + e46 + e47 + e48 + e70 + e10 + e11 + e12 + e13 + e14',
    'e35 + e36 + e37 + e38 + e39 + e73 + e61 + e62 + e63 + e71',
    
    'e40 + e41 + e42 + e43 + e44 + e10 + e11 + e12 + e13 + e14',
    'e35 + e36 + e37 + e38 + e39 + e65 + e66 + e67 + e68 + e69',

    'e40 + e41 + e42 + e43 + e15 + e16 + e17 + e18 + e19 + e69',
    'e40 + e41 + e42 + e20 + e21 + e22 + e23 + e24 + e68 + e69',
    'e40 + e41 + e25 + e26 + e27 + e28 + e29 + e67 + e68 + e69',
    'e40 + e30 + e31 + e32 + e33 + e34 + e66 + e67 + e68 + e69',

    'e35 + e45 + e46 + e47 + e48 + e49 + e11 + e12 + e13 + e14',
    'e35 + e36 + e50 + e51 + e52 + e53 + e54 + e12 + e13 + e14',
    'e35 + e36 + e37 + e55 + e56 + e57 + e58 + e59 + e13 + e14',
    'e35 + e36 + e37 + e38 + e60 + e61 + e62 + e63 + e64 + e14',

    'e72 + e31 - e50 + e37 + e38 + e39 + e73 + e61 - e28 + e57' + (
        '- e22 + e53 - e16 + e70 + e10 + e11 + e12 + e13 + e14')
)
Using floating point numbers:
[edit | edit source]
# python code:

number_of_columns = 65
one = 1 ; Tolerance = 1e-14

# Following string is executable python code:
exc_str1 = """
Values_of_R = []

for I in range(10, 74):
    new_line = [ 0 ] * number_of_columns
    place_in_line = I - 10
    new_line[place_in_line] = one
    i = (I)/10
    new_line[-1] = -i
    matrix1[-1] = new_line

    values_of_i = solveMbyN (matrix1)
    
    exec(exc_str_64ei)

    for Istr in values_of_circuit_current :
        I = eval(Istr)
        for Estr in values_of_circuit_voltage :
            E = eval(Estr) ; R = E/I ; Values_of_R += [R]

print ('Number of calculations =', len(Values_of_R))
tmp = sorted(Values_of_R)
minR = tmp[0] ; maxR = tmp[-1]
s1 = 'minR' ; print(s1,'=',eval(s1))
s1 = 'maxR' ; print(s1,'=',eval(s1))
sum = sum_zero((minR, -maxR))
s1 = 'sum' ; print(s1,'=',eval(s1))
if not sum : print ('With Tolerance of {}, both minR and maxR are equal.'.format(Tolerance))
"""

exec(exc_str1)
Number of calculations = 1664
minR = 723.9285759884408
maxR = 723.9285759884439
sum = 0.0
With Tolerance of 1e-14, both minR and maxR are equal.

This code produced 1664 values of R from minR through maxR, both included.

With tolerance of 1e−14 all values of resistance R = 723.92857598844.

Using Decimal numbers:
[edit | edit source]

With enable_reduceRow clear :

enable_reduceRow = 0
Precision = dgt.prec = 60
Tolerance = dD('1e-' + str(Precision-3))
one = dD(1)

try : exec(exc_str1)
except : pass
solveMbyN (input_, level = -52) :
  <class 'decimal.Overflow'>
  Overflow([<class 'decimal.Overflow'>])
  Length of input = 12
  input_[0] = [-inf, inf, -inf, inf, inf, -inf, inf, -inf, inf, -inf, -inf, inf, 0.0]
  input_[1] = [inf, -inf, -inf, -inf, -inf, inf, -inf, -inf, -inf, -inf, inf, -inf, 0.0]
  input_[2] = [-inf, inf, inf, inf, -inf, -inf, inf, inf, -inf, inf, inf, inf, 0.0]
  input_[3] = [inf, -inf, -inf, inf, -inf, inf, -inf, -inf, inf, -inf, -inf, inf, 0.0]
  input_[4] = [620.0, 0.0, 0.0, 0.0, 290.0, -1200.0, 240.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  input_[5] = [0.0, 630.0, 0.0, 0.0, 0.0, 240.0, -1110.0, 190.0, 0.0, 0.0, 0.0, 0.0, 0.0]
  input_[6] = [inf, -inf, inf, -inf, -inf, inf, -inf, -inf, inf, -inf, inf, -inf, 0.0]
  input_[7] = [-inf, inf, inf, inf, inf, -inf, inf, inf, inf, inf, -inf, inf, 0.0]

With enable_reduceRow set :

enable_reduceRow = 1

exec(exc_str1)
Number of calculations = 1664
minR = 723.928575988442547699732563335265663515838075102937503680560
maxR = 723.928575988442547699732563335265663515838075102937503680663
sum = 0
With Tolerance of 1E-57, both minR and maxR are equal.

With tolerance of 1e−57 all values of resistance R = 723.928575988442547699732563335265663515838075102937503681.