wilbert wrote:fvillanova wrote:Within the calculation routines that I use in my programs are statistical distribution calculations where I need to make millions simulation with factors lexicographical, I do not know if you have seen this kind of calculation.
No, haven't seen before
Haven't got a clue what is going on.
Is it some sort of sorting related thing ?
In the end everything can be translated to asm but the question is if it's worth it.
In this case the best thing you can do is try to speed up the Factorial and Combinations procedures to start with.
Is there a limit to the values passed to the Factorial and Combinations procedures ?
If there is and it isn't very big, you could use a lookup table for this. That would already result in a big speed improvement.
I will explain you what is lexicographic order.
If you take 5 numbers (01 to 05) as an example
How many combinations of only two numbers are possíves with these five.
1-01 02
2-01 03
3-01 04
4-01 05
5-02 03
6-02 04
7-02 05
8-03 04
9-03 05
10-04 05
Answer is = 10 different possible combinations of 5 2-2
My procedure calculates the number of possible combinations, see:
Result=Combinations(5,2) "Result is 10"
Now, if I want to know what lexicographic order of "03 04"
Result=Lexicographic_Order("03 04",5,2) "Result is 8"
And finally, I want to know what are the elements of the combination 4?
aux=Elements_Return(5,2,4)
aux is "01 05" in the above example.
Yes, I can put all factor previously in a vector, but this does not greatly accelerates
the process, the more time-consuming processing is in Lexicographic_Order and Elements_Return procedures
see the change below:
Code: Select all
DisableDebugger
Global.i i,Result, time
Global.s aux
Global Dim da.s(99)
Global Dim FacData.d(100)
Procedure.d Factorial(N.i)
Protected E.d
E=1:For S=2 To N:E*S:Next
ProcedureReturn E
EndProcedure
Procedure.d Combinations(n.i,k.i)
Protected calc.d
calc=FacData(n)/((FacData(n-k))*FacData(k))
ProcedureReturn calc
EndProcedure
Procedure.i Lexicographic_Order(cval.s,nval.i,kval.i)
Protected.i lval, p1val, ival, jval
Protected.d rval
Protected Dim c.i(kval), Dim numv.i(kval): c(0)=0
For ival=0 To kval-1: numv(ival+1)=Val(PeekS(@cval + ival * 3, 2,#PB_Ascii)): Next
p1val=kval-1
For ival=1 To p1val: c(ival)=0
If ival<>1: c(ival)=c(ival-1): EndIf
Repeat
c(ival)+1: rval=Combinations(nval-c(ival),kval-ival)
lval=lval+rval
Until c(ival)>=numv(ival)
lval=lval-rval
Next
lval=lval+numv(kval)-numv(p1val)
ProcedureReturn lval
EndProcedure
Procedure.s Elements_Return(nval.i,pval.i,lval.i)
Protected rval.d, p1val.d, kval.d, ival.i, Dim c.i(pval), cval.s, auxval.s
p1val=pval-1: c(1)=0
For ival=1 To p1val: If ival<>1: c(ival)=c(ival-1): EndIf
Repeat
c(ival)+1: rval=Combinations(nval-c(ival),pval-ival)
kval=kval+rval
Until kval>=lval
kval=kval-rval: cval=cval+auxval+da(c(ival)): auxval=" "
Next
c(pval)=c(pval-1)+lval-kval: cval=cval+auxval+da(c(pval))
ProcedureReturn cval
EndProcedure
For i=0 To 99:If i<10:da(i)="0"+Str(i):Else:da(i)=Str(i):EndIf: Next
For i=0 To 100: FacData(i)=Factorial(i): Next
time = ElapsedMilliseconds()
For i=1 To 2000000
aux=Elements_Return(15,8,6387) ; constructs the Elements of lexicographic order = 6387
Result=Lexicographic_Order("05 08 09 10 12 13 14 15",15,8) ; calculate lexicographic order from "05 08 09 10 12 13 14 15"
Next
time = ElapsedMilliseconds()-time
MessageRequester("Lexicographic Case:","Processed in "+Str(time)+" milliseconds"+Chr(13)+aux+Chr(13)+Str(Result),#PB_MessageRequester_Ok): End
I modified the procedure Combinations() to:
"calc=FacData(n)/((FacData(n-k))*FacData(k))"
I've done this in some of my routines in other programs,
but the idea is to convert the two main procedures for asm.
To understand where these lexicographic routines are used I'll give you an example:
When an insurance company accepts an insurance policy it already
know how many risks are involved and the cost of each of these risks,
the amount of risk involved is 'n'
Each risk has an average cost,then I can do simulation 2 or 3 risk at a time to determine what
the costs of damages that may occur.
"Combinations(n,2)+Combinations(n,3)"
Using the lexicographic functions I can get 2 or 3 risks involved and check the cost of them
and find out what risks are involved in certain compensation expenses.
This is a simple example, just for you understand where these routines are used
in statistics simulations.
thanks
Fernando