;;;;; ;;;;; ;;;;; Mathematics Expressions ;;;;; ;;;;; (define $math-expr (matcher {[,$val [] {[$tgt (if (eq? val tgt) {[]} {})]}] [$ [math-expr'] {[
{
}] [$tgt {(from-math-expr tgt)}]}] })) (define $math-expr' (matcher {[
[poly-expr poly-expr] {[
{[(to-math-expr' p1) (to-math-expr' p2)]}] [_ {}]}] [$ [something] {[$tgt {(to-math-expr' tgt)}]}] })) (define $poly-expr (matcher {[,$val [] {[$tgt (if (eq? val tgt) {[]} {})]}] [$ [poly-expr'] {[ {}] [
@{}}>> {}] [$tgt {(from-math-expr tgt)}]}] })) (define $poly-expr' (matcher {[ [(multiset term-expr)] {[ {(map to-math-expr' ts)}] [
@{}}>> {(map to-math-expr' ts)}] [_ {}]}] [$ [something] {[$tgt {(to-math-expr' tgt)}]}] })) (define $term-expr (matcher {[,$val [] {[$tgt (if (eq? val tgt) {[]} {})]}] [ [term-expr] {[$tgt (if (term? (/ tgt val)) {(/ tgt val)} {})]}] [$ [term-expr'] {[ {}] [
@{}}> @{}}>> {}] [$tgt {(from-math-expr tgt)}]}] })) (define $term-expr' (matcher {[ [integer (assoc-multiset symbol-expr)] {[ {[n (map 2#[(to-math-expr' %1) %2] xs)]}] [
@{}}> @{}}>> {[n (map 2#[(to-math-expr' %1) %2] xs)]}] [_ {}]}] [$ [something] {[$tgt {(to-math-expr' tgt)}]}] })) (define $symbol-expr (matcher {[,$val [] {[$tgt (if (eq? val tgt) {[]} {})]}] [$ [symbol-expr'] {[ {}] [
1] @{}}> @{}}> @{}}>> {}] [ {}] [
1] @{}}> @{}}> @{}}>> {}] [$tgt {(from-math-expr tgt)}]}] })) (define $symbol-expr' (matcher {[ [string] {[ {v}] [
1] @{}}> @{}}> @{}}>> {v}] [_ {}]}] [ [eq (list math-expr)] {[ [v mexprs]] [
1] @{}}> @{}}> @{}}>> {[v (map to-math-expr' mexprs)]}] [_ {}]}] [$ [something] {[$tgt {(to-math-expr' tgt)}]}] })) (define $symbol? (lambda [$mexpr] (match mexpr math-expr {[
>> >> > >>> #t] [_ #f]}))) (define $simple-term? (lambda [$mexpr] (match mexpr math-expr {[
>> >> > >>> #t] [,0 #t] [_ #f]}))) (define $term? (lambda [$mexpr] (match mexpr math-expr {[
>> > >>> #t] [,0 #t] [_ #f]}))) (define $polynomial? (lambda [$mexpr] (match mexpr math-expr {[
> >>> #t] [,0 #t] [_ #f]}))) (define $monomial? (lambda [$mexpr] (match mexpr math-expr {[
>> >>> #t] [,0 #t] [_ #f]}))) (define $from-monomial (lambda [$mexpr] (match mexpr math-expr {[
>> >>> [(/ a b) (/ (foldl *' 1 (map 2#(**' %1 %2) xs)) (foldl *' 1 (map 2#(**' %1 %2) ys)))]]}))) ;; ;; Map ;; (define $map-terms (lambda [$fn $mexpr] (match mexpr math-expr {[
> (/' (foldl +' 0 (map fn ts1)) (foldl +' 0 (map fn ts2)))]}))) (define $map-symbols (lambda [$fn $mexpr] (map-terms (lambda [$term] (match term term-expr {[ (*' a (foldl *' 1 (map 2#(match %1 symbol-expr {[ (**' (fn %1) %2)] [ (** (fn (capply g (map (map-symbols fn $) args))) %2) ]}) xs)))]})) mexpr))) (define $contain-symbol? (lambda [$x $mexpr] (any id (match mexpr math-expr {[
> (map (lambda [$term] (match term term-expr {[ (any id (map 2#(match %1 symbol-expr {[,x #t] [ (any id (map (contain-symbol? x $) args))] [_ #f]}) xs))]})) {@ts1 @ts2})]})))) ;;; ;;; Substitute ;;; (define $substitute (lambda [$ls $mexpr] (if (tensor? mexpr) (tensor-map (substitute ls $) mexpr) (match ls (list [symbol-expr math-expr]) {[ mexpr] [ (substitute rs (substitute' x a mexpr))]})))) (define $substitute' (lambda [$x $a $mexpr] (map-symbols (rewrite-symbol x a $) mexpr))) (define $rewrite-symbol (lambda [$x $a $sexpr] (match sexpr symbol-expr {[,x a] [_ sexpr]}))) ;;; ;;; Coefficient ;;; (define $coefficients (lambda [$f $x] (let {[$m (capply max {0 @(match-all f math-expr [
> _>> _> k])})]} (map (coefficient f x $) (between 0 m))))) (define $coefficient (lambda [$f $x $m] (if (eq? m 0) (/ (sum (match-all f math-expr [
$ts)> _>> _> (foldl *' a (map 2#(**' %1 %2) ts))])) (denominator f)) (coefficient' f x m)))) (define $coefficient' (lambda [$f $x $m] (/ (sum (match-all f math-expr [
> _>> _> (if (eq? m k) (foldl *' a (map 2#(**' %1 %2) ts)) 0)])) (denominator f))))