Matrix Transformation

Write a Python function that transforms a given matrix A using the operation T1AS, where T and S are invertible matrices. The function should first validate if the matrices T and S are invertible, and then perform the transformation. In cases where there is no solution return -1.

Example:

Input:

A = [[1, 2], [3, 4]], T = [[2, 0], [0, 2]], S = [[1, 1], [0, 1]]

Output:

[[0.5,1.5],[1.5,3.5]]

Reasoning:

The matrices T and S are used to transform matrix A by computing T1AS.

Code:

import numpy as np

def transform_matrix(A, T, S):
    A = np.array(A)
    T = np.array(T)
    S = np.array(S)

    if np.linalg.det(T) and np.linalg.det(S):
        return np.linalg.matrix_power(T, -1) @ A @ S

    return -1