Deprecated: Creation of dynamic property wpdb::$categories is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$post2cat is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/wp-db.php on line 668

Deprecated: Creation of dynamic property wpdb::$link2cat is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/wp-db.php on line 668

Deprecated: Return type of WP_Theme::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-theme.php on line 554

Deprecated: Return type of WP_Theme::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-theme.php on line 595

Deprecated: Return type of WP_Theme::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-theme.php on line 535

Deprecated: Return type of WP_Theme::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-theme.php on line 544

Deprecated: Using ${var} in strings is deprecated, use {$var} instead in /usr/home/bugyo/public_html/b-log/wp-includes/comment-template.php on line 1739

Deprecated: Return type of WP_REST_Request::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/rest-api/class-wp-rest-request.php on line 960

Deprecated: Return type of WP_REST_Request::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/rest-api/class-wp-rest-request.php on line 980

Deprecated: Return type of WP_REST_Request::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/rest-api/class-wp-rest-request.php on line 992

Deprecated: Return type of WP_REST_Request::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/rest-api/class-wp-rest-request.php on line 1003

Deprecated: Return type of WP_Block_List::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 151

Deprecated: Return type of WP_Block_List::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 175

Deprecated: Return type of WP_Block_List::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 164

Deprecated: Return type of WP_Block_List::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 186

Deprecated: Return type of WP_Block_List::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 138

Deprecated: Return type of WP_Block_List::offsetExists($index) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 75

Deprecated: Return type of WP_Block_List::offsetGet($index) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 89

Deprecated: Return type of WP_Block_List::offsetSet($index, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 110

Deprecated: Return type of WP_Block_List::offsetUnset($index) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 127

Deprecated: Return type of WP_Block_List::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-block-list.php on line 199

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198

Deprecated: Creation of dynamic property WP_Term::$object_id is deprecated in /usr/home/bugyo/public_html/b-log/wp-includes/class-wp-term.php on line 198
[数式処理][Scheme] 算術式を簡単にする – B-LOG
B-LOG

[数式処理][Scheme] 算術式を簡単にする

微分などで複雑になった算術式を簡素化するには次のようにする。

(define (simple exp)
  (define (flat lst op)
    (append-map! (lambda (x)
                   (if (and (pair? x) (eq? (car x) op))
                       (flat (cdr x) op)
                       (list x)))
                 lst))
  
  (define (constant-fold args init op)
    (define (cf args num acc)
      (cond ((null? args)
             (values acc num))
            ((number? (car args))
             (cf (cdr args) (op num (car args)) acc))
            (else
             (cf (cdr args) num (append! acc (list (car args)))))))
    (cf args init '() ))
  
  (define (simple-+ args)
    (set! args (map simple args))
    (set! args (flat args '+))
    (receive (exps num) (constant-fold args 0 +)
     (cond ((null? exps) num)
           (else `(+ ,@args num)))))
  
  (define (simple-* args)
    (call/cc
     (lambda (c)
       (set! args (map simple args))
       (set! args (flat args '*))
       (map (lambda (x)
              (when (and (number? x) (= x 0))
                    (c 0)))
            args)
       (set! args (constant-fold args 1 *))
       (set! args (remove (lambda (x)
                            (and (number? x)(= x 1)))
                          args))
       (if (= 1 (length args))
           (car args)
           `(* ,@args)))))

  (define (simple-expt base ex)
    (set! base (simple base))
    (set! ex (simple ex))
    (cond ((or (and (number? base) (= base 1))
               (and (number? ex) (= ex 0)))
           1)
          ((or (and (number? base) (= base 0))
               (and (number? ex) (= ex 1)))
           base)
          (else
           `(expt ,base ,ex))))

  (define (simple-- arg)
    (cond ((number? arg) (- arg))
          ((and (pair? arg)
                (eq? (car arg) '+))
           (simple-+ (map (lambda (x) (list '- x))
                          (cdr arg))))
          ((and (pair? arg)
                (eq? (car arg) '*))
           (simple-* (map (lambda (x) (list '- x))
                          (cdr arg))))
          (else
           `(- ,arg))))
  
  (cond ((not (pair? exp)) exp)
        ((eq? (car exp) '-)
         (simple-- (cadr exp)))
        ((eq? (car exp) '+)
         (simple-+ (cdr exp)))
        ((eq? (car exp) '*)
         (simple-* (cdr exp)))
        ((eq? (car exp) 'expt)
         (simple-expt (cadr exp) (caddr exp)))
        (else
         `(,(car exp) ,@(map simple (cdr exp))))))
> (simple (deriv '(expt (* (sin x) (cos x)) 1/2) 'x))
=> (* (expt (* (sin x) (cos x)) -1/2) (+ (* (cos x) (cos x)) (* (sin x) (- (sin x)))) 1/2)

`-‘ 演算子は単項演算子としてのみ使える。