Lua sort table value ascending - sorting

I have a table I need to sort sel_notes[i].Pitch to ascending order.
These are the selected midi notes in a midi editor.
sel notes table :
sel_notes = {}
sel_notes[pitch] = {Pitch = pitch, Idx = i}
sel_notes[i] = {Pitch = pitch, Idx = i}
sel_notes table gives this using table.save-1.0.lua:
return {
-- Table: {1}
{
[64]={2},
[65]={3},
[66]={4},
[67]={5},
[52]={6},
[69]={7},
[68]={8},
[55]={9},
[56]={10},
[57]={11},
[58]={12},
[59]={13},
[60]={14},
[61]={15},
[62]={16},
[63]={17},
},
-- Table: {2}
{
["Pitch"]=63,
["Idx"]=64,
},
-- Table: {3}
{
["Pitch"]=52,
["Idx"]=65,
},
-- Table: {4}
{
["Pitch"]=58,
["Idx"]=66,
},
-- Table: {5}
{
["Pitch"]=52,
["Idx"]=67,
},
-- Table: {6}
{
["Pitch"]=52,
["Idx"]=67,
},
-- Table: {7}
{
["Pitch"]=58,
["Idx"]=69,
},
-- Table: {8}
{
["Pitch"]=63,
["Idx"]=68,
},
-- Table: {9}
{
["Pitch"]=52,
["Idx"]=55,
},
-- Table: {10}
{
["Pitch"]=58,
["Idx"]=56,
},
-- Table: {11}
{
["Pitch"]=63,
["Idx"]=57,
},
-- Table: {12}
{
["Pitch"]=58,
["Idx"]=69,
},
-- Table: {13}
{
["Pitch"]=63,
["Idx"]=59,
},
-- Table: {14}
{
["Pitch"]=52,
["Idx"]=60,
},
-- Table: {15}
{
["Pitch"]=52,
["Idx"]=61,
},
-- Table: {16}
{
["Pitch"]=63,
["Idx"]=62,
},
-- Table: {17}
{
["Pitch"]=63,
["Idx"]=68,
},
}
I need the table sorted so if I do this
for 1 = 1, 15 do
note = sel_notes[i].Pitch
index = sel_notes[i].Idx
print(note,index)
end
I will get this:
52 55
52 60
52 61
52 65
52 67
58 56
58 58
58 63
58 66
58 69
63 57
63 59
63 62
63 64
63 68
So then I'll be able to change the pitch value of the notes so they match the pitch value of another table that has the chord notes.
So:
pitch 52 to 55
pitch 58 to 59
pitch 63 to 62

You can use the table.sort method.
The first argument is a table and the second (optional) is a function that returns whether its first argument is smaller than the second.
Of course there's the problem of the rather weird table structure, so you need to get rid of the first element, which is meaningless. The easiest way for doing this would be using table.remove.
So something like
local function sort(tab)
table.remove(tab, 1) -- Remove first element
table.sort(tab, function(a, b)
return true -- Your sorting criterion goes here
end)
end

Related

How to generate Set Y with multiple elements in CPLEX?

I have written this code to generate a Set Y, with single element
int m=3 ;
range I= 1..m;
int w[i in I]=i;
int q= min(i in I)w[i] ;
int W=1000;
int Ea[I];
{int} B={381,198,291};
{int} E ={rand(f) | f in B: f>0};
execute
{
writeln("E is ", E)
var j=1
for(var k in E)
{
Ea[j]=k; //Array Ea has same values as set E
j=j+1;
}
}
int ok[i in I]=(sum(i in I)Ea[i]*w[i]<=W-q);
{int} Y= {sum(i in I)Ea[i]*w[i]|x in 0..W-q , i in I: ok[i]==1 } ;
execute{
writeln(Y);
}
The output of above code and variable values are
E is {93 42 31}
Y is {270}
Variable Values
How can I generate multiple elements in Set Y, since the rand function has been used while calculating E?
You can use arrays for several casts:
{int} B={381,198,291};
range casts=1..10;
{int} E[c in casts] ={rand(f) | f in B: f>0};
execute
{
writeln(E);
}
int Y[c in casts]= sum(e in E[c]) e;
execute{
writeln(Y);
}
gives
[{93 42 31} {378 131 243} {25 177 61} {4 48 212} {276 1 256} {289 138 264}
{366 192 177} {138 150 164} {125 163 246} {315 180 240}]
[166 752 263 264 533 691 735 452 534 735]

Gives grades according to marks

