Mathematical variables (Ruby)

For general discussion related FlowStone
Post Reply
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Mathematical variables (Ruby)

Post by kortezzzz »

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
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: Mathematical variables (Ruby)

Post by RJHollins »

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 961 times
KG_is_back
Posts: 1196
Joined: Tue Oct 22, 2013 5:43 pm
Location: Slovakia

Re: Mathematical variables (Ruby)

Post by KG_is_back »

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.
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Mathematical variables (Ruby)

Post by tulamide »

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)
User avatar
kortezzzz
Posts: 763
Joined: Tue Mar 19, 2013 4:21 pm

Re: Mathematical variables (Ruby)

Post by kortezzzz »

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)

Post by kortezzzz »

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.
RJHollins
Posts: 1573
Joined: Thu Mar 08, 2012 7:58 pm

Re: Mathematical variables (Ruby)

Post by RJHollins »

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-)
User avatar
martinvicanek
Posts: 1334
Joined: Sat Jun 22, 2013 8:28 pm

Re: Mathematical variables (Ruby)

Post by martinvicanek »

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 18440 times

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

I find it much faster to set up in Ruby, and certainly much easier to debug!
tulamide
Posts: 2714
Joined: Sat Jun 21, 2014 2:48 pm
Location: Germany

Re: Mathematical variables (Ruby)

Post by tulamide »

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)
Post Reply