Autohotkey Matrix (AHK) - autohotkey

At the moment I am working on AI in AHK.
Now I have the problem that I don't know how to deal with a matrix. See below an example matrix:
WeightLooper := 1
Loop %NumberOfWeightsLayerTotal%
{
Random, Weight_%WeightLooper%, -1.0, 1.0
WeightLooper := WeightLooper + 1
}
WEIGHTS_1 := Array([Weight_1, Weight_2, Weight_3, Weight_4], [Weight_5, Weight_6, Weight_7, Weight_8], [Weight_9, Weight_10, Weight_11, Weight_12])
TRAINING_INPUTS := []
rows := (LastFilledY - 1)
columns := (LastFilledX - 1)
Xas := 0
Yas := 0
Loop, % rows
{
Xas := 0
Yas := Yas + 1
row := []
Loop, % columns
{
Xas := Xas + 1
row.push(myarray[Yas][Xas])
}
TRAINING_INPUTS.push(row)
}
Now I have a matrix of 3x4. Suppose I want a matrix of 10x10, how do I do that? So basically I want to create a variable matrix.
I ask this because my input (csv file) can vary from 2x2 to 1000000x1000000.

I'd probably recommend pushing a new array into the array in a loop:
WEIGHTS_1 := []
rows := 5
columns := 7
Loop, % rows
{
row := []
Loop, % columns
{
Random, weight, -1.0, 1.0
row.push(weight)
}
WEIGHTS_1.push(row)
}
Example output:
[[-0.678368, -0.768605, -0.274922, 0.049760, -0.133968, -0.876030, -0.235799]
,[-0.296078, 0.359816, -0.461632, 0.788800, -0.707147, -0.200223, -0.473914]
,[0.474090, 0.085090, 0.458321, -0.820574, 0.145089, 0.193249, 0.990545]
,[0.205461, 0.901953, -0.137901, 0.279726, 0.562361, -0.019861, -0.887540]
,[0.504811, -0.876628, -0.127397, 0.156817, 0.873983, 0.859992, -0.879222]]

Related

Delay Timer in Structured Text

