#!/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():
    """ Compute inverse Matrix of global Python variable A
    and return the result in global Python variable A_inverse. """
    print("Python version:  ", platform.python_version())

    # Global input values.
    # The following lines are optional and only needed to please static code analyzers like PyCharm.
    global I
    global J
    global A

    # Global return values.
    # The following line is required because otherwise A_inverse would only exists in the scope of this function.
    global A_inverse

    print("Inverting matrix with NumPy.")

    # Convert dictionary A to NumPy array.
    np_a = np.array([[A[i, j] for j in J] for i in I])

    # 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 J for i in I}


if __name__ == "__main__":
    invert_matrix()
