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

Community Tip - You can change your system assigned username to something more personal in your community settings. X

How to create custom function in Mathcad

DM_10631844
11-Garnet

How to create custom function in Mathcad

 
 
 
 

Greetings, I tried to create custom functions in mathcad but I was able to do it, however I wanted to create that function fc and fc1 and I couldn't, I get an error message I understand how to compile and tried other functions but when I go to do it with results of vectors and matrices I have this problem here I attach the code in C only for the fc function, could you help me how to create both custom functions please?




 

 

7 REPLIES 7

1111DM_10631844_0-1721038678159.png

 

 

Use subscript rather than function notation when asking for components:

 

More like:

fc.png

111111

11

 

 

#include "mcadincl.h"

#define INTERRUPTED 1
#define INSUFFICIENT_MEMORY 2
#define MUST_BE_REAL 3
#define NUMBER_OF_ERRORS 3

// table of error messages
char * myErrorMessageTable[NUMBER_OF_ERRORS] =
{ "interrupted",
"insufficient memory",
"must be real"
};

// this code executes the operation
LRESULT CreateVectorWithXSquare(COMPLEXARRAY * const Vector, LPCCOMPLEXSCALAR Scalar)
{
unsigned int i;

// check that the scalar argument is real
if (Scalar->imag != 0.0)
return MAKELRESULT(MUST_BE_REAL, 1);

// allocate memory for the vector
if (!MathcadArrayAllocate(Vector, 4, 1, TRUE, FALSE))
return INSUFFICIENT_MEMORY;

// fill the vector with the square of the scalar
for (i = 0; i < 4; i++) {
if (isUserInterrupted()) {
MathcadArrayFree(Vector);
return INTERRUPTED;
}
Vector->hReal[i][0] = Scalar->real * Scalar->real;
}

// normal return
return 0;
}

// fill out a FUNCTIONINFO structure with the information needed for registering the function with Mathcad
FUNCTIONINFO fc2 =
{
"fc2",
"x",
"returns a vector of length 4 with the square of the scalar x",
(LPCFUNCTION)CreateVectorWithXSquare,
COMPLEX_ARRAY,
1,
{ COMPLEX_SCALAR }
};

// DLL entry point code
BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved);

BOOL WINAPI DllEntryPoint(HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
if (!_CRT_INIT(hDLL, dwReason, lpReserved))
return FALSE;
if (CreateUserErrorMessageTable(hDLL, NUMBER_OF_ERRORS, myErrorMessageTable))
CreateUserFunction(hDLL, &fc2);
break;

case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
if (!_CRT_INIT(hDLL, dwReason, lpReserved))
return FALSE;
break;
}
return TRUE;
}

#undef INTERRUPTED
#undef INSUFFICIENT_MEMORY
#undef MUST_BE_REAL
#undef NUMBER_OF_ERRORS

#define INTERRUPTED 1
#define INSUFFICIENT_MEMORY 2
#define MUST_BE_REAL 3
#define NUMBER_OF_ERRORS 3

 

#include "mcadincl.h"

#define INTERRUPTED 1
#define INSUFFICIENT_MEMORY 2
#define MUST_BE_REAL 3
#define NUMBER_OF_ERRORS 3

// table of error messages
char * myErrorMessageTable[NUMBER_OF_ERRORS] =
{ "interrupted",
"insufficient memory",
"must be real"
};

// this code executes the operation
LRESULT CreateRowMatrixWithXSquare(COMPLEXARRAY * const Matrix, LPCCOMPLEXSCALAR Scalar)
{
unsigned int i;

// check that the scalar argument is real
if (Scalar->imag != 0.0)
return MAKELRESULT(MUST_BE_REAL, 1);

// allocate memory for the matrix
if (!MathcadArrayAllocate(Matrix, 1, 4, TRUE, FALSE))
return INSUFFICIENT_MEMORY;

// fill the row with the square of the scalar
for (i = 0; i < 4; i++) {
if (isUserInterrupted()) {
MathcadArrayFree(Matrix);
return INTERRUPTED;
}
Matrix->hReal[0][i] = Scalar->real * Scalar->real;
}

// normal return
return 0;
}

// fill out a FUNCTIONINFO structure with the information needed for registering the function with Mathcad
FUNCTIONINFO fc1 =
{
"fc1",
"x",
"returns a 1x4 row matrix with the square of the scalar x",
(LPCFUNCTION)CreateRowMatrixWithXSquare,
COMPLEX_ARRAY,
1,
{ COMPLEX_SCALAR }
};

// DLL entry point code
BOOL WINAPI _CRT_INIT(HINSTANCE hinstDLL, DWORD dwReason, LPVOID lpReserved);

BOOL WINAPI DllEntryPoint(HINSTANCE hDLL, DWORD dwReason, LPVOID lpReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
if (!_CRT_INIT(hDLL, dwReason, lpReserved))
return FALSE;
if (CreateUserErrorMessageTable(hDLL, NUMBER_OF_ERRORS, myErrorMessageTable))
CreateUserFunction(hDLL, &fc1);
break;

case DLL_THREAD_ATTACH:
case DLL_THREAD_DETACH:
case DLL_PROCESS_DETACH:
if (!_CRT_INIT(hDLL, dwReason, lpReserved))
return FALSE;
break;
}
return TRUE;
}

#undef INTERRUPTED
#undef INSUFFICIENT_MEMORY
#undef MUST_BE_REAL
#undef NUMBER_OF_ERRORS

#define INTERRUPTED 1
#define INSUFFICIENT_MEMORY 2
#define MUST_BE_REAL 3
#define NUMBER_OF_ERRORS 3

 

 

The first code is fc2, it doesn't work, it gives an error and the 2nd is fc1, it works fine in C++

DM_10631844_0-1721041023864.png

 

 

 

 

 

Hello @DM_10631844

 

It looks like you have a response from a community member. If it helped you solve your question please mark the reply as the Accepted Solution. 

Of course, if you have more to share on your issue, please let the Community know so other community members can continue to help you.

Thanks,
Vivek N.
Community Moderation Team.

Announcements

Top Tags