Initializing help system before first use

Invert a Mosel matrix with NumPy


Type: Programming
Rating: 2 (easy-medium)
Description: Invert a Mosel matrix in Python using NumPy.
File(s): invert_matrix.mos
Data file(s): invert_matrix.py


invert_matrix.mos
(!******************************************************
   Mosel Python Example Problems
   =============================

   file invert_matrix.mos
   ``````````````````````
   Invert a Mosel matrix in Python using NumPy.

  (c) 2018 Fair Isaac Corporation
      author: J.Müller
*******************************************************!)
model 'invert_matrix'
  options noimplicit

  uses 'python3'
  uses 'mmjobs'

  ! Input data
  declarations
    I, J: range
    A, A_inverse: array(I, J) of real
  end-declarations

  procedure show_matrix(B: array(I: range, J: range) of real)
    forall (i in I) do
      forall (j in J) do
        write(B(i, j), ' ')
      end-do
      writeln
    end-do
  end-procedure

  writeln('Run Python script that defines invert_matrix function.')
  writeln('Python DSO version:     ', getdsoprop('python3', PROP_VERSION))
  pyrun('invert_matrix.py')

  I := 0..2
  J := 0..2
  A :: [1,0,3,
        0,1,2,
        0,0,1]

  writeln('Mosel matrix A:')
  show_matrix(A)

  writeln('Transfer A, I, and J to Python.')

  initializations to PY_IO_GLOBAL_VAR
    A
    I
    J
  end-initializations

  writeln('Invert matrix with NumPy.')
  pyexec('A_inverse = invert_matrix(A, I, J)')

  writeln('Get A_inverse from Python.')

  initializations from PY_IO_GLOBAL_VAR
    A_inverse
  end-initializations

  writeln('Matrix A_inverse:')
  show_matrix(A_inverse)
end-model

© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.