 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
Laszlo
Joined: 14 Feb 2005 Posts: 4517 Location: Boulder, CO
|
Posted: Fri Jul 03, 2009 4:31 pm Post subject: Matrix multiplication |
|
|
For testing Matrix multiplication we need a couple of I/O functions. Matrix creates a matrix from a list of rows, using global AHK arrays (because arrays cannot be passed as function parameters), and storing the dimensions in the array. MatrixPrint puts the entries in a string, to be printed. The actual matrix multiplication is done by 3 levels of loops, directly following the definition. Note that the names of parameters and local variables are prefixed with underscore, to minimize the danger of a collision with a global variable. | Code: | Matrix("b"," ; rows separated by ","
, 1 2 ; entries separated by space or tab
, 2 3
, 3 0")
MsgBox % "B`n`n" MatrixPrint(b)
Matrix("c","
, 1 2 3
, 3 2 1")
MsgBox % "C`n`n" MatrixPrint(c)
MatrixMul("a",b,c)
MsgBox % "B * C`n`n" MatrixPrint(a)
MsgBox % MatrixMul("x",b,b)
Matrix(_a,_v) { ; Matrix structure: m_0_0 = #rows, m_0_1 = #columns, m_i_j = element[i,j], i,j > 0
Local _i, _j = 0
Loop Parse, _v, `,
If (A_LoopField != "") {
_i := 0, _j ++
Loop Parse, A_LoopField, %A_Space%%A_Tab%
If (A_LoopField != "")
_i++, %_a%_%_i%_%_j% := A_LoopField
}
%_a% := _a, %_a%_0_0 := _j, %_a%_0_1 := _i
}
MatrixPrint(_a) {
Local _i = 0, _t
Loop % %_a%_0_0 {
_i++
Loop % %_a%_0_1
_t .= %_a%_%A_Index%_%_i% "`t"
_t .= "`n"
}
Return _t
}
MatrixMul(_a,_b,_c) {
Local _i = 0, _j, _k, _s
If (%_b%_0_0 != %_c%_0_1)
Return "ERROR: inner dimensions " %_b%_0_0 " != " %_c%_0_1
%_a% := _a, %_a%_0_0 := %_b%_0_0, %_a%_0_1 := %_c%_0_1
Loop % %_c%_0_1 {
_i++, _j := 0
Loop % %_b%_0_0 {
_j++, _k := _s := 0
Loop % %_b%_0_1
_k++, _s += %_b%_%_k%_%_j% * %_c%_%_i%_%_k%
%_a%_%_i%_%_j% := _s
}
}
} |
Matrices can be implemented by lists, `t and `n separated entries and rows, too, but finding and changing an entry would be slower and counterintuitive. With arrays we can simply write a_3_2 := 5. If you dont like underscores in matrices, the first one can be replaced by [, the second one with ][ and add a trailing ]. This changes the above assignment to a[3][2] := 5. However, we still cannot write a[i][j+1]; we would need tmp:=j+1, a[%i%][%tmp%]. |
|
| Back to top |
|
 |
temp01
Joined: 09 Jul 2009 Posts: 120
|
Posted: Mon Oct 12, 2009 2:47 am Post subject: |
|
|
How about this for Variable Swapping: | Code: | Swap(ByRef Left, ByRef Right)
{
Left := (Right "", Right := Left)
} |
No temporary variable needed.
This works due to the way the comma operator works in expressions - it evaluates from left to right but returns the result of the left most expression. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4517 Location: Boulder, CO
|
Posted: Mon Oct 12, 2009 4:41 pm Post subject: |
|
|
| Clever! |
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 312 Location: Houston, TX
|
Posted: Sun Jan 03, 2010 2:17 am Post subject: second ahk wave on rosettacode |
|
|
We have slipped to 14.
There were a bunch of oop tasks that autohotkey was not well suited for before, now they should be fairly easy.
Now that we have objects in Lexikos' build, we can probably make a second push on rosettacode.
I am probably going to focus on the ppm based tasks.
I would like to hit top 5 this time...
http://rosettacode.org/wiki/Reports:Tasks_not_implemented_in_AutoHotkey |
|
| Back to top |
|
 |
tomoe_uehara
Joined: 05 Sep 2009 Posts: 170 Location: Somewhere near you
|
Posted: Sat Jan 30, 2010 6:49 pm Post subject: |
|
|
I'd like to test the Look and Say Sequence. But there's no instruction on how to use the GUI...
What should I put on the edit field, is it a sequence like '1121'? And, please add the +default at the button's option, so I can hit the enter button instead of clicks on the button. _________________
I'll be away for a moment, until I get a new CPU. Heheee... |
|
| Back to top |
|
 |
Guest
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4517 Location: Boulder, CO
|
Posted: Sat Jan 30, 2010 9:02 pm Post subject: |
|
|
| ... you could note that the back quote symbol is escape character, and so you have to double it, if you want to see it. |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 1898 Location: MN, USA
|
Posted: Sat Jan 30, 2010 10:45 pm Post subject: |
|
|
Try this. | Code: | Gui, Add, Edit, w500 vIn, 1
Gui, Add, Edit, wp r10 vOut
Gui, Add, Button, Default, Calculate
Gui, Show,,Look-and-Say sequence
return
GuiClose:
ExitApp
ButtonCalculate:
Gui, Submit, NoHide
GuiControl,, Out,% In "`n" Out
GuiControl,, In,% lookSay(In)
return
lookSay(input,start=2,count=1) {
prev := SubStr(input,start-1,1)
If (start > StrLen(input))
Return, count prev
Else If (SubStr(input,start,1) = prev)
Return, lookSay(input,++start,++count)
Else Return, count prev lookSay(input,++start)
} |
[Rant] Untested code drives me crazy. Why do people waste their time writing code when they don't care whether it works or not? I'll never understand that... and then to go and post it as a representative example of AHK! [/Rant] _________________ http://autohotkey.net/~jaco0646/ |
|
| Back to top |
|
 |
sinkfaze
Joined: 19 Mar 2008 Posts: 2721 Location: the tunnel(?=light)
|
Posted: Sat Jan 30, 2010 11:08 pm Post subject: |
|
|
| Laszlo wrote: | | ... you could note that the back quote symbol is escape character, and so you have to double it, if you want to see it. |
But even that's not enough (unless I'm missing something), consider this:
| Code: | MsgBox % Repeat("ain`t", 5)
Repeat(str,num) {
Loop % num {
if str contains ``
StringReplace, str, str, ``, ````, All
res.=str
}
Return res
} |
Returns 'ain' followed by a tab 5 times. _________________ Try Quick Search for Autohotkey or see the tutorial for newbies. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4517 Location: Boulder, CO
|
Posted: Sat Jan 30, 2010 11:52 pm Post subject: |
|
|
You probably meant to replace all back quotes once: | Code: | Repeat(str,num) {
StringReplace, str, str, ``, ````, All
Loop % num
res.=str
Return res
} | However, it does not help, because the AHK interpreter replaces `t with tab when the script is loaded. You have to write your strings according to AHK conventions: when you want back quote, double it:All the escape sequences are special. Double quotes inside quoted literal strings needed to be doubled, too: |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4517 Location: Boulder, CO
|
Posted: Sat Jan 30, 2010 11:55 pm Post subject: |
|
|
...btw, you can avoid AHK loops with StringReplace, or with RegExReplace: | Code: | VarSetCapacity(x,4,1)
MsgBox % RegExReplace(x,".","ha") |
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4517 Location: Boulder, CO
|
Posted: Sun Jan 31, 2010 12:35 am Post subject: |
|
|
for Look and Say Sequence:
| jaco0646 wrote: | | Try this... |
Nice. In this case an iterative algorithm is simpler and faster, though: | Code: | loop
MsgBox % x := x="" ? 1 : Look_Say(x)
Look_Say(x) {
Loop Parse, x
If (A_LoopField = d)
c++
Else
r .= c d, d := A_LoopField, c := 1
Return r c d
} |
|
|
| Back to top |
|
 |
tomoe_uehara
Joined: 05 Sep 2009 Posts: 170 Location: Somewhere near you
|
Posted: Sun Jan 31, 2010 7:05 am Post subject: |
|
|
Those 'look and say' scripts are good.
Jaco's script uses GUI and I can see last result.
Laszlo's uses msgbox, clean and simple but only shows current result. Well done =) _________________
I'll be away for a moment, until I get a new CPU. Heheee... |
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 378
|
Posted: Tue Feb 02, 2010 2:45 am Post subject: |
|
|
Sorry for bringing up an old subject: I have issue with the current moving average code. The code itself is excellent; however, I think its not ideal for the purpose of rosettacode. Keeping static copies of the different values inside the function doesn't conform to the spirit of the objective imo, and is not universal (i.e. it couldn't be used in a library).
Something like this might be better:
| Code: | avg := i := 0
MsgBox % MovAvg(avg, 3 , ++i)
MsgBox % MovAvg(avg, 8 , ++i)
MsgBox % MovAvg(avg, 16 , ++i)
MsgBox % MovAvg(avg, 53 , ++i)
MsgBox % MovAvg(avg, 22.12 , ++i)
MsgBox % MovAvg(avg, 34.1142, ++i)
MovAvg(ByRef ca, x, i=0) {
return ca+=(x-ca)/i
} | While keeping another two variables outside the function doesn't look very pretty, using the ByRef average variable helps a bit. This function conforms to a real moving (cumulative) average, without ever storing the total (which could overflow with an average of many large numbers). And since it is stateless it's more universal.
Because of new ahk features it now stores floating point numbers to 15 digits accuracy by default which is probably sufficient for a great number of small averaging; therefore, a separate floating point function should not be necessary. _________________
Scripts - License |
|
| Back to top |
|
 |
tomoe_uehara
Joined: 05 Sep 2009 Posts: 170 Location: Somewhere near you
|
Posted: Tue Feb 02, 2010 5:32 pm Post subject: |
|
|
Jaco0646, tidbit told me about this link, but it seems the author isn't active anymore (for a year), and I can't find any AHK script in the RC about mandelbrot. _________________
I'll be away for a moment, until I get a new CPU. Heheee... |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|