from sympy import linsolve, symbols, Matrix, simplify
A, B, f, n, t, b, l, r = symbols('A B f n t b l r')
S = Matrix([
[2/(r-l), 0, 0, 0],
[0 , 2/(t-b), 0, 0],
[0 , 0,2/(f-n), 0],
[0 , 0, 0, 1]
])
T = Matrix([
[1, 0, 0, -(r+l)/2],
[0, 1, 0, -(t+b)/2],
[0, 0, 1, -(-n-f)/2],
[0, 0, 0, 1]
])
P = Matrix([
[n, 0, 0, 0],
[0, n, 0, 0],
[0, 0, A, B],
[0, 0, -1, 0]
])
F = Matrix([
[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, -1, 0],
[0, 0, 0, 1]
])
O = simplify(S*T)
display(O)
#eqns = [-A * n + B + n*f, -A * f + B + n*f] # flip frustrum
eqns = [-A * n + B + n*n, -A * f + B + f*f] # no flip
linsolve(eqns, A, B)
P = P.subs(A, f+n).subs(B, f*n)
# execute full pipeline
simplify(F*O*P)