AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

AERS : Algebraic Equation ReGex Solver

 
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
k3ph



Joined: 20 Jul 2006
Posts: 174

PostPosted: Thu Aug 21, 2008 1:20 pm    Post subject: AERS : Algebraic Equation ReGex Solver Reply with quote

[ 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 Mon Sep 29, 2008 5:32 pm; edited 3 times in total
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu Aug 21, 2008 3:24 pm    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
k3ph



Joined: 20 Jul 2006
Posts: 174

PostPosted: Thu Aug 21, 2008 5:59 pm    Post subject: Reply with quote

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 Thu Aug 21, 2008 6:13 pm; edited 1 time in total
Back to top
View user's profile Send private message
Jex



Joined: 01 Aug 2008
Posts: 61

PostPosted: Thu Aug 21, 2008 6:12 pm    Post subject: Reply with quote

You don't like math homework.

Are you planning to add curves?
_________________
Woot.

Please read forum etiquette
Back to top
View user's profile Send private message
k3ph



Joined: 20 Jul 2006
Posts: 174

PostPosted: Thu Aug 21, 2008 6:16 pm    Post subject: Reply with quote

(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 Thu Aug 21, 2008 6:33 pm; edited 1 time in total
Back to top
View user's profile Send private message
Laszlo



Joined: 14 Feb 2005
Posts: 4078
Location: Pittsburgh

PostPosted: Thu Aug 21, 2008 6:30 pm    Post subject: Reply with quote

Jex wrote:
Are you planning to add curves?
They are in the popup calculator.
Back to top
View user's profile Send private message
k3ph



Joined: 20 Jul 2006
Posts: 174

PostPosted: Mon Sep 29, 2008 5:37 pm    Post subject: Reply with quote

I've updated this script to support N number of sums and put an little explanation of it. Enjoy!
_________________
                                  [ profile | ahk.net | ahk.talk ]
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group