i have SSRS report i want to give "Accepted" if value between 50 and 65 and gives "Good" if value between 65 and 75 and gives "V good" if value between 75 and 85 and gives "Excellent" if value between 85 and 100
how can i do it my code :
=(IIF(Fields!marks.Value >50, "fail", 0)) And (IIF(Fields!marks.Value <65, "Accepted", 0))
You could use the Switch function to accomplish this:
=Switch(
Fields!marks.Value < 50, "fail",
Fields!marks.Value < 65, "accepted",
Fields!marks.Value < 75, "good",
Fields!marks.Value < 85, "v good",
True, "excellent"
)
what you want is a switch statement:
=switch(
Fields!marks.Value<50,"Fail",
Fields!marks.Value>= 50 and Fields!marks.Value<65,"Accepted",
Fields!marks.Value>= 65 and Fields!marks.Value<75,"Good",
Fields!marks.Value>= 75 and Fields!marks.Value<85,"V Good",
Fields!marks.Value>= 85 and Fields!marks.Value<100,"Excellent",
,"")

How to multiple two Eigen tensors along batch dimension?

For example, I have a tensor A(2,3,4) and tensor B(3,4) and want to make koefs multiplication(not Contraction operation) of A along dimension 0( batch dimension) with B. How to do this without for loop?
If I understand your question, you could first reshape B to (1,3,4) and then use broadcasting to replicate the values along axis 0. Don't know if this is very efficient, but it works.
auto A = Tensor<float, 3>(2, 3, 4);
A.setValues({
{
{1,2,3,4},
{5,6,7,8},
{9,10,11,12},
},{
{ 10,20,30,40 },
{ 50,60,70,80 },
{ 90,100,110,120 },
}
});
auto W = Tensor<float, 2>(3,4);
W.setValues({
{ 1,2,3,4 },
{ 5,6,7,8 },
{ 9,10,11,12 }
});
auto three_dims = Eigen::array<int, 3>({ 1,3,4 });
auto bcast = Eigen::array<int, 3>({ 2,1,1 });
auto X = W.reshape(three_dims).broadcast(bcast);
auto Y = A*X;
std::cout << Y << std::endl;
Prints:
1 25 81 4 36 100 9 49 121 16 64 144
10 250 810 40 360 1000 90 490 1210 160 640 1440

modify json file using cJSON library

what i want to do is reading a json format file and modify it then writing the modified content to file.
55 cJSON *root,*basicpara;
56 char *out;
57
58 root = dofile("basicparameter.cfg");
59 out = cJSON_Print(root);
60 printf("before modify:%s\n",out);
61 free(out);
62 basicpara = cJSON_GetObjectItem(root,"basicparameter");
63 cJSON_GetObjectItem(basicpara,"mode")->valueint = 0;
64 cJSON_GetObjectItem(basicpara,"TimeoutPoweron")->valueint = 10;
65
66 out = cJSON_Print(root);
67 printf("after modify:%s\n",out);
68 free(out);
69 //write_file("basicparameter.cfg",out);
70 cJSON_Delete(root);
i am confused why both contents are the same...
before modify:{
"basicparameter": {
"mode": 1,
"nBefore": 2,
"nAfter": 2,
"LuxAutoOn": 50,
"LuxAutoOff": 16,
"TimeoutPoweron": 30
}
}
after modify:{
"basicparameter": {
"mode": 1,
"nBefore": 2,
"nAfter": 2,
"LuxAutoOn": 50,
"LuxAutoOff": 16,
"TimeoutPoweron": 30
}
}
Please use the cJSON_SetNumberValue macro for setting the number. The problem is, that you are only setting the valueint property but printing relies on the valuedouble property.
Having both valueint and valuedouble in cJSON was a terrible design decision and will probably confuse many people in the future as well.

Serialize ternary tree in c

I am writing ternary tree for string lookup in c.
So far I am able to store strings and check whether string exists.
Example insert
Node *node_link(Node *this, char *line) {
26 if(!this) {
27 this = node_create(*line);
28 }
29 if(*line < this->ch) {
30 this->left = node_link(this->left, line);
31 } else if(*line == this->ch) {
32 if(*(++line) == 0) {
33 this->there = TRUE;
34 return this;
35 } else {
36 this->mid = node_link(this->mid, line);
37 }
38 } else {
39 this->right = node_link(this->right, line);
40 }
41 return this;
42 }
I need to get back the strings that I inserted. For example if I inserted
hat
cat
catpost
tent
tents
square
squarish
I should be able to extract the list with same strings. I do not get it if I do tree traversal (DFS). How can I get the list of stored items?
Example traversal
void node_walk_link(Node *this, char *line) {
44 if(this) {
45 node_walk_link(this->left, line);
46 node_walk_link(this->right, line);
47 node_walk_link(this->mid, line_push(line, this->ch, 0));
48 }
49 }
Also how can find union of two ternary trees? That have items from both trees without duplicates.

Resources