logic-solver icon indicating copy to clipboard operation
logic-solver copied to clipboard

Multiplication

Open vielhuber opened this issue 5 years ago • 4 comments

Hello!

This is an awesome project.

I wonder if it is possible to realize integer multiplication of two variable Bits.

At the moment there is only the sum-method available.

Can I somehow execute the sum k-times, where k is another variable (of Logic.variableBits)?

Thanks for your answer.

vielhuber avatar Sep 27 '19 23:09 vielhuber

This is a way to roll your own (quadratic multiplication) using the API:

function mul(xs, ys) {
  const bs = []
  const ws = []
  xs.bits.forEach(
    (x, i) =>
      ys.bits.forEach(
        (y, j) => {
          bs.push(Logic.and(x, y))
          ws.push(Math.pow(2, i+j))
        }))
  return Logic.weightedSum(bs, ws)
}

// a little test:
const s = new Logic.Solver()
const x = Logic.variableBits('x', 3)
const y = Logic.variableBits('y', 3)
const xy = Logic.variableBits('xy', 6)
s.require(Logic.equalBits(xy, mul(x, y)))
let sol
while (sol = s.solve()) {
  s.forbid(sol.getFormula())
  console.log({
    x: sol.evaluate(x),
    y: sol.evaluate(y),
    xy: sol.evaluate(xy),
  })
}

danr avatar Oct 10 '19 11:10 danr

Cool stuff, thank you. This should deserve a place in README.md.

vielhuber avatar Oct 10 '19 13:10 vielhuber

It seems my program is now to heavy on memory.

Currently I get:

MINISAT-out: Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 67108864, (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.

Is there any way to increase the TOTAL_MEMORY option?

vielhuber avatar Oct 28 '19 11:10 vielhuber

I have sample problem MINISAT-out: Cannot enlarge memory arrays. Either (1) compile with -s TOTAL_MEMORY=X with X higher than the current value 67108864, (2) compile with ALLOW_MEMORY_GROWTH which adjusts the size at runtime but prevents some optimizations, or (3) set Module.TOTAL_MEMORY before the program runs.. Is there any way to increase the TOTAL_MEMORY option?

tranthelam22082003 avatar Nov 12 '22 09:11 tranthelam22082003