AutoHotkey Community

It is currently May 27th, 2012, 1:22 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 6 posts ] 
Author Message
PostPosted: August 21st, 2008, 2:20 pm 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
[ description ]
This script returns the maximum multipliers incognitas and supports N numbers of variables.
Examples: a * x = s1 ... a * x + b * y = s2 ... a * x + b * y + c * z = s3 ... N number of sum of multipliers.
It's known that big Z values makes the perfomance decrease but the main objective is to show a PoC of the transforming of a Finite State Automata through REgex

[ explanation ]
Transforming FSA into RE- by Model Engineering College

The idea behind the construction is the following: (assuming states are enumerated) we will give an algorithm by which we can calculate a regular expression Ti,j(k) which describes all the strings which can take us from state i in the FSA to state j via states 1, 2, ... k. The language described by an FSA with n states, and where the start state is named 1 is then the union of all strings which take us from state 1 to a final state f through any of the states: the sum of all regular expressions T1,f(n) where f ranges over the final states.

The algorithm to calculate Ti,j(k) is stated inductively over variable k:
Base case: k=0 . We subdivide this into two cases, when i is the same as j and when they are not:

· Ti,i(0) =1+a+...+z where a to z are the labels on transition arcs going from state i to itself. Note that if no such arcs exist, the expression becomes simply 1.

· Ti,j(0) =a+...+z where a to z are the labels on transition arcs going from state i to state j (which are not equal). Note that if no such arcs exist, the expression becomes simply 0.

Inductive case: When the third parameter (k) is larger than 0, we use the following equation to calculate the desired result:

Ti,j(k+1) = Ti,j(k) + Ti,k+1(k)(Tk+1,k+1(k))*Tk+1,j(k)

_________
[ examples ]

example 1:
Code:
; this example: 7 * answer1 + 9 * answer2 = 53
aers =
(
7  9  53
)

#include aers.ahk


example 2:
Code:
; this example: 11 * answer1 + 3 * answer2 + 7 * answer3 = 16435
aers =
(
11  3  7  16435
)

#include aers.ahk


aers.ahk
Code:
/*
   AERS : Algebraic Equation ReGex Solver
   by k3ph - Licensed under BSD [ http://autohotkey.net/~k3ph/license.txt ]
   
   version: prototype 01
*/   

loop, parse, aers, %A_Space%`n`;`t`,`r[](){}<> ; [vxe]'s parse
{
   if (A_Loopfield is NUMBER)
   {
      n++
      n%n% = %A_LoopField%
      if (n = 1)
         p_b := "^(.*)\1{" . n1 - 1
      if (n = 2)
         p_m := "}(.*)\" . n . "{" A_LoopField - 1
      if (n > 2)
         p_m := p_m . "}(.*)\" . n . "{" A_LoopField - 1
   }
}
result := n%n% ; get the result from last root
roots := n - 1
last_n := result - 1
loop % result + 1 ; convert answer to match
{
   if a_index > 1
      result := result . chr(48)
   else
      result := ""
}
stringlen, last_nl, last_n
last_nl := 8 + last_nl
StringTrimRight, p_m, p_m, %last_nl% ; remove result from regex
p_e := "}$"
needle := p_b . p_m . p_e ; make needle
answers :=  RegExMatch(result, needle, match) ; the regex
if (aers != "") ; check if the script was feeded
   loop %roots% ; check how many answers
   {
      if (answer = "") ; if null, answer is zero
         answer = 0
      answer := StrLen(match%A_Index%)
      msgbox %answer% ; display the answers. if there are multiple possible solutions, the one with the highest value of x will be returned since x is handled earlier in the regex.
   }


[ keywords ]
algebra, regex, aers

_________________
                                  [ profile | ahk.net | ahk.talk ]


Last edited by k3ph on September 29th, 2008, 6:32 pm, edited 3 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 21st, 2008, 4:24 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Interesting idea. I would explicitly write * for multiplication (or at least a mandatory space, because Ax, By, or even 7x are valid AHK variable names, and there seem to be no reason to restrict the system of equations to handle only single letters for the parameters.

For larger systems of equations one can consider writing the coefficients in an array, without the variable names. These array can be conveniently initialized with StringSplit.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 21st, 2008, 6:59 pm 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
Thanks for the reply, I appreciate it. As guessed, I plan to design a finite-dimensional linear solver with it, and an array-like notation would be very useful for it, thanks again for sharing your ideas.

I had so much fun thinking in the possibilities with this PoC. If you do have more, please share with me. I hope more people join it ;) yay.

_________________
                                  [ profile | ahk.net | ahk.talk ]


Last edited by k3ph on August 21st, 2008, 7:13 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 21st, 2008, 7:12 pm 
Offline

Joined: August 2nd, 2008, 12:31 am
Posts: 101
You don't like math homework.

Are you planning to add curves?

_________________
Woot.

Please read forum etiquette


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 21st, 2008, 7:16 pm 
Offline

Joined: July 21st, 2006, 12:26 am
Posts: 223
(lol)

Maybe some graphical functions will be added. But actually I can't see how to get better than Laszlo's Popup Calculator and I'm a little bit concerned about speed perfomance. Testing this version out with a Z with 5 digits length took me some long seconds to calculate the answers.

Does someone have any ideas of accelerate it? I guess I should look for a way to replace the loop...

Also I've take a look into [VxE]'s Laplace Expansion to find Matrix Determinant and I'm plan to communicate with it.

_________________
                                  [ profile | ahk.net | ahk.talk ]


Last edited by k3ph on August 21st, 2008, 7:33 pm, edited 1 time in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 21st, 2008, 7:30 pm 
Offline

Joined: February 14th, 2005, 4:05 pm
Posts: 4710
Location: Boulder, CO
Jex wrote:
Are you planning to add curves?
They are in the popup calculator.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Bon and 13 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