creating new operators

creating new operators - Messages

#1 Posted: 11/26/2011 2:02:57 AM
Jon J

Jon J

0 likes in 3 posts.

Group: User

Is it possible to add custom operators?

For instance: I would find it handy to have an "absolute value" operator that would work on vectors. For this example call it Vabs().

The existing absolute value operator only accepts a scalar argument.

Vabs() would accept a vector as an argument and return the absolute value of the vector. If a vector V were created as a variable, then a unit vector in the direction of V would be V/(Vabs(V)).

Calculating the result within Smath using only the available operators is straightforward and works fine but the resulting page gets cluttered if there are many vectors. If I could create my own operator and add it to the pallet of vector operators, I'd do it.

I haven't seen this in any of the documentation I've reviewed so far.

What I'd REALLY like to do is redefine the current absolute value operator to accept both scalar or vector arguments but I suspect that is impossible.

Jon J
#2 Posted: 11/26/2011 9:52:08 AM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Hello,

Actually, I do not think you can make some additional operators at the moment, but you can redefine the existing ones because the operators and functions are quite the same, except the final presentation:
[MATH=eng]abs(x)←x^3[/MATH]
[MATH=eng]x←-5[/MATH]
[MATH=eng]abs(x)=-125[/MATH]

But this is not advisable. I tried to redefine absolute value operator with some multiline functions but there were crashes and I would not recommend it. Moreover, function calls in SMath can be recursive and there is another potential source of mistakes. I think that the safest way is to use user defined functions and not to redefine the existing operators.

On the other hand here is a function that can manage to give absolute values for both scalar and vector/matrix argument. There is no IsScalar() or IsArray() function at the moment, and we can try to find "mat" string in a num2string expression in order to decide if the argument is scalar or vector/matrix.
[LIVE width=480 height=456]http://smath.info/live/?file=3289[/LIVE]
It is not working for nested arrays (matrix inside matrix).
You can make a "snippet" from this function and insert it in your worksheet.

Regards,
Radovan
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
#3 Posted: 11/27/2011 9:11:55 PM
Jon J

Jon J

0 likes in 3 posts.

Group: User

First off...thank you for your reply.

I have stated my question imprecisely. Although I used the term "absolute value", that is mathematically incorrect. What I am really trying to do is calculate the "magnitude" of a 3x1 vector and subsequently divide the orginal vector by its magnitude to define a unit vector with a magnitude of 1.0 but with the same direction of the original vector.

I have not used Mathcad on a regular basis for some time. As I recall if the "absolute value" operator is applied to a 3x1 vector, the magnitude is returned. Perhaps I recall that incorrectly.

I can use Smath to calculate both the magnitude and create the unit vector with no problem, but it is time consuming to enter the algorithm over and over. My user defined function intended to automate the process doesn't seem to work. I assume that I have a syntax error.

I have a worksheet that illustrates the problem, but don't know how to post my worksheet to the forum. I tried cutting/pasting the work sheet directly into this reply and also cutting/pasting a zip file containing the worksheet but these methods don't work.

How do I post my worksheet on the forum?


Jon J
#4 Posted: 11/28/2011 2:04:35 AM
Radovan Omorjan

Radovan Omorjan

325 likes in 2052 posts.

Group: Moderator

Hello Jon,
Wrote

How do I post my worksheet on the forum?


See this page Using the Forum. Morover, you could use some free file storage system like a nice free one http://www.dropbox.com/ . Upload your file into the Public folder of dropbox and put the link into the Forum post. Files in dropbox will never be deleted.

Regards,
Radovan

Edit: Consider this
[MATH=eng]Vabs(V)←sqrt(el((transpose(V)*V),1,1))[/MATH]
[MATH=eng]Vunit(V)←V/Vabs(V)[/MATH]
[MATH=eng]V←mat(-5,2,3,3,1)[/MATH]
[MATH=eng]Vabs(V)=5[/MATH]
[MATH=eng]V.u←Vunit(V)[/MATH]
[MATH=eng]V.u=mat(-1,0.4,0.6,3,1)[/MATH]
[MATH=eng]Vabs(V.u)=1[/MATH]
When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
  • New Posts New Posts
  • No New Posts No New Posts