# Factorize Boolean expressions

## Description

This function finds all combinations of common factors in a Boolean expression written in SOP - Sum Of Products form.

## Usage

    factorize(expression, snames = "", noflevels, use.tilde = FALSE, pos = FALSE, ...)

## Arguments

`expression` |  |  |  | A string containing the SOP expression, or an object of class `"qca"`.
`snames` |  |  |  | A string containing the sets' names, separated by commas.
`noflevels` |  |  |  | Numerical vector containing the number of levels for each set.
`use.tilde` |  |  |  | Logical, use a tilde to negate the sets.
`pos` |  |  |  | Logical, if possible factorize using product(s) of sums.
`...` |  |  |  | Other arguments (mainly for backwards compatibility).

## Details

Factorization is a process of finding common factors in a Boolean expression, written in a SOP - sum of products (or DNF - disjunctive normal form). Whenever possible, the factorization can also be performed in a POS - product of sums form.

Conjunctions should preferably be indicated with a star `*` sign, but this is not necessary when conditions have single letters or when the expression is expressed in multi-value notation.

The number of levels in `noflevels` is needed only when negating multivalue conditions, and it should complement the `snames` argument.

If `input` is an object of class `"qca"` (the result of the `minimize()` function), a factorization is performed for each of the minimized solutions.

The argument `use.tilde` is automatically activated if the input contains a tilde to negate conditions.

## Value

A list with two components:

initial |  | The input expression.
factored |  | All possible factorizations of the input expression.

## References

Ragin, C.C. (1987) The Comparative Method. Moving beyond qualitative and quantitative strategies, Berkeley: University of California Press

## Examples

    # typical example with redundant conditions
    factorize("AbcD + AbCd + AbCD + ABCd")

    M1: AbcD + AbCd + AbCD + ABCd

      F1: A(bD + bC + Cd)
      F2: ACd + bA(D + C)
      F3: AbD + CA(b + d)
      F4: Ab(D + C) + ACd
      F5: AbD + AC(b + d)

    # results presented in alphabetical order
    factorize("one*TWO*four + one*THREE + THREE*four")

    M1: one*TWO*four + one*THREE + THREE*four

      F1: four*(THREE + one*TWO) + one*THREE
      F2: four*THREE + one*(THREE + four*TWO)
      F3: four*one*TWO + THREE*(one + four)

    # to preserve a certain order of the set names
    factorize("one*TWO*four + one*THREE + THREE*four",
              snames = "ONE, TWO, THREE, FOUR")

    M1: one*TWO*four + one*THREE + THREE*four

      F1: one*(THREE + TWO*four) + THREE*four
      F2: one*TWO*four + THREE*(one + four)
      F3: four*(THREE + one*TWO) + one*THREE

    factorize("~ONE*TWO*~FOUR + ~ONE*THREE + THREE*~FOUR",
              snames = "ONE, TWO, THREE, FOUR")

    M1: ~ONE*TWO*~FOUR + ~ONE*THREE + THREE*~FOUR

      F1: ~ONE*(THREE + TWO*~FOUR) + THREE*~FOUR
      F2: ~ONE*TWO*~FOUR + THREE*(~ONE + ~FOUR)
      F3: ~FOUR*(THREE + ~ONE*TWO) + ~ONE*THREE

    # using pos - products of sums
    factorize("ac + aD + bc + bD", pos = TRUE)

    M1: ac + aD + bc + bD

      F1: (a + b)(c + D)

    # using an object of class "qca" produced with minimize()
    data(CVF)
    pCVF <- minimize(CVF, outcome = "PROTEST", incl.cut = 0.8,
                     include = "?", use.letters = TRUE)

    factorize(pCVF)

    M1: e + aBD + ABC + ACD

      F1: AC(B + D) + e + aBD
      F2: B(aD + AC) + e + ACD
      F3: D(aB + AC) + e + ABC

    M2: e + aBD + ABd + ACD

      F1: A(Bd + CD) + e + aBD
      F2: B(aD + Ad) + e + ACD
      F3: D(aB + AC) + e + ABd

    M3: e + ABC + ACD + BCD

      F1: AC(B + D) + e + BCD
      F2: BC(A + D) + e + ACD
      F3: C(AB + AD + BD) + e
      F4: CD(A + B) + e + ABC

    M4: e + ABd + ACD + BCD

      F1: A(Bd + CD) + e + BCD
      F2: B(Ad + CD) + e + ACD
      F3: CD(A + B) + e + ABd

    # using an object of class "deMorgan" produced with negate()
    factorize(negate(pCVF))

    M1: AcE + abE + bcE + bdE + adE + cdE

      F01: E(Ac + ab + bc + bd + ad + cd)
      F02: aE(b + d) + E(Ac + bc + bd + cd)
      F03: aE(b + d) + bE(c + d) + cE(A + d)
      F04: aE(b + d) + bdE + cE(A + b + d)
      F05: aE(b + d) + cE(A + b) + dE(b + c)
      F06: bE(a + c + d) + E(Ac + ad + cd)
      F07: adE + bE(a + c + d) + cE(A + d)
      F08: AcE + bE(a + c + d) + dE(a + c)
      F09: cE(A + b + d) + E(ab + bd + ad)
      F10: adE + bE(a + d) + cE(A + b + d)
      F11: abE + cE(A + b + d) + dE(b + a)
      F12: dE(b + a + c) + E(Ac + ab + bc)
      F13: AcE + bE(a + c) + dE(b + a + c)
      F14: abE + cE(A + b) + dE(b + a + c)

    M2: abE + bcE + bdE + adE + AcDE

      F01: E(ab + bc + bd + ad + AcD)
      F02: aE(b + d) + E(bc + bd + AcD)
      F03: AcDE + aE(b + d) + bE(c + d)
      F04: aE(b + d) + bdE + cE(b + AD)
      F05: bE(a + c + d) + E(ad + AcD)
      F06: cE(b + AD) + E(ab + bd + ad)
      F07: adE + bE(a + d) + cE(b + AD)
      F08: abE + cE(b + AD) + dE(b + a)
      F09: dE(b + a) + E(ab + bc + AcD)
      F10: AcDE + bE(a + c) + dE(b + a)

    M3: cE + abE + adE + bdE

      F1: E(c + ab + ad + bd)
      F2: aE(b + d) + E(c + bd)
      F3: bE(a + d) + E(c + ad)
      F4: dE(a + b) + E(c + ab)

    M4: abE + acE + adE + bcE + bdE + cDE

      F01: E(ab + ac + ad + bc + bd + cD)
      F02: aE(b + c + d) + E(bc + bd + cD)
      F03: aE(b + c + d) + bE(c + d) + cDE
      F04: aE(b + c + d) + bdE + cE(b + D)
      F05: bE(a + c + d) + E(ac + ad + cD)
      F06: aE(c + d) + bE(a + c + d) + cDE
      F07: adE + bE(a + c + d) + cE(a + D)
      F08: cE(a + b + D) + E(ab + ad + bd)
      F09: aE(b + d) + bdE + cE(a + b + D)
      F10: adE + bE(a + d) + cE(a + b + D)
      F11: abE + cE(a + b + D) + dE(a + b)
      F12: dE(a + b) + E(ab + ac + bc + cD)
      F13: aE(b + c) + cE(b + D) + dE(a + b)
      F14: bE(a + c) + cE(a + D) + dE(a + b)

## Author

Adrian Dusa

## See also

`translate`