Coverage report: /home/ellis/comp/core/lib/obj/tensor/proto.lisp

KindCoveredAll%
expression343 7.0
branch00nil
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; proto.lisp --- Tensor Protocols
2
 
3
 ;; Tensor Object API
4
 
5
 ;;; Commentary:
6
 
7
 ;; This file contains the 'high-level' tensor protocol. See meta.lisp for the
8
 ;; low-level bits.
9
 
10
 ;;; Code:
11
 (in-package :obj/tensor)
12
 
13
 ;;; Vars
14
 (defparameter *default-sparse-store-increment* 100
15
   "Determines the increment by which the store of a compressed sparse matrix is
16
 increased, when it runs out of store.")
17
 
18
 (defparameter *default-sparsity* 1/1000
19
   "Determines the default sparsity for a newly created sparse matrix, when the
20
 number of non-zero is not specified.")
21
 
22
 (defparameter *max-sparse-size* 10000
23
   "Upper bounds the store size for a newly created sparse matrix, when the number
24
 of non-zero is not specified.")
25
 
26
 ;;Default ordering of strides
27
 (eval-always
28
   (defparameter *default-stride-ordering* :col-major
29
     "Determines whether strides are row or column major by default.
30
 
31
 (let ((*default-stride-ordering* :col-major))
32
    (make-real-tensor 10 10))
33
 ;; returns a 10x10 matrix in Column major order."))
34
 
35
 (defparameter *default-tensor-type* 'real-tensor)
36
 
37
 (defparameter *tensor-safety-p* t
38
   "If non-nil, then check for invalid values in the field of the class in the
39
 :after specialized method (if defined), else do nothing. One ought to be very
40
 carful when doing, much of Matlisp's code is written on the assumption that
41
 the fields of a tensor don't take invalid values; failing which case, may lead
42
 to memory error. Use at your own risk.")
43
 
44
 (defparameter *print-tensor-max-len* 10
45
   "Maximum number of elements in any particular argument to print.
46
 Set this to T to print all the elements.")
47
 
48
 (defparameter *print-tensor-max-args* 5
49
   "Maximum number of arguments of the tensor to print.
50
 Set this to T to print all the arguments.")
51
 
52
 (defparameter *print-tensor-indent* 0
53
   "Determines how many spaces will be printed before each row
54
 of a matrix (default 0)")
55
 
56
 ;;; Conditions
57
 (define-condition tensor-invalid-dimension-value (error)
58
   ((argument :initarg :argument)
59
    (dimension :initarg :dimension))
60
   (:report 
61
    (lambda (c s)
62
      (with-slots (argument dimension) c
63
        (format s "Invalid dimension arg: ~A~%dimension: ~A" argument dimension)))))
64
 
65
 ;;; Types
66
 (deftype index-type () 'fixnum)
67
 
68
 (deftype index-store-vector (&optional (size '*)) `(simple-array index-type (,size)))
69
 
70
 ;;; Generics
71
 (defgeneric print-element (tensor
72
                            element stream)
73
   (:documentation "This generic function is specialized to TENSOR to print ELEMENT to STREAM.
74
 Called by PRINT-TENSOR/MATRIX to format a tensor into the STREAM."))
75
 
76
 (defgeneric size (obj)
77
   (:method ((obj sequence))
78
     (length obj))
79
   (:method ((arr array))
80
     (reduce #'* (array-dimensions arr))))
81
 
82
 (defgeneric store-size (tensor)
83
   (:documentation "Returns the number of elements the store of the tensor can hold (which is not
84
 necessarily equal to its vector length)."))
85
 
86
 (defgeneric store-ref (tensor idx)
87
   (:documentation  "Generic serial read access to the store."))
88
 
89
 (defgeneric (setf store-ref) (value tensor idx))
90
 
91
 (defgeneric subtensor (tensor subscripts)
92
   (:documentation "Creates a new tensor data structure, sharing store with TENSOR but with
93
 different strides and dimensions, as defined in the subscript-list SUBSCRIPTS.
94
 
95
 Examples:
96
 (defvar X (make-real-tensor 10 10 10))
97
 ;; X
98
 
99
 ;; Get (: 0 0)
100
 (subtensor X '((nil nil . nil) (0 1 . nil) (0 1 . nil)))
101
 ;; Get (: 2:5 :)
102
 (subtensor X '((nil nil . nil) (2 5 . nil)))
103
 ;; Get (: : 0:2:10) (0:10:2 = [i : 0 <= i < 10, i % 2 = 0])
104
 (subtensor X '((nil nil . nil) (nil nil . nil) (0 10 . 2)))
105
 
106
 Sadly in our parentheses filled world, this function has to be necessarily
107
 verbose (unlike MATLAB, Python). However, this function has been designed with
108
 the express purpose of using it with a Lisp reader macro. The slicing
109
 semantics is essentially the same as MATLAB except for the zero-based
110
 indexing."))
111
 
112
 (defgeneric suptensor (tensor ord &optional start))
113
 
114
 (defgeneric reshape (tensor dims)
115
   (:documentation "Reshape TENSOR to DIMS. This function expects all the strides to be of the
116
 same sign when TENSOR is subtype of STANDARD-TENSOR."))
117
 
118
 (defgeneric ref (tensor &rest subscripts)
119
   (:documentation "Return the element from TENSOR corresponding to SUBSCRIPTS"))
120
 
121
 (defgeneric (setf ref) (value tensor &rest subscripts))
122
 
123
 (defgeneric fc (x)
124
   (:method ((x complex))
125
     (conjugate x))
126
   (:method ((x real))
127
     x))
128
 
129
 ;;; Tensor Classes
130
 ;; base-tensor, dense/sparse
131
 ;; standard-tensor (lisp)
132
 ;; static-tensor, c-tensor
133
 ;; blas-tensor, lapack-tensor
134
 ;; simd-tensor