We have 3 equations with 3 unknowns x, y, and z and we are to solve for these unknowns.
Equations 4x -3y +z = -10, 2x +y +3z = 0, and -x +2y -5z = 17 will be passed in as an array of [[4, -3, 1, -10], [2, 1, 3, 0], [-1, 2, -5, 17]] and the result should be returned as an array like [1, 4, -2] (i.e. [x, y, z]).

Note: In C++ do not use new or malloc to allocate memory for the returned variable as allocated memory will not be freed in the Test Cases. Setting the variable as static will do.

Source: codewars | Difficulty: EASY
Tags: Mathematics

Starter code: def solve_eq(eq):
	

Solutions:
import numpy as np

def solve_eq(eq):
	a = np.array([arr[:3] for arr in eq])
	b = np.array([arr[-1] for arr in eq])
	return [round(x) for x in np.linalg.solve(a, b)]


import numpy as np

def solve_eq(eq):
	m = np.array(eq)
	return np.linalg.solve(m[:, :3], m[:, 3]).round().astype(int).tolist()


def solve_eq(mat):
	determinant = lambda m: m[0][0] * (m[1][1] * m[2][2] - m[2][1] * m[1][2]) - m[0][1] * (m[1][0] * m[2][2] - m[2][0] * m[1][2]) + m[0][2] * (m[1][0] * m[2][1] - m[1][1] * m[2][0])
	d = determinant([i[:3] for i in mat])
	d1 = determinant([i[1:] for i in mat])
	d2 = determinant([[i[0], i[3], i[2]] for i in mat])
	d3 = determinant([i[:2] + [i[3]] for i in mat])
	return [d1 / d, d2 / d, d3 / d]


def solve_eq(eq):
	for i in range(3):
		if eq[i][i] == 0:
			(eq[i], eq[i + 1]) = (eq[i + 1], eq[i])
		eq[i] = [x / eq[i][i] for x in eq[i]]
		for j in range(3):
			if i != j:
				eq[j] = [-eq[j][i] * eq[i][x] + eq[j][x] for x in range(len(eq[j]))]
	return [round(x[-1]) for x in eq]


from numpy.linalg import solve

def solve_eq(eq):
	((*eq1, d1), (*eq2, d2), (*eq3, d3)) = eq
	return list(map(round, solve([eq1, eq2, eq3], [d1, d2, d3])))


import numpy as np

def solve_eq(eq):
	[p, q, r] = eq
	a = np.array([p[:-1], q[:-1], r[:-1]])
	b = np.array([p[-1], q[-1], r[-1]])
	return list(map(round, np.linalg.solve(a, b).tolist()))


def solve_eq(eq):
	(a1, b1, c1, d1) = eq[0]
	(a2, b2, c2, d2) = eq[1]
	(a3, b3, c3, d3) = eq[2]
	x = (b1 * c2 * d3 - b1 * c3 * d2 - b2 * c1 * d3 + b2 * c3 * d1 + b3 * c1 * d2 - b3 * c2 * d1) / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1)
	y = (-a1 * c2 * d3 + a1 * c3 * d2 + a2 * c1 * d3 - a2 * c3 * d1 - a3 * c1 * d2 + a3 * c2 * d1) / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1)
	z = (a1 * b2 * d3 - a1 * b3 * d2 - a2 * b1 * d3 + a2 * b3 * d1 + a3 * b1 * d2 - a3 * b2 * d1) / (a1 * b2 * c3 - a1 * b3 * c2 - a2 * b1 * c3 + a2 * b3 * c1 + a3 * b1 * c2 - a3 * b2 * c1)
	return [x, y, z]


from numpy import array as a, matrix as m

def solve_eq(eq):
	return [round(i[0]) for i in a(m([i[:3] for i in eq]).I * m([i[3] for i in eq]).T)]


import numpy as np

def solve_eq(eq):
	return [i[0] for i in np.linalg.solve(np.reshape([x for x in (y[:-1] for y in eq)], (3, 3)), np.reshape([x for x in (y[-1] for y in eq)], (3, 1))).round().astype(int)]


def solve_eq(eq):
	d = det(eq[0][0], eq[0][1], eq[0][2], eq[1][0], eq[1][1], eq[1][2], eq[2][0], eq[2][1], eq[2][2])
	return [det(eq[0][3], eq[0][1], eq[0][2], eq[1][3], eq[1][1], eq[1][2], eq[2][3], eq[2][1], eq[2][2]) / d, det(eq[0][0], eq[0][3], eq[0][2], eq[1][0], eq[1][3], eq[1][2], eq[2][0], eq[2][3], eq[2][2]) / d, det(eq[0][0], eq[0][1], eq[0][3], eq[1][0], eq[1][1], eq[1][3], eq[2][0], eq[2][1], eq[2][3]) / d]

def det(a, b, c, d, e, f, g, h, i):
	return int(a * (e * i - h * f) - b * (d * i - g * f) + c * (d * h - g * e))