 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
tidbit
Joined: 09 Mar 2008 Posts: 1806 Location: Minnesota, USA
|
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Tue Feb 02, 2010 5:08 pm Post subject: |
|
|
| infogulch wrote: | | 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). | Well, you don't even need a function: | Code: | avg := i := 0
MsgBox % avg += (3-avg)/++i
MsgBox % avg += (8-avg)/++i
MsgBox % avg += (16-avg)/++i
MsgBox % avg += (53-avg)/++i |
|
|
| Back to top |
|
 |
infogulch
Joined: 27 Mar 2008 Posts: 649
|
Posted: Tue Feb 02, 2010 5:24 pm Post subject: |
|
|
| Laszlo wrote: | | Well, you don't even need a function: | You are absolutely correct.
...
Actually: | Quote: | Create a stateful function/class/instance that takes a number as argument and returns a simple moving average of its arguments so far.
Note that the moving average has a period and numbers beyond the period must be dropped from the calculation of the average. |
I didn't read the goal correctly, so I misinterpreted moving average for cumulative average. I don't have any issues anymore, sorry.  _________________ Scripts - License |
|
| Back to top |
|
 |
ih57452
Joined: 06 Feb 2010 Posts: 57
|
Posted: Sat Feb 06, 2010 5:17 pm Post subject: Print a Multiplication Table |
|
|
Print a Multiplication Table
| Code: | SendMode, Input
Run, Notepad.exe,, Max
WinWaitActive, ahk_class Notepad
size = 12
x = 0
y = -1
Loop, % size + 2 {
If (y = -1)
Send, % format("X|")
Else If (y = 0)
Send, % format("|", "-")
Else Send, % format(y . "|")
Loop, %size% {
x += 1
If (y = -1)
Send, % format(x)
Else If (y = 0)
Send, % format("", "-")
Else If (x < y)
Send, % format("")
Else Send, % format(x * y)
}
x = 0
y += 1
Send, {Enter}
}
format(str, fill = " ") {
global size
While, (StrLen(str) <= StrLen(size * size + 1))
str := fill . str
Return, str
} |
I was trying to make the table look like the Perl example, but when I tried to put a "+" where the two lines intersected, my format() function apparently interpreted it as an operator instead of just a character in a string. I haven't figured out how to fix that yet, but a pipe will work. It works fine for the required 12x12 table, but turning it up to about 32x32 can cause some weird things to happen (looks like possibly a buffer getting filled up?). I've also considered making a version to display it in a AutoHotkey GUI and possibly another one to make it put the numbers into OpenOffice Calc. |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sat Feb 06, 2010 6:47 pm Post subject: |
|
|
Nice! You can write it little shorter: | Code: | size = 24
Loop % size + 2 {
t .= (y:=A_Index-2)<0 ? format("X|") : y=0 ? "`n" format("+", "-") : "`n" format(y . "|")
Loop %size%
t .= y<0 ? format(A_Index) : y=0 ? format("", "-") : A_Index<y ? format("") : format(A_Index * y)
}
Gui Font, s10, Courier New
Gui Add, Text,,%t%
Gui Show
format(str, fill = " ") {
Global size
VarSetCapacity(f, n:=StrLen(size*size)+1-StrLen(str), Asc(fill))
Return SubStr(f,1,n) str
} |
|
|
| Back to top |
|
 |
tinku99
Joined: 03 Aug 2007 Posts: 513 Location: Houston, TX
|
|
| Back to top |
|
 |
wolf_II
Joined: 18 Oct 2007 Posts: 343 Location: Saarland, Germany
|
Posted: Fri Apr 16, 2010 11:08 pm Post subject: Root Mean Square |
|
|
I have contributed the following (so far unsolved task):
| Code: | MsgBox, % RMS(1, 10)
RMS(a, b) { ; Root Mean Square
n := b - a + 1
Loop, %n%
Sum += (a + A_Index - 1) * (a + A_Index - 1)
Return, Sqrt(Sum / n)
}
|
As this is my first contribution to Rosetta, I would appreciate if anybody was kind enough to check if it's OK, please.
Here is the link: Averages/Root mean square
Wolf _________________ Wolf
Schön wär's, wenn's schön wär! |
|
| Back to top |
|
 |
wolf_II
Joined: 18 Oct 2007 Posts: 343 Location: Saarland, Germany
|
Posted: Sat Apr 17, 2010 12:52 am Post subject: |
|
|
And another one: Take notes on the command line
| Code: | Notes := "Notes.txt"
If 0 = 0 ; no arguments
{
If FileExist(Notes) {
FileRead, Content, %Notes%
MsgBox, %Content%
} Else
MsgBox, %Notes% does not exist
Goto, EOF
}
; date and time, colon, newline (CRLF), tab
Date := A_DD "/" A_MM "/" A_YYYY
Time := A_Hour ":" A_Min ":" A_Sec "." A_MSec
FileAppend, %Date% %Time%:`r`n%A_Tab%, %Notes%
; command line parameters, trailing newline (CRLF)
Loop, %0%
FileAppend, % %A_Index% " ", %Notes%
FileAppend, `r`n, %Notes%
EOF:
|
_________________ Wolf
Schön wär's, wenn's schön wär! |
|
| Back to top |
|
 |
