Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. Algorithms
  4. Good way to detect a useful portion of equivalent expressions

Good way to detect a useful portion of equivalent expressions

Scheduled Pinned Locked Moved Algorithms
testingbeta-testinghelplearning
35 Posts 9 Posters 12 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Matty22

    Eric's suggestion is excellent and is quite similar to how the fast primality test finds large primes quickly. If you test in enough bases the chance of an equation failing can be reduced to 1 in billions This means it falls under the meteor rule.. If the chance of a false positive is less than being hit by a meteor today..It's okay to use it So I think Eric is got an excellent idea.

    D Offline
    D Offline
    David1987
    wrote on last edited by
    #26

    I respectfully disagree. This will be part of an optimizing compiler. If the meteor decides to hit us after all then someone will have a nearly impossible to diagnose bug. There are also large classes of expressions for which Eriks heuristic method does not work at all regardless of how many iterations it uses or require such a large number of iterations that it becomes impractical. For example: (uint64)x / 3 and ((uint64)x * 0x5555556) >> 32 are the same for x in the interval 0..0x7FFFFFFF but not outside it. That gives a 0.5^k error probability where k is the number of iterations - which is fairly good, but if you combine a couple of those you can make the error probability arbitrarily high. And I'm not even "cheating" with comparisons here - a single comparison against a constant can make the error probability arbitrarily high. They could be special-cased of course, but can the expressions above be special cased?

    1 Reply Last reply
    0
    • D David1987

      Ok, maybe I now understand what you meant. However, it doesn't solve the problem, since false positives are not allowed at all.

      M Offline
      M Offline
      mbyamukama
      wrote on last edited by
      #27

      David, if these expressions are analytic expressions (defined and differentiable for all values of input), then a simple proof by induction will ALWAYS verify their equivalence. So you only have to try for a few values.

      BHM

      D 1 Reply Last reply
      0
      • M mbyamukama

        David, if these expressions are analytic expressions (defined and differentiable for all values of input), then a simple proof by induction will ALWAYS verify their equivalence. So you only have to try for a few values.

        BHM

        D Offline
        D Offline
        David1987
        wrote on last edited by
        #28

        Maybe so, but that's not what we have here, they are expressions with types such as int32 (see original post) and the class of expressions is not limited to something simple. For example, 1/x and 2/x differ only for x=1 and x=2 (edit: and -1 and -2), but there are 2^32 possibilities for x if it is an int32 and even more if it is an int64 - the chance that either 1 or 2 is tested isn't that big.

        modified on Sunday, April 3, 2011 8:58 AM

        1 Reply Last reply
        0
        • D David1987

          I have to detect whether two given expressions are equivalent (both compute the same value for all inputs). The inputs will all be of simple types, such as int32, so it is theoretically possible to try all inputs and see whether the result are always the same. That's too slow of course, so it will have to be done differently. False positives are not acceptable at all. False negatives are acceptable, but I need a useful amount of equivalences, so merely testing whether two expressions are equal does not solve the problem. I have tried applying many transformations to one of the expressions and see whether they are ever equal, but that doesn't work very well. The False Negatives rate is still far too high to be useful.

          A Offline
          A Offline
          Alex Shtof
          wrote on last edited by
          #29

          Can you define or characterize this "good portion"? For example, if you want to detect equivalence between bounded-degree polynomials (two computer programs or expressions such that their result is eventually a polynomial in the input), and you know the degree bound, it can be easily done by checking a small and finite number of inputs.

          D 1 Reply Last reply
          0
          • A Alex Shtof

            Can you define or characterize this "good portion"? For example, if you want to detect equivalence between bounded-degree polynomials (two computer programs or expressions such that their result is eventually a polynomial in the input), and you know the degree bound, it can be easily done by checking a small and finite number of inputs.

            D Offline
            D Offline
            David1987
            wrote on last edited by
            #30

            A good portion is hard to define :) It should find "significantly" more equivalences than merely comparing whether two expressions are equal. The class of expressions is not constrained in any way. I suppose I could first detect whether two expressions are both polynomials and then use a special comparison, but there are plenty of other types of expression so in order to find a "good portion" of equivalences I'd need something for them too.

            A 1 Reply Last reply
            0
            • D David1987

              A good portion is hard to define :) It should find "significantly" more equivalences than merely comparing whether two expressions are equal. The class of expressions is not constrained in any way. I suppose I could first detect whether two expressions are both polynomials and then use a special comparison, but there are plenty of other types of expression so in order to find a "good portion" of equivalences I'd need something for them too.

              A Offline
              A Offline
              Alex Shtof
              wrote on last edited by
              #31

              Well, I suppose that you know that for polynomials of degree n, you need to check for equality at (n+1) points. If all are equal, the polynomials are the same. Anyway, detecting polynomials is NOT easy. They can be computed in many ways, with for loops, while loops and other kinds of computations. It's not always visible by analyzing an expression that it is a polynomial. Alex.

              D 1 Reply Last reply
              0
              • A Alex Shtof

                Well, I suppose that you know that for polynomials of degree n, you need to check for equality at (n+1) points. If all are equal, the polynomials are the same. Anyway, detecting polynomials is NOT easy. They can be computed in many ways, with for loops, while loops and other kinds of computations. It's not always visible by analyzing an expression that it is a polynomial. Alex.

                D Offline
                D Offline
                David1987
                wrote on last edited by
                #32

                I could detect Some polynomials and match them that way I suppose. That may be more effort than it's worth.. well I guess I'll try it and see how much it helps.

                1 Reply Last reply
                0
                • D David1987

                  I have to detect whether two given expressions are equivalent (both compute the same value for all inputs). The inputs will all be of simple types, such as int32, so it is theoretically possible to try all inputs and see whether the result are always the same. That's too slow of course, so it will have to be done differently. False positives are not acceptable at all. False negatives are acceptable, but I need a useful amount of equivalences, so merely testing whether two expressions are equal does not solve the problem. I have tried applying many transformations to one of the expressions and see whether they are ever equal, but that doesn't work very well. The False Negatives rate is still far too high to be useful.

                  D Offline
                  D Offline
                  dasblinkenlight
                  wrote on last edited by
                  #33

                  This is an excellent question - the bad news is, the answer is rather complicated. You would need to implement a unification algorithm[^] to do it correctly. Things would not be as bad if you considered Prolog[^] as the language for your implementation, but something tells me that that would be out of the question ;) On the other hand, you can always embed Prolog to get unification for free. But no matter how you look at it, the learning curve is going to be steep. Good luck!

                  D 1 Reply Last reply
                  0
                  • D dasblinkenlight

                    This is an excellent question - the bad news is, the answer is rather complicated. You would need to implement a unification algorithm[^] to do it correctly. Things would not be as bad if you considered Prolog[^] as the language for your implementation, but something tells me that that would be out of the question ;) On the other hand, you can always embed Prolog to get unification for free. But no matter how you look at it, the learning curve is going to be steep. Good luck!

                    D Offline
                    D Offline
                    David1987
                    wrote on last edited by
                    #34

                    That page only seems to discuss logic formula's (unless I'm missing something?), does it also work for other formula's?

                    D 1 Reply Last reply
                    0
                    • D David1987

                      That page only seems to discuss logic formula's (unless I'm missing something?), does it also work for other formula's?

                      D Offline
                      D Offline
                      dasblinkenlight
                      wrote on last edited by
                      #35

                      Absolutely. Formally, you can represent A+B as add(A,B), and define an additional rule that add(A,B) :== add(B,A). In fact, since you are interested only in a yes/no answer (as opposed to variable assignments produced by unification), you don't even need to implement a complete unification: a simpler recursive matching algorithm would do. The only challenge in its implementation is matching ((A+B)+C) trees to (A+(B+C)). You can deal with it by flattening the list of operands, and trying all possible orderings on one of the sides. Don't forget to memoize[^] your matches, both positive and negative. Otherwise, your search will be too slow.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups