cancel
Showing results for
Did you mean:
cancel
Showing results for
Did you mean:

Community Tip - Need to share some code when posting a question or reply? Make sure to use the "Insert code sample" menu option. Learn more! X

4-Participant

## [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")

5 REPLIES 5
24-Ruby V
(To:JW1)

I remember there were a few threads in this forum about exchanging matrices via the API in the past but I am not sure if there ever was a satisfying solution. You may try the search function in the forum to find those threads.

While not specifically covering python, maybe this post can be of help: https://community.ptc.com/t5/PTC-Mathcad/Using-Matrix-with-Prime-3-1-API/m-p/313195/highlight/true#M...

4-Participant
(To:Werner_E)

I really appreciate it.  I will look at it.

4-Participant
(To:JW1)

Is there anybody who can reach https://www.ptc.com/en/support/article/CS301830?

The name of article is 'Article - CS301830 How to set a matrix element value using API SetMatrixElement() and SetMatrixValue() in Mathcad Prime'.

It is the right thing for me.

JW

24-Ruby V
(To:JW1)

Here you are, but the "article" just consists of the code example which you can also find in the thread I linked to in my first answer.

------------------------------------------------------- snip -------------------------------------------------

# How to set a matrix element value using API SetMatrixElement() and SetMatrixValue() in Mathcad Prime

Modified: 28-Feb-2019

## Applies To

• Mathcad Prime 3.1 to 5.0
• API

## Description

• How to set a matrix element value using API SetMatrixElement() and SetMatrixValue()

## Resolution

4-Participant
(To:Werner_E)

It is ridiculous.  Even though I coded like the c++ in the above, it still does not work. I do not know where I start from.

--------------------

<Functions>

def create_matrix(self, rows, columns):
""" Creates a Mathcad matrix """
try:
rows, columns = int(rows), int(columns)
self.shape = (rows, columns)
self.object = self.__ws.CreateMatrix(rows, columns)
return True
except ValueError:
raise ValueError("Matrix dimensions must be integers")
return False
except:
raise Exception("COM Error creating Mathcad matrix")
return False

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)  # @FIXME
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")
-----------------------
<Main>
A = Matrix()
A.create_matrix(2,2)
A.set_element(1,1,3.0)
--------------------
<Error messege>