AutoHotkey Community

It is currently May 27th, 2012, 2:03 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1 ... 8, 9, 10, 11, 12, 13, 14 ... 28  Next
Author Message
 Post subject: Matrix multiplication
PostPosted: July 3rd, 2009, 4:31 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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 don’t 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%]”.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 12th, 2009, 2:47 am 
Offline

Joined: July 9th, 2009, 9:25 pm
Posts: 120
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.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 12th, 2009, 4:41 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Clever!


Report this post
Top
 Profile  
Reply with quote  
PostPosted: January 3rd, 2010, 2:17 am 
Offline

Joined: August 3rd, 2007, 8:01 am
Posts: 555
Location: Houston, TX
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:Tas ... AutoHotkey


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 6:49 pm 
Offline
User avatar

Joined: September 5th, 2009, 2:06 pm
Posts: 1713
Location: Somewhere near you
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.

_________________
Image
The quick onyx goblin jumps over the lazy dwarf


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 7:45 pm 
Some are too easy http://rosettacode.org/wiki/Repeating_a_string
Code:
MsgBox % Repeat("ha", 5)

Repeat(String, Multiply)
   {
    Loop % Multiply
      NewString .= String
    Return NewString   
   }


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 9:02 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
... you could note that the back quote symbol is escape character, and so you have to double it, if you want to see it.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 10:45 pm 
Offline

Joined: October 7th, 2006, 4:50 pm
Posts: 3157
Location: MN, USA
tomoe_uehara wrote:
I'd like to test the Look and Say Sequence.

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! :evil: [/Rant]


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 11:08 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
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.

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 11:52 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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:
Code:
like ``this'
All the escape sequences are special. Double quotes inside quoted literal strings needed to be doubled, too:
Code:
like ""this""


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 30th, 2010, 11:55 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
...btw, you can avoid AHK loops with StringReplace, or with RegExReplace:
Code:
VarSetCapacity(x,4,1)
MsgBox % RegExReplace(x,".","ha")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 31st, 2010, 12:35 am 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
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
}


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 31st, 2010, 7:05 am 
Offline
User avatar

Joined: September 5th, 2009, 2:06 pm
Posts: 1713
Location: Somewhere near you
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 =)

_________________
Image
The quick onyx goblin jumps over the lazy dwarf


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 2:45 am 
Offline

Joined: March 27th, 2008, 2:14 pm
Posts: 700
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


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: February 2nd, 2010, 5:32 pm 
Offline
User avatar

Joined: September 5th, 2009, 2:06 pm
Posts: 1713
Location: Somewhere near you
tidbit wrote:
Found this link on mandelbrot.
Apocalyse~r's code for mandelbrot

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.

_________________
Image
The quick onyx goblin jumps over the lazy dwarf


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 417 posts ]  Go to page Previous  1 ... 8, 9, 10, 11, 12, 13, 14 ... 28  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group