Menu

#3669 psubst simplifies with simp:false

None
open
nobody
psubst (1)
5
2024-06-13
2020-11-01
No
block([simp:false],print(psubst([a=1,b=1],a+b)));

prints 2, while

block([simp:false],print(sublis([a=1,b=1],a+b)));

prints 1+1, as it should.

Here is a quick patch to fix this:

(defmfun $psubstitute (old new &optional (expr nil three-arg?))
  (cond (three-arg? (maxima-substitute old new expr))
        (t
         (let ((l old) (z new))
           (cond ((and ($listp l)
                       ($listp (cadr l))
                       (null (cddr l)))
                  ;; A nested list.
                  ($psubstitute (cadr l) z))
                 ((and ($listp l)
                       (eq (caar (cadr l)) 'mequal)
                       (null (cddr l)))
                  ;; A list with one equation.
                  ($psubstitute (cadr l) z))
                 ((notloreq l) (improper-arg-err l '$psubstitute))
                 ((eq (caar l) 'mequal)
                  ;; Do a substitution for one equation.
                  (maxima-substitute (caddr l) (cadr l) z))
                 (t
                  ;; We have a list of equations. We do parallel substitution.
                  (let (gensymbol genlist eqn (osimp $simp) ($simp nil))  ;; <<<
                    ;; At first substitute a gensym for the expressions of
                    ;; the left hand side of the equations.
                    (do ((l (cdr l) (cdr l)))
                        ((null l) z)
                      (setq eqn (car l))
                      (when (not (eq 'mequal (caar eqn)))
                        (improper-arg-err old '$substitute))
                      (setq gensymbol (gensym))
                      ;; Store the gensym and the new expression into a list.
                      (push (cons gensymbol (caddr eqn)) genlist)
                      ;; Substitute a gensym for the old expression.
                      (setq z (maxima-substitute gensymbol (cadr eqn) z)))
                      ;; Substitute the new expressions for the gensyms.
                      (do ((l genlist (cdr l)))
                          ((null l)
                           ;; Resimplify the result.
                           (let (($simp osimp)) (resimplify z)))   ;; <<<
                        (setq z (maxima-substitute (cdar l) (caar l) z))))))))))

Discussion

  • Robert Dodier

    Robert Dodier - 2020-12-12

    I've noticed that psubst calls simplification directly, which subst does not. I think that's a bug in psubst; I think psubst should be consistent with other functions, which do not call simplification directly.

    I probably noticed that in some pattern matching stuff. I think the main thing is that it's more nearly possible to predict what's going to happen to the extent that functions work consistently.

     
  • Robert Dodier

    Robert Dodier - 2024-06-13
    • labels: --> psubst
     
  • Robert Dodier

    Robert Dodier - 2024-06-13

    I've bumped into this before. psubst should honor the prevailing value of the simp flag -- I guess that is what the patched version is doing above.

     

Log in to post a comment.