Is there a way to determine the units of an arbitrary variable

Is there a way to determine the units of an arbitrary variable - Messages

#1 Posted: 3/10/2016 2:16:37 PM
kevnol

kevnol

5 likes in 9 posts.

Group: User

Is there a way to determine the unit attached to an arbitrary variable? I would like to be able to do this in a user defined function.
I tried the most obvious thing x/abs(x) but this gives a dimensionless answer.
Anybody got any ideas?

example for clarification
if x=100V I would like to create a function Units(X) to return V or 1.V
#2 Posted: 3/10/2016 2:43:30 PM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

Hello kevnol,

You can use UoM() from Custom Functions plugin.

2016-03-10 18_46_06-SMath Studio Desktop - [Page1_].png
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
kevnol 3/10/2016 6:54:00 PM
#3 Posted: 3/10/2016 3:06:12 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Forum Unit.gif
#4 Posted: 3/10/2016 5:15:09 PM
kevnol

kevnol

5 likes in 9 posts.

Group: User

I am not 100% sure what you are doing Jean but I guess you have abandoned the default unit system and instead you are using your own units which are just unique numbers. Interesting idea but think this system will break down once you introduce compound units (unless you can somehow use prime numbers for basic units). Haven't fully thought it through though.
#5 Posted: 3/10/2016 5:23:24 PM
kevnol

kevnol

5 likes in 9 posts.

Group: User

Davide

When I try to use UoM() in one of my own user defined functions it doesn't seem to work., Any ideas?

I am trying to use UoM to strip the units off a figure

eg StripUnit(x):=x/UoM(x)
x=50V
StripUnit(x)=50V

Or even more simply
Unit(x):=UoM(x)
Unit(50V)=1
1 users liked this post
Davide Carpi 3/10/2016 7:36:00 PM
#6 Posted: 3/10/2016 6:17:35 PM
Brian Runagle

Brian Runagle

13 likes in 27 posts.

Group: User

Try this:

stripunit

I'm not sure why this is needed.

Brian
2 users liked this post
kevnol 3/10/2016 6:54:00 PM, Davide Carpi 3/10/2016 7:36:00 PM
#7 Posted: 3/10/2016 6:40:58 PM
kevnol

kevnol

5 likes in 9 posts.

Group: User

Brian

How do I create a multi line function like that? This looks very useful.

Ahh .. Found it in the programming reference. Fun to be had.
#8 Posted: 3/10/2016 7:34:38 PM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

Strange behavior, I have to investigate it...

BTW in the Extensions Manager, section Snippets, there is a Snippet called "Operations with units"; if you download it, you can insert the snippet in the canvas typing "Units" and then pressing the TAB key. In this snippet there is a version of UoM made with built-in functions (only for scalars).
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
#9 Posted: 3/10/2016 7:42:23 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

... or like this:

Forum Unit Volt.gif
1 users liked this post
kevnol 3/10/2016 8:37:00 PM
#10 Posted: 3/10/2016 7:43:28 PM
Davide Carpi

Davide Carpi

1415 likes in 2872 posts.

Group: Moderator

Ok ok, I was lost in a glass of water...

UoM was designed to act in a generic way both with symbolic and numeric data... when you use it in a inline function, since x is unknown, his unit is 1 (dimensionless - hold the mouse over the definition); if you want to use it in a inline function you have to use Units(x):UoM(eval(x)). In programming functions (when you use line), eval in the argument is not needed, because the evaluation of UoM(x) is made when you call your function.

2016-03-11 00_18_01-SMath Studio Desktop - [Page1_].png

Obviously if you know the dimensions the best approach is the one suggested in the post above (#9) by Jean.
If you like my plugins please consider to support the program buying a license; for personal contributions to me: paypal.me/dcprojects
1 users liked this post
kevnol 3/10/2016 8:37:00 PM
#11 Posted: 3/11/2016 6:46:52 AM
kevnol

kevnol

5 likes in 9 posts.

Group: User

Thank you again David, your input has been very helpful.

The background behind my query is that I use complex number calculations a lot for electrical engineering. Smath handles units well for basic complex number calculations but certain key functions do not work with units (Arg(), Re() and Im()) in particular). You can get around this by dividing off the unit but this looks inelegant and needs to be explained every time I show the work to someone. I hope to write my own user defined functions to replace them and having a UoM() function is a nessecary step.
#12 Posted: 3/11/2016 11:36:46 AM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

Wrote

The background behind my query is that I use complex number calculations a lot for electrical engineering. Smath handles units well for basic complex number calculations but certain key functions do not work with units (Arg(), Re() and Im()) in particular). You can get around this by dividing off the unit but this looks inelegant and needs to be explained every time I show the work to someone. I hope to write my own user defined functions to replace them and having a UoM() function is a nessecary step.



I use lot of complex too. Im(,) Re(,) work well in Smath 5346.
A short example would help to compare.

Jean

#13 Posted: 3/11/2016 12:29:41 PM
kevnol

kevnol

5 likes in 9 posts.

Group: User

Jean here is an example showing what I mean. The functions Re(), Im() and arg() don't work when the operands have units.

Sample.png

By the way you may be interested in the worksheet I did for a simple AC calculation in the complex domain. it is available in the exampls section under "Engineering" and you can see it here: https://smath.com/wiki/GetFile.aspx?File=Examples/ElecEngExample.pdf
#14 Posted: 3/11/2016 5:43:43 PM
Jean Giraud

Jean Giraud

983 likes in 6866 posts.

Group: User

There is more than unit problem. I don't use unit
maths are unitless. BUT I append result in "UnitProject".
Years long fight in Mathcad, conclusion from collabs:
"Jean is right". ISO, Engineering ... formulas are given
wrt the unit system. Revisit the "Visual Graphic" example
just reposted this morning.

Jean

Forum Conjugate.gif
#15 Posted: 3/11/2016 8:26:42 PM
Andrey Ivashov

Andrey Ivashov

2269 likes in 3729 posts.

Group: Super Administrator

FYI: New build will contain UnitsOf(..) function which returns units of specified expression.

The following file created to test its most important use cases:
UnitsOf.png

Best regards.
6 users liked this post
ioan92 3/12/2016 5:58:00 AM, kevnol 3/11/2016 8:34:00 PM, Davide Carpi 3/11/2016 10:09:00 PM, уни 3/12/2016 12:23:00 AM, frapuano 3/12/2016 3:21:00 AM, Radovan Omorjan 3/12/2016 5:47:00 AM
#16 Posted: 3/14/2016 7:47:36 AM
kevnol

kevnol

5 likes in 9 posts.

Group: User

Excellent Andrey. I look forward to playing with this new build.
  • New Posts New Posts
  • No New Posts No New Posts