jaco0646
Joined: 07 Oct 2006 Posts: 3113 Location: MN, USA
|
Posted: Sat Apr 17, 2010 2:00 am Post subject: |
|
|
Here's an alternative. | Code: | If 0 = 0 ;no arguments
{
Run, %comspec% /k type Notes.txt
ExitApp
}
FormatTime, var
var .= "`n" A_Tab
Loop, %0%
var .= %A_Index% A_Space
FileAppend, %var%`n, Notes.txt |
|
|
| Back to top |
|
 |
wolf_II
Joined: 18 Oct 2007 Posts: 343 Location: Saarland, Germany
|
Posted: Sat Apr 17, 2010 2:04 am Post subject: |
|
|
And another one: Averages/Pythagorean means
| Code: | A := ArithmeticMean(1, 10)
G := GeometricMean(1, 10)
H := HarmonicMean(1, 10)
If G Between %H% And %A%
Result := "True"
Else
Result := "False"
MsgBox, %A%`n%G%`n%H%`n%Result%
ArithmeticMean(a, b) {
n := b - a + 1
Loop, %n%
Sum += (a + A_Index - 1)
Return, Sum / n
}
GeometricMean(a, b) {
n := b - a + 1
Prod := 1
Loop, %n%
Prod *= (a + A_Index - 1)
Return, Prod ** (1 / n)
}
HarmonicMean(a, b) {
n := b - a + 1
Loop, %n%
Sum += 1 / (a + A_Index - 1)
Return, n / Sum
}
|
_________________ Wolf
Schön wär's, wenn's schön wär! |
|
| Back to top |
|
 |
wolf_II
Joined: 18 Oct 2007 Posts: 343 Location: Saarland, Germany
|
Posted: Sat Apr 17, 2010 2:26 am Post subject: |
|
|
@jaco0646: Nice one!
Wolf _________________ Wolf
Schön wär's, wenn's schön wär! |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sat Apr 17, 2010 3:39 pm Post subject: |
|
|
RMS: wolf_II's code is correct, but we can make it a little more general. This version computes the RMS of 1..10 arbitrary numbers: | Code: | MsgBox % RMS(1) ; 1
MsgBox % RMS(3,4) ; 3.535534
MsgBox % RMS(1,2,3,4,5,6,7,8,9,10) ; 6.204837
RMS(a0,a1="",a2="",a3="",a4="",a5="",a6="",a7="",a8="",a9="") { ; Root Mean Square of 1..10 arguments
s := a0*a0
Loop 10
If (a%A_Index% = "")
Return Sqrt(s/A_Index)
Else s += a%A_Index% ** 2
} | Another version computes the RMS of a comma separated list of numbers | Code: | MsgBox % RMS("1") ; 1
MsgBox % RMS("3,4") ; 3.535534
MsgBox % RMS("1,2,3,4,5,6,7,8,9,10") ; 6.204837
RMS(a) { ; Root Mean Square of list of numbers
s := 0, n := 0
Loop Parse, a, `,
s += A_LoopField ** 2, n++
Return Sqrt(s/n)
} | You can also use AHK arrays, and pass the array name as a parameter, but it is not nice, because the array has to be global and must not conflict with local variables.
The same ideas work for the other mean variants, too. |
|
| Back to top |
|
 |
wolf_II
Joined: 18 Oct 2007 Posts: 343 Location: Saarland, Germany
|
Posted: Sat Apr 17, 2010 5:30 pm Post subject: |
|
|
@Laszlo:
| Laszlo wrote: | | wolf_II's code is correct | Thank you for checking.
| rosettacode.org wrote: | | Compute the Root mean square of the numbers 1..10. |
| rosettacode.org also wrote: | | Compute all three of the Pythagorean means of the set of integers 1 through 10. |
I consider at present my published code sufficiently generalized for the given task. I will of course change it, if requested. I am nevertheless grateful for the comments so far, as I can learn from them.
I like especially the elegance of jaco0646's version that uses FileAppend only once. But I prefer MsgBox over a console popping up. While I am at it, let me say that I thought this would be a good place to show that AHK has got a GOTO command, although it is the first time I have ever used it.
I have done the following minor change to RMS:
| Code: | Sum += (a + A_Index - 1) * (a + A_Index - 1) ; previously (performance)
Sum += (a + A_Index - 1) ** 2 ; now (elegance)
|
Wolf _________________ Wolf
Schön wär's, wenn's schön wär! |
|
| Back to top |
|
 |
wolf_II
Joined: 18 Oct 2007 Posts: 343 Location: Saarland, Germany
|
Posted: Sat Apr 17, 2010 5:56 pm Post subject: |
|
|
@All:
From the list AutoHotkey examples needing attention I found Fork (not working on my PC, author unknown to me):
| Code: | instancenum = %1%+1
MsgBox, 4, Fork Process, %instancenum% number: run another?
IfMsgBox, Yes
Run, %A_ScriptFullName% %instancenum%
ExitApp |
I want to replace with:
| Code: | If 0 = 0 ; no arguments
InstanceNum = 1
Else
InstanceNum = %1%
MsgBox, 4, Fork Process, Number %InstanceNum%: Run another?
InstanceNum += 1
IfMsgBox, Yes
Run, "%A_AhkPath%" "%A_ScriptFullPath%" %InstanceNum% |
I herewith put my suggestion up for discussion, if you please.
Wolf _________________ Wolf
Schön wär's, wenn's schön wär! |
|
| Back to top |
|
 |
Laszlo
Joined: 14 Feb 2005 Posts: 4710 Location: Boulder, CO
|
Posted: Sat Apr 17, 2010 6:08 pm Post subject: |
|
|
| wolf_II wrote: | | my published code sufficiently generalized for the given task | My suggestions make the function generally useful. There is a simple closed form expression for the RMS of the integers from b to a, so there is no need for a loop, but you might need to compute the RMS of some measurement results. | Code: | a := 10, b := 1 ; RMS of integers from b to a
MsgBox % Sqrt((a*(1+a)*(1+a+a)-(b-1)*b*(b+b-1))/6/(a-b+1)) |
|
|
| 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
|