Lua: How do I read from a list of numbers - lua

I have a function which does all the magic for me and controls a motor.
This reduces my programming to this one line...
Set_RPM_With_Acceleration_Value( 200, 15)
In the example shown, the motor will spin up to 200 RPM, with an acceleration value of 15
I'm testing the stop/start/interrupt/restart capability of the system hardware and software.
I now want to run the test with different values given to the function; e.g.,,,
Set_RPM_With_Acceleration_Value( 200, 15)
Set_RPM_With_Acceleration_Value( 50, 09)
Set_RPM_With_Acceleration_Value( 400, 20)
Set_RPM_With_Acceleration_Value( 500, 25)
I want to encapsulate this into a loop which...
Reads two numbers (from a table ? Idunno, advise me)
Gives me those two numbers in two variables
Lets me call the function using those names, and not hard coded numbers
e.g., where I'm currently doing this...
Set_RPM_With_Acceleration_Value( 200, 15)
I would like to do this...
Set_RPM_With_Acceleration_Value( RPM_Value, Acceleration_Value)
How do I set up the numbers, and how do I read from the table ?
For that matter, is a table the right way to think about this ?
Does there exist such a thing in Lua as a table of tables ?
This will be for automated machine testing. We do NOT want a human involved at the keyboard or mouse.
On the other hand, it is quite desirable for me to hand code the values in the table once, ahead of time, before the script runs. I want to see what happens with various combinations of RPM, Acceleration, Power Drop Outs, etc.

Try this:
V={
200, 15,
50, 09,
400, 20,
500, 25,
}
for i=1,#V,2 do
local RPM_Value = V[i]
local Acceleration_Value = V[i+1]
Set_RPM_With_Acceleration_Value(RPM_Value, Acceleration_Value)
end

Related

Wrong POSITION shown for SMF with multiple tempos (using MCI)

Is it not possible to get the proper millisecond POSITION value
in Standard MIDI Files having multiple Tempos?
This line
ret = mciSendString("status MCIMIDI position", 0, 0, 0)
works only if the MIDI file has ONE Tempo setting.
How do you deal with multiple Tempos?

Repeating a Function x times in the same Line in Google Sheets

I have been researching a solution to a problem that I just can not seem to avoid, and have yet to find a solution.
In brief, I am trying to calculate unique probabilities that lead to a "1 or 0" for more than one variable, but all in one cell.
Here is my working code line that represents the probability of just one variable:
=sum(if(randbetween(1,100) > subtotal(1,L23), 0, 1))
What I am trying to figure out is how to repeat this function times x, but with it yielding a different randbetween number each time, all in one cell.
As my x variable can represent 10 different independent variables at this time, and stem over 30 specific formula lengths for each IV, utilizing the preset workaround would lead me to creating hundreds of cells of data. I obviously do not want that clutter.
If code worked the way I wanted it to, the best formula-esque way I would describe what I wanted to happen is this:
=sum(repeatuniqueformula(sum(if(randbetween(1,100) > subtotal(1,L23), 0, 1)), x))
Simplified, relevant question gathered from a problem by problem analysis:
How to replicate a function in the function line that allows for the randbetween to recalculate each time.
Sub-information: If you simply multiply the function by lets say 6, it will multiply the answer of the randbetween function without recalculating.
=sum(if(randbetween(1,100) > subtotal(1,L23), 0, 1)*6)
Alternatively, I could do a workaround and create other cells with individual randbetween functions, but that causes a lot of manual work due to having to adjust the number of times a function in a line is repeated.
=sum(if(Q2 > subtotal(1,L15), 0, 1),if(Q3 > subtotal(1,L15), 0, 1),if(Q4 > subtotal(1,L15), 0, 1),if(Q5 > subtotal(1,L15), 0, 1),if(Q6 > subtotal(1,L15), 0, 1),if(Q7 > subtotal(1,L15), 0, 1),if(Q8 > subtotal(1,L15), 0, 1))
The alternative is both cluttery and takes a lot of effort to maintain, as changing the number of "x" will change the amount of
if(Q2 > subtotal(1,L15), 0, 1)
I would need.
In order to get what you want to happen (=sum(repeatuniqueformula(sum(if(randbetween(1,100) > subtotal(1,L23), 0, 1)), x))) you will have to create a custom function by using Google Apps Script, but x should be replaced by number or by a reference to a cell having a value or formula that returns that value.
References
https://developers.google.com/apps-script/guides/sheets
https://developers.google.com/apps-script/guides/sheets/functions

Sending a program change with a program number (patch number)

