Question
[Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElement()
Dear sir,
Now I am solving a design optimization problem using Mathcad prime 6.0 through python API. I uses comtypes library to deal with API programming in my Python code.
In python code, I can set a single variable easily and vice verse. However, I could not set a matrix and get it from Mathcad.
I really love to exchange variables which whether it is a real single variable or matrix between Python and Mathcad.
I could not find any solution method for this trouble.
Please help me with this issue. I really want to make them exchangeable for reproducible research work. It is not commercial but academic.
With my best regards,
JW
---------------------------------------------------------------------------------------
The following is the python code to call and set matrix in Mathcad. Two functions are defined. The first(set_element) is to set the elements of the matrix in Mathcad but the second(get_element) is to get the element of the matrix from Mathcad.
---
import comtypes.client as CC
import comtypes
def set_element(self, row_index, column_index, value):
""" Sets the value of an element in the Matrix """
if self.object is not None:
try:
row, col = int(row_index), int(column_index)
self.object.SetMatrixElement(row, col, value)
except ValueError:
raise ValueError("Matrix maths can only use numerical values")
except:
raise Exception("COM Error setting element value") # Hidden for above @FIXME
else:
raise TypeError("Matrix must first be created")
def get_element(self, row_index, column_index):
""" Fetches the value of an element in the Matrix """
if self.object is not None:
try:
row, col = int(row_index), int(column_index)
self.object.GetMatrixElement(row, col)
except ValueError:
raise ValueError("Matrix maths can only use numerical values")
except:
raise Exception("COM Error fetching element value")
else:
raise TypeError("Matrix must first be created")

