Welcome Guest! To enable all features please Login. New Registrations are disabled.

Notification

Icon
Error

Login


Options
Go to last post Go to first unread
Offline Jon J  
#1 Posted : 26 November 2011 09:02:57(UTC)
Jon J

Rank: Newbie

Groups: Registered
Joined: 26/11/2011(UTC)
Posts: 3

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

Wanna join the discussion?! Login to your SMath Studio Forum forum account. New Registrations are disabled.

Offline omorr  
#2 Posted : 26 November 2011 16:52:08(UTC)
omorr


Rank: Administration

Groups: Registered, Advanced Member
Joined: 23/06/2009(UTC)
Posts: 1,740
Man
Serbia

Was thanked: 318 time(s) in 268 post(s)
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:
abs(x)←x^3
x←-5
abs(x)=-125

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.
Open in SMath Cloud
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

Edited by user 27 November 2011 10:31:20(UTC)  | Reason: Not specified

When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
Offline Jon J  
#3 Posted : 28 November 2011 04:11:55(UTC)
Jon J

Rank: Newbie

Groups: Registered
Joined: 26/11/2011(UTC)
Posts: 3

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
Offline omorr  
#4 Posted : 28 November 2011 09:04:35(UTC)
omorr


Rank: Administration

Groups: Registered, Advanced Member
Joined: 23/06/2009(UTC)
Posts: 1,740
Man
Serbia

Was thanked: 318 time(s) in 268 post(s)
Hello Jon,
Jon J 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
Vabs(V)←sqrt(el((transpose(V)*V),1,1))
Vunit(V)←V/Vabs(V)
V←mat(-5,2,3,3,1)
Vabs(V)=5
V.u←Vunit(V)
V.u=mat(-1,0.4,0.6,3,1)
Vabs(V.u)=1

Edited by user 28 November 2011 10:34:23(UTC)  | Reason: Not specified

When Sisyphus climbed to the top of a hill, they said: "Wrong boulder!"
Users browsing this topic
Guest
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.