I have two Roland midi devices that behave the same when I try to send a bank and program change. It always sets it to the first patch of the bank. It won't change the patch I choose in the bank. Pro Logic can, however, switch to different banks.
The following example cause the devices to change to the bank but the program (patch) on the device defaults to the first in that bank and not number 9.
var event = AKMIDIEvent(controllerChange: 0, value: 89, channel: 0)
midiOut.sendEvent(event)
event = AKMIDIEvent(controllerChange: 32, value: 64, channel: 0)
midiOut.sendEvent(event)
event = AKMIDIEvent(programChange: 9, channel: 0)
midiOut.sendEvent(event)
Anyone have experience with sending this MIDI messages?
I was going through the same issue and was about to go crazy. It turns out the Program Change values in various MIDI data specifications, from various vendors, are 1 based. Not 0. Or perhaps it is the AudioKit implementation that is wrong?
So, instead of a programChange value of 9 you should use a value of 8. Here is my code for changing the current instrument on channel 0 to the Bösendorfer grand piano on a Yamaha Clavinova keyboard, where the programChange value in the MIDI data specification is designated as 1.
midiOut.sendControllerMessage(0, value: 108) // MSB sound bank selection
midiOut.sendControllerMessage(32, value: 0) // LSB sound bank selection
midiOut.sendEvent(AKMIDIEvent(programChange: 0, channel: 0)) // Initiate program change based on MSB and LSB selections
While reading various documentation about how MIDI works I also saw some forum posts describing keyboards that expect the LSB bank selection before the MSB bank selection. That is however not my understanding of how MIDI should work, but worth a try if you still cannot make it work with your Roland keyboards.

How do I sum the product of two values, across multiple objects in Rails?

Imagine I have a portfolio p that has 2 stocks port_stocks. What I want to do is run a calculation on each port_stock, and then sum up all the results.
[60] pry(main)> p.port_stocks
=> [#<PortStock:0x00007fd520e064e0
id: 17,
portfolio_id: 1,
stock_id: 385,
volume: 2000,
purchase_price: 5.9,
total_spend: 11800.0>,
#<PortStock:0x00007fd52045be68
id: 18,
portfolio_id: 1,
stock_id: 348,
volume: 1000,
purchase_price: 9.0,
total_spend: 9000.0>]
[61] pry(main)>
So, in essence, using the code above I would like to do this:
ps = p.port_stocks.first #(`id=17`)
first = ps.volume * ps.purchase_price # 2000 * 5.9 = 11,800
ps = p.port_stocks.second #(`id=18`)
second = ps.volume * ps.purchase_price # 1000 * 9.0 = 9,000
first + second = 19,800
I want to simply get 19,800. Ideally I would like to do this in a very Ruby way.
If I were simply summing up all the values in 1 total_spend, I know I could simply do: p.port_stocks.map(&:total_spend).sum and that would be that.
But not sure how to do something similar when I am first doing a math operation on each object, then adding up all the products from all the objects. This should obviously work for 2 objects or 500.
The best way of doing this using Rails is to pass a block to sum, such as the following:
p.port_stocks.sum do |port_stock|
port_stock.volume * port_stock.purchase_price
end
That uses the method dedicated to totalling figures, and tends to be very fast and efficient - particularly when compared to manipulating the data ahead of calling a straight sum without a block.
A quick benchmark here typically shows it performing ~20% faster than the obvious alternatives.
I've not been able to test, but give that a try and it should resolve this for you.
Let me know how you get on!
Just a quick update as you also mention the best Ruby way, sum was introduced in 2.4, though on older versions of Ruby you can use reduce (also aliased to inject):
p.port_stocks.reduce(0) do |sum, port_stock|
sum + (port_stock.volume * port_stock.purchase_price)
end
This isn't as efficient as sum, but thought I'd give you the options :)
You are right to use Array#map to iterate through all stocks, but instead to sum all total_spend values, you could calculate it for each stock. After, you sum all results and your done:
p.port_stocks.map{|ps| ps.volume * ps.purchase_price}.sum
Or you could use Enumerable#reduce like SRack did. This would return the result with one step/iteration.

processing a variable size map in pig

I have a data set that is incoming as
(str,[[40,74],[50,75],[60,73],[70,43]])
and I need to be able to get this in the output variable using pig:
str, 40, 74
str , 50, 75
str, 60, 73
str, 70, 43
and this could be variable set of elements.
Tried with tokenizing and then flatten, but that doesn't help as it creates token using comma. and end up being this way..
str , {([[40), (74]), ... }
Would any one suggestions on if I could use built in functions or write a UDF for this.
many thanks,
Ana
You will need to write a custom UDF to parse this. Assuming your data does not get more complicated than this, you can probably get away with a quick, shallow method of parsing using String.split with delimiter "],[".

Resources