#! /usr/bin/env python

import openturns as ot

ot.TESTPREAMBLE()


# First : an n by n case
matrix1 = ot.Matrix(2, 2)
matrix1[0, 0] = 1.0
matrix1[1, 0] = 2.0
matrix1[0, 1] = 5.0
matrix1[1, 1] = 12.0
print("matrix1=" + repr(matrix1))

pt1 = ot.Point()
pt1.add(5.0)
pt1.add(0.0)
print("pt1=" + repr(pt1))

result1 = ot.Point()
result1 = matrix1.solveLinearSystem(pt1)
print("result1=" + repr(result1))

# Second : an n by p case, n < p
matrix2 = ot.Matrix(2, 3)
matrix2[0, 0] = 1.0
matrix2[1, 0] = 2.0
matrix2[0, 1] = 5.0
matrix2[1, 1] = 12.0
matrix2[0, 2] = 3.0
matrix2[1, 2] = 4.0
print("matrix2=" + repr(matrix2))

pt2 = ot.Point()
pt2.add(5.0)
pt2.add(0.0)
print("pt2=" + repr(pt2))

result2 = ot.Point()
result2 = matrix2.solveLinearSystem(pt2)
print("result2=" + repr(result2))

# Third : an n by p case, n > p
matrix3 = ot.Matrix(3, 2)
matrix3[0, 0] = 1.0
matrix3[1, 0] = 2.0
matrix3[2, 0] = 4.0
matrix3[0, 1] = 5.0
matrix3[1, 1] = 12.0
matrix3[2, 1] = 3.0

print("matrix3=" + repr(matrix3))

pt3 = ot.Point()
pt3.add(5.0)
pt3.add(0.0)
pt3.add(1.0)
print("pt3=" + repr(pt3))

result3 = ot.Point()
result3 = matrix3.solveLinearSystem(pt3)
print("result3=" + repr(result3))

b1 = ot.Matrix(2, 4)
b1[0, 0] = 5.0
b1[1, 0] = 1.0
b1[0, 1] = 10.0
b1[1, 1] = 2.0
b1[0, 2] = 15.0
b1[1, 2] = 3.0
b1[0, 3] = 20.0
b1[1, 3] = 4.0
print("b1=" + repr(b1))
result4 = matrix1.solveLinearSystem(b1)
print("result4=" + repr(result4))
result4 = matrix1.solveLinearSystemInPlace(b1)
print("result4=" + repr(result4))

result5 = matrix2.solveLinearSystem(b1)
print("result5=" + repr(result5))
result5 = matrix2.solveLinearSystemInPlace(b1)
print("result5=" + repr(result5))

b2 = ot.Matrix(3, 4)
b2[0, 0] = 5.0
b2[1, 0] = 1.0
b2[2, 0] = -2.0
b2[0, 1] = 10.0
b2[1, 1] = 2.0
b2[2, 1] = -4.0
b2[0, 2] = 15.0
b2[1, 2] = 3.0
b2[2, 2] = -6.0
b2[0, 3] = 20.0
b2[1, 3] = 4.0
b2[2, 3] = -8.0
print("b2=" + repr(b2))
result6 = matrix3.solveLinearSystem(b2)
print("result6=" + repr(result6))
result6 = matrix3.solveLinearSystemInPlace(b2)
print("result6 = " + repr(result6))
