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

KindCoveredAll%
expression082 0.0
branch02 0.0
Key
Not instrumented
Conditionalized out
Executed
Not executed
 
Both branches taken
One branch taken
Neither branch taken
1
 ;;; util.lisp --- Tensor Utils
2
 
3
 ;; 
4
 
5
 ;;; Code:
6
 (in-package :obj/tensor)
7
 ;;; Utils
8
 (defmacro with-no-init-checks (&body body)
9
   `(let ((*tensor-safety-p* nil))
10
      ,@body))
11
 
12
 (defun subfieldp (a b)
13
   (subtypep (field-type a) (field-type b)))
14
 
15
 (defun t.zeros (ty dims &optional (initial-element 0))
16
   (let* ((adims (make-index-store dims)))
17
     (declare (index-store-vector adims))
18
     (multiple-value-bind (astrs sizs) (make-stride adims)
19
       (declare (type index-store-vector astrs))
20
       (make-instance ty
21
         :dimensions adims
22
         :head 0
23
         :strides astrs
24
         :store (t.store-allocator ty sizs initial-element)))))
25
 
26
 ;; (deft t.zeros (class coordinate-sparse-tensor) (dims &optional nz)
27
 ;;   (with-gensyms (astrs adims sizs)
28
 ;;     `(let* ((,adims (make-index-store ,dims)))
29
 ;;        (declare (type index-store-vector ,adims))
30
 ;;        (multiple-value-bind (,astrs ,sizs) (make-stride-cmj ,adims)
31
 ;;          (declare (type index-store-vector ,astrs))
32
 ;;          (make-instance ',class
33
 ;;            :dimensions ,adims
34
 ;;            :strides ,astrs
35
 ;;            :store (t.store-allocator ,class ,sizs ,nz))))))
36
 
37
 ;; (deft t.zeros (class compressed-sparse-matrix) (dims &optional nz)
38
 ;;   (with-gensyms (dsym)
39
 ;;     `(let ((,dsym ,dims))
40
 ;;        (destructuring-bind (vr vd) (t.store-allocator ,class ,dsym ,nz)
41
 ;;          (make-instance ',class
42
 ;;            :dimensions (make-index-store ,dims)
43
 ;;            :neighbour-start (allocate-index-store (1+ (second ,dsym)))
44
 ;;            :neighbour-id vr
45
 ;;            :store vd)))))
46
 
47
 (defgeneric %zeros (dims dtype &optional initial-element)
48
   (:documentation "internal dispatch for ZEROS.")
49
   (:method ((dims cons) (dtype t) &optional initial-element)
50
     ;; (assert (member dtype *tensor-type-leaves*) nil 'tensor-abstract-class :tensor-class dtype)
51
         (if initial-element
52
             (t.zeros dtype dims initial-element)
53
             (t.zeros dtype dims))))
54
 
55
 (definline zeros (dims &optional (type *default-tensor-type*) (initial-element 0))
56
 "Create a tensor with dimensions @arg{dims} of class @arg{dtype}.
57
 The optional argument @arg{initial-element} is used in two completely
58
 incompatible ways.
59
 
60
 If TYPE is a dense tensor, then INITIAL-ELEMENT, is used to initialize all the
61
 elements. If TYPE is however, a sparse tensor, it is used for computing the
62
 number of nonzeros slots in the store.
63
 
64
 Example:
65
 (zeros 3)
66
 #<REAL-TENSOR #(3)
67
   0.0000      0.0000      0.0000     
68
 >
69
 
70
 (zeros 3 'complex-tensor 2)
71
 #<COMPLEX-TENSOR #(3)
72
   2.0000      2.0000      2.0000     
73
 >
74
 
75
 (zeros '(10000 10000) 'real-compressed-sparse-matrix 10000)
76
 #<REAL-COMPRESSED-SPARSE-MATRIX #(10000 10000), store-size: 10000>"
77
   (with-no-init-checks
78
     (etypecase dims
79
       (cons
80
        (%zeros dims type initial-element))
81
       (vector
82
        (%zeros (vector-to-list dims) type initial-element))
83
       (fixnum
84
        (%zeros (list dims) type initial-element)))))
85
 
86
 (declaim (ftype (function ((or cons vector fixnum) &optional t t) base-tensor) zeros))
87
 
88
 (defmacro with-rowm (&rest body)
89
   `(let ((*default-stride-ordering* :row-major))
90
      ,@body))
91
 
92
 (defmacro with-colm (&rest body)
93
   `(let ((*default-stride-ordering* :col-major))
94
      ,@body))
95
 
96
 
97
 (definline nrows (matrix)
98
   (aref (the index-store-vector (dimensions matrix)) 0))
99
 
100
 (definline ncols (matrix)
101
   (aref (the index-store-vector (dimensions matrix)) 1))
102
 
103
 (definline row-stride (matrix)
104
   (aref (the index-store-vector (strides matrix)) 0))
105
 
106
 (definline col-stride (matrix)
107
   (aref (the index-store-vector (strides matrix)) 1))
108
 
109
 (definline tensor-square-matrixp (matrix)
110
   (and (tensor-matrixp matrix) (tensor-squarep matrix)))