#!/usr/bin/env python # -*- coding: utf-8 -*- # ****************************************************** # Mosel Python Example Problems # ============================= # # file invert_matrix.py # ````````````````````` # Invert a Mosel matrix in Python using NumPy. # # (c) 2018 Fair Isaac Corporation # author: J.Müller # ******************************************************* import platform import numpy as np from numpy.linalg import inv def invert_matrix(dictionary, indices1, indices2): """ Compute inverse matrix of a two dimensional Mosel array. Parameters ---------- dictionary : dict Two dimensional Python dictionary (Mosel array). indices1 : range Range of all indices in dimension one. indices2 : iterable Range of all indices in dimension two. Returns ------- dict Inverse matrix as two dimensional Python dictionary (Mosel array). """ # Convert dictionary to NumPy array. np_a = np.array([[dictionary[i, j] for j in indices2] for i in indices1]) # Compute inverse matrix. np_a_inverse = inv(np_a) print(np_a_inverse) # Convert NumPy array to dictionary. a_inverse = {(i, j): np_a_inverse[i, j] for j in indices2 for i in indices1} return a_inverse if __name__ == "__main__": print("Python library version:", platform.python_version()) print("NumPy library version: ", np.__version__)