How to solve math equations using core.logic

I tried to enter text into the query in core.logic:

(run* [q] (== 0 (+ (* qq) (* 4 q) 4))) 

And the hint says:

 error: lvar cannot be cast to a number 

If I didn’t quite understand what logical programming is, are there any ways to solve this problem with core.logic?

+8
clojure clojure-core.logic
source share
3 answers

As far as I can find, core.logic cannot make algebra to solve this equation. It can do basic math, although the inputs to this math must be actual values ​​not LVar , because math functions cannot work with them:

 user> (run* [q] (fresh [x] (== x 1) (project [x] (== q (+ (* xx) 4))))) (5) 

works when x has a clear meaning and does not work when x does not:

 user> (run* [q] (fresh [x] (== xq) (project [x] (== q (+ (* xx) 4))))) ClassCastException clojure.core.logic.LVar cannot be cast to java.lang.Number 
+5
source share

You should read The Reasoned Schemer for ideas. In principle, the method of mathematics in a logical program is to create encodings based on letters, which the logical engine can grow as necessary to try. I don’t have a convenient book, but it encodes integers in the form of a list of bits, in some strange way I can’t completely remember: maybe (1) represents 0, (0) is illegal, and MSB is the last in the list?

In any case, this is a lot of work; David Nolen also recently introduced something about final domains in core.logic. I don’t know how this works, but I think they simplify the problem for you, letting you specify which types of numbers should be considered as a solution to your problem.

+5
source share

core.logic in it the current form is not developed as a solver of numerical equations - it is more suitable for solving logical and relational expressions.

Basically, you have two practical ways to solve mathematical equations:

  • Analytical solvers - solutions can be easily found for simple cases, for example. quadratic equations, like the ones you have above, but start to get more and more complicated pretty quickly, and then become impossible / unacceptable for many equations. This is a huge open research topic.
  • Numerical solvers - these methods are much more general and can be used for almost any equation. However, the results are not accurate, and the algorithms cannot find the correct solution (s) if the equation has “unpleasant” signs (gaps, odd gradients, complex sets of local minima, etc.).

Solvers of equations require special intelligence to understand the "rules" of mathematical equations, for example. how to define polynomial expressions (for analytical solutions) or how to evaluate a derivative (for numerical solutions).

Some links that may be of interest:

+2
source share

All Articles