Skip to content

Factoring of user-provided expressions/intermediates #561

Description

@Krzmbrzl

Sometimes it would be handy to be able to tell SeQuant to factor out a given expression A out of an expression B. So for instance if we have B = D F + DG + E F + E G, we should be able to ask SeQuant to factor out A = (D + E) which should lead to one of B = (D + E) (F + G) = (D + E) F + (D + E) G (preferably the first, of course).

This can come in handy if the user knows (or thinks to know) of a better factorization than whatever SeQuant might determine via its internal optimization routines. Alternatively, there might be other (external) considerations beyond minimal FLOP count (or whatever the exact metric SeQuant happens to use for optimization) that would make a user prefer using a specific way of factorizing.

Operation of this feature should have different modes:

  • Failure if the given expression can't be factored out without adding cancellation terms. That is, attempting to factor A out of A B + A C + D would fail as not every term contains A.
  • Factor out the entire expression even if that implies adding cancellation terms. Taking the example from above, the factorization would lead to A * (B + C + D) - (A - 1) D. If the cancellation terms are not well-formed, an error should be emitted.
  • Split the to-be-factorized expression into terms that can be cleanly/directly factored and those that don't contain the provided factor. Then only perform the factorization on the group that allows for it. Here, our example would be split into the factored group A (B + C) and D

In all cases there should be a possibility to steer whether factoring out an expression causes appropriate rewriting and parenthization or whether the factor shall be removed from the expression. That is, factoring A from A B + A C can lead either to A (B + C) or to B + C. The easiest way to achieve this is likely to always eliminate the factor from the expression. The caller asking for the factoring by means of being able to call the function has access to A and can therefore choose to multiply it with the processed equations B + C to arrive at A (B + C). However, this would imply that we either don't have a mode that adds cancellation terms (for cases that don't factorize cleanly, the mode separating the processed expression into factorizable and non-factorizable groups should be able to step in as a replacement) or we allow for cancellation terms that imply multiplicative inverses, which might be unfeasible (for tensors we don't support it, for constants it's trivial unless they are zero (for which an error could be emitted) and for variables this hinges on the assumption that they can't be zero. Same applies for composite expressions).

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions