;;; calc-vec.el --- vector functions for Calc
-;; Copyright (C) 1990, 1991, 1992, 1993, 2001 Free Software Foundation, Inc.
+;; Copyright (C) 1990, 1991, 1992, 1993, 2001, 2002, 2003, 2004,
+;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
;; Author: David Gillespie <daveg@synaptics.com>
-;; Maintainers: D. Goel <deego@gnufans.org>
-;; Colin Walters <walters@debian.org>
+;; Maintainer: Jay Belanger <jay.p.belanger@gmail.com>
;; This file is part of GNU Emacs.
+;; GNU Emacs is free software: you can redistribute it and/or modify
+;; it under the terms of the GNU General Public License as published by
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
+
;; GNU Emacs is distributed in the hope that it will be useful,
-;; but WITHOUT ANY WARRANTY. No author or distributor
-;; accepts responsibility to anyone for the consequences of using it
-;; or for whether it serves any particular purpose or works at all,
-;; unless he says so in writing. Refer to the GNU Emacs General Public
-;; License for full details.
-
-;; Everyone is granted permission to copy, modify and redistribute
-;; GNU Emacs, but only under the conditions described in the
-;; GNU Emacs General Public License. A copy of this license is
-;; supposed to have been given to you along with GNU Emacs so you
-;; can know your rights and responsibilities. It should be in a
-;; file named COPYING. Among other things, the copyright notice
-;; and this notice must be preserved on all copies.
+;; but WITHOUT ANY WARRANTY; without even the implied warranty of
+;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+;; GNU General Public License for more details.
+
+;; You should have received a copy of the GNU General Public License
+;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
;; This file is autoloaded from calc-ext.el.
-(require 'calc-ext)
+(require 'calc-ext)
(require 'calc-macs)
-(defun calc-Need-calc-vec () nil)
+;; Declare functions which are defined elsewhere.
+(declare-function math-read-expr-level "calc-aent" (exp-prec &optional exp-term))
(defun calc-display-strings (n)
(calc-wrapper
(calc-binary-op "cros" 'calcFunc-cross arg)))
+(defun calc-kron (arg)
+ (interactive "P")
+ (calc-wrapper
+ (calc-binary-op "kron" 'calcFunc-kron arg)))
+
(defun calc-remove-duplicates (arg)
(interactive "P")
(calc-wrapper
(cons 'vec (nreverse (sort (copy-sequence (cdr vec)) 'math-beforep)))
(math-reject-arg vec 'vectorp)))
-(defun calcFunc-grade (grade-vec)
- (if (math-vectorp grade-vec)
- (let* ((len (1- (length grade-vec))))
+;; The variable math-grade-vec is local to calcFunc-grade and
+;; calcFunc-rgrade, but is used by math-grade-beforep, which is called
+;; by calcFunc-grade and calcFunc-rgrade.
+(defvar math-grade-vec)
+
+(defun calcFunc-grade (math-grade-vec)
+ (if (math-vectorp math-grade-vec)
+ (let* ((len (1- (length math-grade-vec))))
(cons 'vec (sort (cdr (calcFunc-index len)) 'math-grade-beforep)))
- (math-reject-arg grade-vec 'vectorp)))
+ (math-reject-arg math-grade-vec 'vectorp)))
-(defun calcFunc-rgrade (grade-vec)
- (if (math-vectorp grade-vec)
- (let* ((len (1- (length grade-vec))))
+(defun calcFunc-rgrade (math-grade-vec)
+ (if (math-vectorp math-grade-vec)
+ (let* ((len (1- (length math-grade-vec))))
(cons 'vec (nreverse (sort (cdr (calcFunc-index len))
'math-grade-beforep))))
- (math-reject-arg grade-vec 'vectorp)))
+ (math-reject-arg math-grade-vec 'vectorp)))
(defun math-grade-beforep (i j)
- (math-beforep (nth i grade-vec) (nth j grade-vec)))
+ (math-beforep (nth i math-grade-vec) (nth j math-grade-vec)))
;;; Compile a histogram of data from a vector.
(math-reject-arg a "*Three-vector expected")))
-
-
-
-(defun math-read-brackets (space-sep close)
+;;; Compute a Kronecker product
+(defun calcFunc-kron (x y &optional nocheck)
+ "The Kronecker product of objects X and Y.
+The objects X and Y may be scalars, vectors or matrices.
+The type of the result depends on the types of the operands;
+the product of two scalars is a scalar,
+of one scalar and a vector is a vector,
+of two vectors is a vector.
+of one vector and a matrix is a matrix,
+of two matrices is a matrix."
+ (unless nocheck
+ (cond ((or (math-matrixp x)
+ (math-matrixp y))
+ (unless (math-matrixp x)
+ (setq x (if (math-vectorp x)
+ (list 'vec x)
+ (list 'vec (list 'vec x)))))
+ (unless (math-matrixp y)
+ (setq y (if (math-vectorp y)
+ (list 'vec y)
+ (list 'vec (list 'vec y))))))
+ ((or (math-vectorp x)
+ (math-vectorp y))
+ (unless (math-vectorp x)
+ (setq x (list 'vec x)))
+ (unless (math-vectorp y)
+ (setq y (list 'vec y))))))
+ (if (math-vectorp x)
+ (let (ret)
+ (dolist (v (cdr x))
+ (dolist (w (cdr y))
+ (setq ret (cons (calcFunc-kron v w t) ret))))
+ (cons 'vec (nreverse ret)))
+ (math-mul x y)))
+
+
+;; The variable math-rb-close is local to math-read-brackets, but
+;; is used by math-read-vector, which is called (directly and
+;; indirectly) by math-read-brackets.
+(defvar math-rb-close)
+
+;; The next few variables are local to math-read-exprs in calc-aent.el
+;; and math-read-expr in calc-ext.el, but are set in functions they call.
+(defvar math-exp-pos)
+(defvar math-exp-str)
+(defvar math-exp-old-pos)
+(defvar math-exp-token)
+(defvar math-exp-keep-spaces)
+(defvar math-expr-data)
+
+(defun math-read-brackets (space-sep math-rb-close)
(and space-sep (setq space-sep (not (math-check-for-commas))))
(math-read-token)
(while (eq math-exp-token 'space)
(math-read-token))
- (if (or (equal math-expr-data close)
+ (if (or (equal math-expr-data math-rb-close)
(eq math-exp-token 'end))
(progn
(math-read-token)
(setq vals2 (catch 'syntax (math-read-vector))))
(if (and (not (stringp vals2))
(or (assoc math-expr-data '(("\\ldots") ("\\dots") (";")))
- (equal math-expr-data close)
+ (equal math-expr-data math-rb-close)
(eq math-exp-token 'end)))
(setq space-sep nil
vals vals2)
(math-read-token)
(setq vals (if (> (length vals) 2)
(cons 'calcFunc-mul (cdr vals)) (nth 1 vals)))
- (let ((exp2 (if (or (equal math-expr-data close)
+ (let ((exp2 (if (or (equal math-expr-data math-rb-close)
(equal math-expr-data ")")
(eq math-exp-token 'end))
'(var inf var-inf)
(if (equal math-expr-data ")") 2 3)
vals
exp2)))
- (if (not (or (equal math-expr-data close)
+ (if (not (or (equal math-expr-data math-rb-close)
(equal math-expr-data ")")
(eq math-exp-token 'end)))
(throw 'syntax "Expected `]'")))
(if (equal math-expr-data ";")
(let ((math-exp-keep-spaces space-sep))
(setq vals (cons 'vec (math-read-matrix (list vals))))))
- (if (not (or (equal math-expr-data close)
+ (if (not (or (equal math-expr-data math-rb-close)
(eq math-exp-token 'end)))
(throw 'syntax "Expected `]'")))
(or (eq math-exp-token 'end)
(math-read-token))
(and (not (eq math-exp-token 'end))
(not (equal math-expr-data ";"))
- (not (equal math-expr-data close))
+ (not (equal math-expr-data math-rb-close))
(not (equal math-expr-data "\\dots"))
(not (equal math-expr-data "\\ldots"))))
(if (equal math-expr-data ",")
(setq mat (nconc mat (list (math-read-vector)))))
mat)
-;;; arch-tag: 7902a7af-ec69-440a-8635-ebb4db263402
+(provide 'calc-vec)
+
+;; arch-tag: 7902a7af-ec69-440a-8635-ebb4db263402
;;; calc-vec.el ends here