Skip to main content
1-Visitor
August 1, 2020
Question

[Q] Serious API troubling between Python and Mathcad prime - SetMatrixElement(), GetMatrixElement()

  • August 1, 2020
  • 1 reply
  • 3216 views
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")
 
 

1 reply

25-Diamond I
August 1, 2020

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#M122230

JW11-VisitorAuthor
1-Visitor
August 1, 2020

I really appreciate it.  I will look at it.

JW11-VisitorAuthor
1-Visitor
August 1, 2020

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.

 

Please help me look at the article.

 

JW