Support

If you have a problem or need to report a bug please email : support@dsprobotics.com

There are 3 sections to this support area:

DOWNLOADS: access to product manuals, support files and drivers

HELP & INFORMATION: tutorials and example files for learning or finding pre-made modules for your projects

USER FORUMS: meet with other users and exchange ideas, you can also get help and assistance here

NEW REGISTRATIONS - please contact us if you wish to register on the forum

Users are reminded of the forum rules they sign up to which prohibits any activity that violates any laws including posting material covered by copyright

Mathematical variables (Ruby)

For general discussion related FlowStone

Mathematical variables (Ruby)

Postby kortezzzz » Fri Jan 29, 2016 9:52 pm

Hi,

Since I've never had a chance to combine ruby based math in my project, just wanted to get a clue about the very basic algebra functionality in ruby. Can someone post a very basic (but please, full ) code how should we use mathematical variables with ruby?

For example:

I have 1 int. input named @input1. I would like to make a variable called "x" with it:

x = ((@input1 * 4) + 2)

How do I actually formulate the ruby code to use my variable correctly if ,let's say, I got 2 outputs in this code and I want that the first would output my "x" value and second would output "1" anytime "x" = 1 or else, it would output "0".

Thanks
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Mathematical variables (Ruby)

Postby RJHollins » Fri Jan 29, 2016 10:24 pm

Hi Kortezzz.

Not sure this will help, but [someone] posted mathematical examples in RUBY, and shared it. :)

He's the code.
Attachments
Mathematical Examples in Ruby - Beginners.fsm
(5.12 KiB) Downloaded 913 times
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: Mathematical variables (Ruby)

Postby KG_is_back » Fri Jan 29, 2016 10:30 pm

Code: Select all

def event(i,v,t)

x = ((@input1 * 4) + 2)

output 0,x #outputs value of x to the first output (indexing starts at zero).
if x==1
output 1,1 #of x==1 send 1 to second output
else
output 1,0 # if x!=1 send 0 to second output
end


end


Alternatively, if this is the only thing the module will do, you can omit the "event(i,v,t)" declaration.
Also the "if" statement may be represented in various ways. The shortest would probably be ternary operator:
Code: Select all
x = ((@input1 * 4) + 2)
output 0,x
output 1, (x==1 ? 1 : 0) # this is shortcut for "if x==1 then 1 else 0 end"


Ruby is very high-level language. It automatically declares variables on fly and converts formats.
KG_is_back
 
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Mathematical variables (Ruby)

Postby tulamide » Fri Jan 29, 2016 11:40 pm

KG_is_back wrote:Alternatively, if this is the only thing the module will do, you can omit the "event(i,v,t)" declaration.
Also the "if" statement may be represented in various ways. The shortest would probably be ternary operator:
Code: Select all
x = ((@input1 * 4) + 2)
output 0,x
output 1, (x==1 ? 1 : 0) # this is shortcut for "if x==1 then 1 else 0 end"


Ruby is very high-level language. It automatically declares variables on fly and converts formats.

They say: "Nip it in the bud!"
So let me point out that the ternary operator is considered bad coding in Ruby. Why? Because it is a relic from C/C++, where an if-clause is not an object. In Ruby it is, so it is better to write
Code: Select all
output 1, if x==1 then 1 else 0 end


Ruby made all this language stuff available to have a programming language that everyone can read, not just some creepy C developers. Using english as normal as possible is a goal of Ruby. That's why you can also write things like "@myvar.even?" instead of doing some cryptic math just to see if a number is odd or even. Some people may think the ternary operator would be quicker, because it is written shorter. That's not the case!

Also, for instance variables, they should always be initialized in the initializing method. It is bad habit to initialize them in some of the many sub-methods, so that others have a hard time to understand the flow of the code.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Mathematical variables (Ruby)

Postby kortezzzz » Fri Jan 29, 2016 11:55 pm

Thanks a lot KG, that's very helpful. The first schematic contains much complex math examples, which are kinda overkill for me (but great to have it here. Some talented folks would surely find it helpful one day :ugeek: ). The second code was exactly what I've been looking for; simple, understandable yet effective math functionality for "every-day" usage.

Cheers!
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Mathematical variables (Ruby)

Postby kortezzzz » Sat Jan 30, 2016 12:19 am

Ruby made all this language stuff available to have a programming language that everyone can read, not just some creepy C developers. Using english as normal as possible is a goal of Ruby


Yep, tulamide. I'm absolutely with you in this case. As a poor coder, I also prefer the more readable code rather that the shortcuts. I don't really have an idea if both codes are just the same in case of effectiveness and speed, but your version is much clearer to my unprofessional eyes.

Thanks for that comment, pal.
User avatar
kortezzzz
 
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Mathematical variables (Ruby)

Postby RJHollins » Sat Jan 30, 2016 1:52 am

Thanks tulamide for your comments and examples.

As someone that needs maximum learning with minimal time, I have to follow every thread on this board.

Examples are always welcomed, but when someone shares their insight/experience on the 'thought process' [a liken RUBY to english language] ... that really does helps !

True, I need to spend more time with RUBY documentation/syntax ... but the 'thought' process shared puts a framework to it.

thanks once again 8-)
RJHollins
 
Posts: 1571
Joined: Thu Mar 08, 2012 7:58 pm

Re: Mathematical variables (Ruby)

Postby martinvicanek » Sun Jan 31, 2016 4:02 pm

Math is very easy to do in Ruby and a good entry point for noobs like me. Here is another example which shows the advantage of Ruby over green math: a simple biquad lowpass filter coefficients module. This is how a green math implementation of the RBJ cook book formulas looks like:
RBJ_LP_coeff_green.png
RBJ_LP_coeff_green.png (32.41 KiB) Viewed 15574 times

And this is the same in Ruby:
RBJ_LP_coeff_Ruby.png
RBJ_LP_coeff_Ruby.png (22.98 KiB) Viewed 15574 times

I find it much faster to set up in Ruby, and certainly much easier to debug!
User avatar
martinvicanek
 
Posts: 1328
Joined: Sat Jun 22, 2013 8:28 pm

Re: Mathematical variables (Ruby)

Postby tulamide » Sun Jan 31, 2016 9:48 pm

martinvicanek wrote:And this is the same in Ruby:
RBJ_LP_coeff_Ruby.png

I find it much faster to set up in Ruby, and certainly much easier to debug!

Small tip: PI is defined in Math, so
Code: Select all
omega = Math::PI * @freq

gives you higher accuracy

You are right. It is faster to set up and easier to debug. But don't forget, this is for us, people that already programmed with another programming language before. Those that never programmed before will love that they can program visually instead!

On the other hand, without Ruby I would never have been able to realize the spline module. A nightmare thinking about how to do that in green!

In the end, I think, that means to start in green, then slowly start to simplify things with Ruby modules, until you're experienced enough to do the whole thing in Ruby.
"There lies the dog buried" (German saying translated literally)
tulamide
 
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany


Return to General

Who is online

Users browsing this forum: No registered users and 83 guests