I have Just started working on PLC using Structured text, I have to store values in Array of Temperature variable after delay of 1 min every time but i am not able to do that.
FOR i := 0 TO 5 DO
Temp[i] := tempsensor;
END_FOR;
This is kind a pseudo code.
I just need to bring in the delay in the loop that after every 1 min it could read the value and store it in the array location.
Even if there is any other way then I will really appreciate that.
Try this
VAR
i:INT;
Temp: ARRAY[0..10000] OF LREAL;
delayTimer: TON;
END_VAR
delayTimer(IN := not delayTimer.Q, PT := T#1m);
IF delayTimer.Q THEN
Temp[i] := tempsensor;
i := i + 1;
IF i > 10000 THEN
i := 0;
END_IF;
END_IF;
After 1 minute it will record 1 temperature value and index the array. If it reaches the end of the array it will start to write over at the beginning.
Once every minute you cycle through array and set values.
VAR
i: INT := 1; (* Cycle number *)
temp: ARRAY[1..5] OF REAL; (* Array of temperatures *)
ton1: TON; (* Timer *)
END_VAR
ton1(IN := NOT ton1.Q, PT := T#1m);
IF ton1.Q THEN
temp[i] := tempsensor;
IF i >= 5 THEN i := 1 ELSE i := i + 1 END_IF;
END_IF;

Numbering with TRichEdit in Delphi

I am trying to implement numbering in TRichEdit component, Delphi. Ideally I want to get the same behavior as in these 3rd party component:
As you can see Numbering button works similar way as Bullet button. I mean it sets FirstIdent or LeftIdent (I am not sure) and put the numbers 1,2,3,... instead of bullets. When you move cursor to the left close to number it does not allow to move onto number but jumps one line up.
This is what I got so far:
procedure TMainForm.NumberingButtonClick(Sender: TObject);
var
i: Integer;
s: String;
begin
if NumberingButton.Down then
begin
Editor.Paragraph.Numbering := nsNone;
i := Editor.ActiveLineNo;
s := Editor.Lines[i];
insert(inttostr(i)+'. ', s, 1);
//Editor.Paragraph.LeftIndent := 10;
Editor.Paragraph.FirstIndent := 10;
Editor.Lines[i] := s;
end;
end;
But it does not work as I want. Anybody have any ideas?
This code works exactly how I expected:
procedure TMainForm.NumberingButtonClick(Sender: TObject);
var
i: Integer;
s: String;
fmt: TParaFormat2;
begin
FillChar(fmt, SizeOf(fmt), 0);
fmt.cbSize := SizeOf(fmt);
fmt.dwMask := PFM_NUMBERING or PFM_NUMBERINGSTART or
PFM_NUMBERINGSTYLE or PFM_NUMBERINGTAB;
if NumberingButton.Down then
fmt.wNumbering := 2
else
fmt.wNumbering := 0;
// wNumbering:
// 0 - no numbering
// 1 - bullet list (·, ·, ·, ...).
// 2 - Arabic numbers (1, 2, 3, ...).
// 3 - small letters (a, b, c, ...).
// 4 - capital letters (A, B, C, ...).
// 5 - small Roman numbers (i, ii, iii, ...).
// 6 - capital Roman numbers (I, II, III, ...).
// 7 - Unicode character sequence
fmt.wNumberingStart := 1;
// wNumberingStart:
// The number at which the numbering starts.
fmt.wNumberingStyle := $200;
// wNumberingStyle:
// Numbering Style
// 0 : 1)
// $100 : (1)
// $200 : 1.
// $300 : 1
// $400 : remove list
// $8000 : continues to number the list without changing the style
fmt.wNumberingTab := 1440 div 4;
// wNumberingTab:
// the space between number and paragraph text
Editor.Perform( EM_SETPARAFORMAT, 0, lParam( #fmt ) );
if BulletsButton.Down then
BulletsButton.Down := False;
end;
Thanks to www.decoding.dax.ru

Best way to index large file

I have a file with about 100gb with a word:tag per line. I want to index these on word to easily get the list of tags for a given word.
I wanted to save this on boltdb (mainly to checkout boltdb) but random write access is bad so I was aiming to index the file in some other way first, then moving all of it to boltdb without need to check for duplicates or de/serialisation of the tag list
So, for reference, if I simply read the file into memory (discarding data), I get about 8 MB/s.
If I write to boltdb files using code such as
line := ""
linesRead := 0
for scanner.Scan() {
line = scanner.Text()
linesRead += 1
data := strings.Split(line, ":")
err = bucket.Put([]byte(data[0]), []byte(data[1]))
logger.FatalErr(err)
// commit on every N lines
if linesRead % 10000 == 0 {
err = tx.Commit()
logger.FatalErr(err)
tx, err = db.Begin(true)
logger.FatalErr(err)
bucket = tx.Bucket(name)
}
}
I get about 300 Kb/s speed and this is not even complete (as it's not adding tag to each word, only stores the last occurrence). So adding the array and JSON serialisation would definitely lower that speed...
So I gave mongodb a try
index := mgo.Index{
Key: []string{"word"},
Unique: true,
DropDups: false,
Background: true,
Sparse: true,
}
err = c.EnsureIndex(index)
logger.FatalErr(err)
line := ""
linesRead := 0
bulk := c.Bulk()
for scanner.Scan() {
line = scanner.Text()
data := strings.Split(line, ":")
bulk.Upsert(bson.M{"word": data[0]}, bson.M{"$push": bson.M{"tags": data[1]}})
linesRead += 1
if linesRead % 10000 == 0 {
_, err = bulk.Run()
logger.FatalErr(err)
bulk = c.Bulk()
}
}
And I get about 300 Kb/s as well (though Upsert and $push here handle appending to the list).
I tried with a local MySQL instance as well (indexed on word) but speed was like 30x slower...

Call by Reference and Call by Value Result

Well, there was a debate on the below code between me and my friend. We're a bit confused about the output it produce. Can someone clarify the call-by-reference and call-by value result for the below piece of code?
program params;
var i: integer;
a: array[1..2] of integer;
procedure p(x,y: integer);
begin
x := x + 1;
i := i + 1;
y := y + 1;
end;
begin
a[1] := 1;
a[2] := 2;
i := 1;
p( a[i],a[i] );
output( a[1],a[2] );
end.
The resulting output of this program in the case that the
parameters are transmitted to procedure p by value-result and by reference.
Call by Value
x and y in p are local variables initialized with the actual parameters, while i is a global variable, so the call p( a[i],a[i] ) is equivalent to:
x := 1 /* The value of a[i] */
y := 1 /* The value of a[i] */
x := 2 /* x + 1 */
i := 2 /* i + 1 */
y := 2 /* y + 1 */
and at the end the values 1, 2 are printed since they are the values of a[1], a[2] which weren't changed.
Call by Reference
Both x and y in p are alias for a[1] and (again) a[1] (since i = 1 when the procedure is called), so the call is equivalent to:
a[1] := 2 /* a[1] + 1 */
i := 2 /* i + 1 */
a[1] := 3 /* a[1] + 1 */
and at the end the values 3, 2 are printed.
Call by Name
Call by Name is equivalent to Call by Reference when simple variables are passed as parameters, but is different when you pass an expression that denotes a memory location, like a subscript. In this case the actual parameter is re-evaluated each time it is encountered. So in this case, this is the effect of the call of p( a[i],a[i] ):
a[1] := 2 /* since i = 1, the result is equal to a[1] + 1 */
i := 2 /* i + 1 */
a[2] := 3 /* since i is now 2, the result is equal to a[2] + 1 */
and at the end the values 2, 3 are printed. In practice the implementation calls an anonymous function (a “thunk”), each time it must evaluate a parameter.
Call by Value Result
Just to complete the discussion, here is the case for the value-result parameter passing, in which x and y are initialized at the beginning of the procedure execution with the values of the actual parameters, and, at the end of the execution of the procedure, are copied back to the original variables addresses:
x := 1 /* The value of a[i] */
y := 1 /* The value of a[i] */
x := 2 /* x + 1 */
i := 2 /* i + 1 */
y := 2 /* y + 1 */
a[1] := 2 /* the value of x is copied back to a[1] */
a[1] := 2 /* the value of y is copied back to a[1] (not a[2]!) */
and at the end the values 2, 2 are printed.
For a discussion of the different ways of passing parameters, see for instance this.
procedure p(x, y: integer);
begin
end;
In this case the varaibles passed as parameters are never modified. They are copied either to two registers (likely x: EAX and y: ECX) or to the stack. (depending on the compiler ABI)
procedure p(var x, y: integer);
begin
end;
In this case the original parameters are modified. x and y are pointers to the original variables passed as parameters.

Maple: RNG is not random

i was "finding Pi" with Monte Carlo Method, but the answer was incorrect. The oryginal code was:
RandomTools[MersenneTwister]: with(Statistics):
tries := 10000:
s := 0;
for i to tries do
if GenerateFloat()^2+GenerateFloat()^2 < 1 then s := s+1 end if;
end do:
evalf(4*s/tries)
It gives answer aroud 2.8-2.85
when I change the code to
s := 0;
x := Array([seq(GenerateFloat(), i = 1 .. tries)]);
y := Array([seq(GenerateFloat(), i = 1 .. tries)]);
for i to tries do
if x[i]^2+y[i]^2 < 1 then s := s+1 end if;
end do:
evalf(4*s/tries)
Then the answer is correct. I have no idea why i can't generate number in "for" loop.
I've founded that the mean of it is the same, but the variance is different.
For:
tries := 100000;
A := Array([seq(GenerateFloat(), i = 1 .. 2*tries)]);
s1 := Array([seq(A[i]^2+A[tries+i]^2, i = 1 .. tries)]);
Mean(s1);
Variance(s1);
s2 := Array([seq(GenerateFloat()^2+GenerateFloat()^2, i = 1 .. tries)]);
Mean(s2);
Variance(s2);
output is:
0.6702112097021581
0.17845439723457215
0.664707674135025
0.35463131700965245
What's wrong with it? GenerateFloat() should be as uniform as possible.
Automatic simplification is turning your,
GenerateFloat()^2+GenerateFloat()^2
into,
2*GenerateFloat()^2
before GenerateFloat() is evaluated.
One simple change to get it to work as you expected would be separate them. Eg,
restart:
with(RandomTools[MersenneTwister]):
tries := 10^4:
s := 0:
for i to tries do
t1,t2 := GenerateFloat(),GenerateFloat();
if t1^2+t2^2 < 1 then s := s+1 end if;
end do:
evalf(4*s/tries);
Another way is to use a slightly different construction which doesn't automatically simplify. Consider, single right quotes (uneval quotes) don't stop automatic simplification (which is a definition of the term if you want).
'f()^2 + f()^2';
2
2 f()
But the following does not automatically simplify,
a:=1:
'f()^2 + a*f()^2';
2 2
f() + a f()
Therefore another easy workaround is,
restart:
with(RandomTools[MersenneTwister]):
tries := 10^4:
s := 0:
a := 1;
for i to tries do
if GenerateFloat()^2 + a*GenerateFloat()^2 < 1 then s := s+1 end if;
end do:
evalf(4*s/tries);