comparison lisp/calc/calc-units.el @ 112453:06719a229a46 default tip

* calc/calc.el (calc-default-power-reference-level) (calc-default-field-reference-level): New variables. * calc/calc-units.el (math-standard-units): Add dB and Np. (math-logunits): New variable. (math-extract-logunits, math-logcombine, calcFunc-luplus) (calcFunc-luminus, calc-luplus, calc-luminus, math-logunit-level) (calcFunc-fieldlevel, calcFunc-powerlevel, calc-level): New functions. (math-find-base-units-rec): Add entry for ln(10). * calc/calc-help.el (calc-u-prefix-help): Add logarithmic help. (calc-ul-prefix-help): New function. * calc/calc-ext.el (calc-init-extensions): Autoload new units functions. Add keybindings for new units functions.
author Jay Belanger <jay.p.belanger@gmail.com>
date Sun, 23 Jan 2011 23:08:04 -0600
parents 61f7601898b1
children
comparison
equal deleted inserted replaced
112452:b8a360ee54f1 112453:06719a229a46
294 ( mup "1.410606662*10^(-26) J/T" "Proton magnetic moment" nil 294 ( mup "1.410606662*10^(-26) J/T" "Proton magnetic moment" nil
295 "1.410606662 10^-26 J/T (*)") 295 "1.410606662 10^-26 J/T (*)")
296 ( R0 "8.314472 J/(mol K)" "Molar gas constant" nil 296 ( R0 "8.314472 J/(mol K)" "Molar gas constant" nil
297 "8.314472 J/(mol K) (*)") 297 "8.314472 J/(mol K) (*)")
298 ( V0 "22.710981*10^(-3) m^3/mol" "Standard volume of ideal gas" nil 298 ( V0 "22.710981*10^(-3) m^3/mol" "Standard volume of ideal gas" nil
299 "22.710981 10^-3 m^3/mol (*)"))) 299 "22.710981 10^-3 m^3/mol (*)")
300 ;; Logarithmic units
301 ( Np nil "*Neper")
302 ( dB "(ln(10)/20) Np" "decibel")))
300 303
301 304
302 (defvar math-additional-units nil 305 (defvar math-additional-units nil
303 "*Additional units table for user-defined units. 306 "*Additional units table for user-defined units.
304 Must be formatted like `math-standard-units'. 307 Must be formatted like `math-standard-units'.
869 (math-find-base-units-rec (nth 1 expr) pow)) 872 (math-find-base-units-rec (nth 1 expr) pow))
870 ((eq (car expr) 'var) 873 ((eq (car expr) 'var)
871 (or (eq (nth 1 expr) 'pi) 874 (or (eq (nth 1 expr) 'pi)
872 (error "Unknown name %s in defining expression for unit %s" 875 (error "Unknown name %s in defining expression for unit %s"
873 (nth 1 expr) (car math-fbu-entry)))) 876 (nth 1 expr) (car math-fbu-entry))))
877 ((equal expr '(calcFunc-ln 10)))
874 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry)))))) 878 (t (error "Malformed defining expression for unit %s" (car math-fbu-entry))))))
875 879
876 880
877 (defun math-units-in-expr-p (expr sub-exprs) 881 (defun math-units-in-expr-p (expr sub-exprs)
878 (and (consp expr) 882 (and (consp expr)
1549 (display-buffer buf))) 1553 (display-buffer buf)))
1550 (if enter-buffer 1554 (if enter-buffer
1551 (pop-to-buffer (get-buffer "*Units Table*")) 1555 (pop-to-buffer (get-buffer "*Units Table*"))
1552 (display-buffer (get-buffer "*Units Table*"))))) 1556 (display-buffer (get-buffer "*Units Table*")))))
1553 1557
1558 ;;; Logarithmic units functions
1559
1560 (defvar math-logunits '((var dB var-dB)
1561 (var Np var-Np)))
1562
1563 (defun math-extract-logunits (expr)
1564 (if (memq (car-safe expr) '(* /))
1565 (cons (car expr)
1566 (mapcar 'math-extract-logunits (cdr expr)))
1567 (if (memq (car-safe expr) '(^))
1568 (list '^ (math-extract-logunits (nth 1 expr)) (nth 2 expr))
1569 (if (member expr math-logunits) expr 1))))
1570
1571 (defun math-logcombine (a b neg)
1572 (let ((aunit (math-simplify (math-extract-logunits a))))
1573 (if (not (eq (car-safe aunit) 'var))
1574 (calc-record-why "*Improper logarithmic unit" aunit)
1575 (let* ((units (math-extract-units a))
1576 (acoeff (math-simplify (math-remove-units a)))
1577 (bcoeff (math-simplify (math-to-standard-units
1578 (list '/ b units) nil))))
1579 (if (math-units-in-expr-p bcoeff nil)
1580 (calc-record-why "*Inconsistent units" nil)
1581 (if (and neg
1582 (or (math-lessp acoeff bcoeff)
1583 (math-equal acoeff bcoeff)))
1584 (calc-record-why "*Improper coefficients" nil)
1585 (math-mul
1586 (if (equal aunit '(var dB var-dB))
1587 (math-mul 10
1588 (calcFunc-log10
1589 (if neg
1590 (math-sub
1591 (math-pow 10 (math-div acoeff 10))
1592 (math-pow 10 (math-div bcoeff 10)))
1593 (math-add
1594 (math-pow 10 (math-div acoeff 10))
1595 (math-pow 10 (math-div bcoeff 10))))))
1596 (calcFunc-ln
1597 (if neg
1598 (math-sub
1599 (calcFunc-exp acoeff)
1600 (calcFunc-exp bcoeff))
1601 (math-add
1602 (calcFunc-exp acoeff)
1603 (calcFunc-exp bcoeff)))))
1604 units)))))))
1605
1606 (defun calcFunc-luplus (a b)
1607 (math-logcombine a b nil))
1608
1609 (defun calcFunc-luminus (a b)
1610 (math-logcombine a b t))
1611
1612 (defun calc-luplus (arg)
1613 (interactive "P")
1614 (calc-slow-wrapper
1615 (if (calc-is-inverse)
1616 (calc-binary-op "lu-" 'calcFunc-luminus arg)
1617 (calc-binary-op "lu+" 'calcFunc-luplus arg))))
1618
1619 (defun calc-luminus (arg)
1620 (interactive "P")
1621 (calc-slow-wrapper
1622 (if (calc-is-inverse)
1623 (calc-binary-op "lu+" 'calcFunc-luplus arg)
1624 (calc-binary-op "lu-" 'calcFunc-luminus arg))))
1625
1626 ;(defun calcFunc-lmul (a b)
1627
1628
1629 (defun math-logunit-level (val ref power)
1630 (let ((lunit (math-simplify (math-extract-logunits val))))
1631 (if (not (eq (car-safe lunit) 'var))
1632 (calc-record-why "*Improper logarithmic unit" lunit)
1633 (if (not (eq 1 (math-simplify (math-extract-units (math-div val lunit)))))
1634 (calc-record-why "*Inappropriate units" nil)
1635 (let ((coeff (math-simplify (math-div val lunit))))
1636 (if (equal lunit '(var dB var-dB))
1637 (math-mul
1638 ref
1639 (math-pow
1640 10
1641 (math-div
1642 coeff
1643 (if power 10 20))))
1644 (math-mul
1645 ref
1646 (calcFunc-exp
1647 (if power
1648 (math-mul 2 coeff)
1649 coeff)))))))))
1650
1651 (defvar calc-default-field-reference-level)
1652 (defvar calc-default-power-reference-level)
1653
1654 (defun calcFunc-fieldlevel (val &optional ref)
1655 (unless ref
1656 (setq ref (math-read-expr calc-default-field-reference-level)))
1657 (math-logunit-level val ref nil))
1658
1659 (defun calcFunc-powerlevel (val &optional ref)
1660 (unless ref
1661 (setq ref (math-read-expr calc-default-power-reference-level)))
1662 (math-logunit-level val ref t))
1663
1664 (defun calc-level (arg)
1665 (interactive "P")
1666 (calc-slow-wrapper
1667 (if (calc-is-hyperbolic)
1668 (if (calc-is-option)
1669 (calc-binary-op "plvl" 'calcFunc-powerlevel arg)
1670 (calc-unary-op "plvl" 'calcFunc-powerlevel arg))
1671 (if (calc-is-option)
1672 (calc-binary-op "flvl" 'calcFunc-fieldlevel arg)
1673 (calc-unary-op "flvl" 'calcFunc-fieldlevel arg)))))
1674
1554 (provide 'calc-units) 1675 (provide 'calc-units)
1555 1676
1556 ;; Local variables: 1677 ;; Local variables:
1557 ;; coding: utf-8 1678 ;; coding: utf-8
1558 ;; End: 1679 ;; End: