Jump to content

Recommended Posts

Posted

Hmm yes! Well I think that's only true providing that the expression equals zero; which is true in this case, so just forget the 4 lines that I added, which are not a good solution anyway. However, you might want to consider scenarios where the user wishes to factorize without knowing the value of the expression. Perhaps that's something for a future release. Still, I'm glad that the above code was helpful to you. :)

It works on the values of a, b and c. And will only be valid where "ax^2 + bx + c = 0", I might work on it though. I think the majority of users will only recognize a quadratic in that form anyhow.

And it was very helpful. very very helpful. Thanks a lot!

Mat

Posted

It works on the values of a, b and c. And will only be valid where "ax^2 + bx + c = 0", I might work on it though. I think the majority of users will only recognize a quadratic in that form anyhow.

And it was very helpful. very very helpful. Thanks a lot!

Mat

You're welcome. I'm looking forward to seeing the new version. :)
Posted

Another attempted factorization method.

Similar to converting a decimal to a fraction.

eg.

Fraction

x = 1.2

x = 12/10

x = 6/5

Factor

if (x - 1.2) = 0 then quadratic equation, 10x^2 + 13x - 30 = 0

(x - 6/5) = 0

5 * (x - 6/5) = 5 * 0

(5x - 6) = 0

(Denominator X +/- numerator)

Local $x, $ans, $ans1, $factors, $A

$x = InputBox("Values of X", 'Example entry "1x^2 + 0x - 9"', "10x^2 + 13x - 30", "", 50, 50, 150, 100);
$ans = Execute(StringRegExpReplace($x, "(.*?)(x\^2)(.*?)(x)(.*)", "((-(${3})+(((${3})*(${3}))-(4*${1}*(${5})))^0.5)/(2*${1}))"))
$ans1 = Execute(StringRegExpReplace($x, "(.*?)(x\^2)(.*?)(x)(.*)", "((-(${3})-(((${3})*(${3}))-(4*${1}*(${5})))^0.5)/(2*${1}))"))

$factors = Fact($ans) & Fact($ans1)

; Where factors are (ax +b)(cx+d) and quadratic equation is Ax^2 + Bx + C = 0
;The following checks that a*c = A, when A <> 1.
$A = StringRegExpReplace($x, "(.*?)(x\^2)(.*?)(x)(.*)", "\1")
If Execute(StringRegExpReplace($factors, "\((.*)x.*\)\((.*)x.*\)", "\1 * \2")) <> $A And $A <> "1" Then $factors = "Could not factorize the expression!"

MsgBox(0, "Results", 'For "' & $x & ' = 0"' & @CRLF & @CRLF & "x = " & $ans & @CRLF & " or " & _
        @CRLF & "x = " & $ans1 & @CRLF & @CRLF & "Factors: " & $factors)


Func Fact($ans)
    Local $Num, $GCD
    If StringInStr($ans, ".") = 0 Then ; No decimal point
        Return StringFormat("(x %+d)", -$ans)
    Else
        $Num = StringLen(StringRegExpReplace($ans, "(.*\.)(.*)", "\2")) ; Number of deciminal places.
        $GCD = Abs(gcd($ans * 10 ^ $Num, 10 ^ $Num))
        Return StringFormat("(%.0fx %+.0f)", Abs(10 ^ $Num / $GCD), $ans * 10 ^ $Num / $GCD * - 1)
    EndIf
EndFunc ;==>Fact

;Greatest Common Denominator - Euclid's algorithm.
Func gcd($A, $m)
    Dim $p = 0, $r = 0, $b
    $p = Mod($A, $m)
    If $p = 0 Then
        $A = $r
    Else
        $r = $m
        Do
            $b = Mod($r, $p)
            $r = $p
            $p = $b
        Until $b = 0
    EndIf
    Return $r
EndFunc ;==>gcd
Posted

Another attempted factorization method.

Similar to converting a decimal to a fraction.

eg.

Fraction

x = 1.2

x = 12/10

x = 6/5

Factor

if (x - 1.2) = 0 then quadratic equation, 10x^2 + 13x - 30 = 0

(x - 6/5) = 0

5 * (x - 6/5) = 5 * 0

(5x - 6) = 0

(Denominator X +/- numerator)

Local $x, $ans, $ans1, $factors, $A

$x = InputBox("Values of X", 'Example entry "1x^2 + 0x - 9"', "10x^2 + 13x - 30", "", 50, 50, 150, 100);
$ans = Execute(StringRegExpReplace($x, "(.*?)(x\^2)(.*?)(x)(.*)", "((-(${3})+(((${3})*(${3}))-(4*${1}*(${5})))^0.5)/(2*${1}))"))
$ans1 = Execute(StringRegExpReplace($x, "(.*?)(x\^2)(.*?)(x)(.*)", "((-(${3})-(((${3})*(${3}))-(4*${1}*(${5})))^0.5)/(2*${1}))"))

$factors = Fact($ans) & Fact($ans1)

; Where factors are (ax +b)(cx+d) and quadratic equation is Ax^2 + Bx + C = 0
;The following checks that a*c = A, when A <> 1.
$A = StringRegExpReplace($x, "(.*?)(x\^2)(.*?)(x)(.*)", "\1")
If Execute(StringRegExpReplace($factors, "\((.*)x.*\)\((.*)x.*\)", "\1 * \2")) <> $A And $A <> "1" Then $factors = "Could not factorize the expression!"

MsgBox(0, "Results", 'For "' & $x & ' = 0"' & @CRLF & @CRLF & "x = " & $ans & @CRLF & " or " & _
        @CRLF & "x = " & $ans1 & @CRLF & @CRLF & "Factors: " & $factors)


Func Fact($ans)
    Local $Num, $GCD
    If StringInStr($ans, ".") = 0 Then ; No decimal point
        Return StringFormat("(x %+d)", -$ans)
    Else
        $Num = StringLen(StringRegExpReplace($ans, "(.*\.)(.*)", "\2")) ; Number of deciminal places.
        $GCD = Abs(gcd($ans * 10 ^ $Num, 10 ^ $Num))
        Return StringFormat("(%.0fx %+.0f)", Abs(10 ^ $Num / $GCD), $ans * 10 ^ $Num / $GCD * - 1)
    EndIf
EndFunc ;==>Fact

;Greatest Common Denominator - Euclid's algorithm.
Func gcd($A, $m)
    Dim $p = 0, $r = 0, $b
    $p = Mod($A, $m)
    If $p = 0 Then
        $A = $r
    Else
        $r = $m
        Do
            $b = Mod($r, $p)
            $r = $p
            $p = $b
        Until $b = 0
    EndIf
    Return $r
EndFunc ;==>gcd

It's nice to see different approaches to this. However I encountered problems the first time I ran your script. It would not factorize the following expression:

51x^2 - 193x + 106 = (3x - 2)(17x - 53)

I doubt that trying to factorize an expression by manipulating decimal fractions can ever be a reliable approach. However I like the concept. :)

Posted

That reminded me... I need to fix a few things. Just noticed them ;)

Next version will include more options. I have been looking at wikipedia, and these look doable as alternate solving methods:

* By Lagrange resolvents

* Alternative formula

* Possibly "Newtons method" when I work out what wikipedia is on about :)

and for additional info:

* Min point

* Generalization

If anyone has anything to add that'll be good!

I'm also looking at possible writing some simple graphing functions, and add that as well. I don't have a lot of time at the moment, so It might not be for a while... and I am also looking at rewriting it in C# (I can add richedits and toolbars without it being a pain etc).

If I do continue to do it in au3 then I will definitely try adding in solving to more decimal places (the BigNum udf is awesome!)

Mat

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...