summaryrefslogtreecommitdiffstats
path: root/ml/dlib/dlib/svm/sparse_vector_abstract.h
blob: e0c8d1f8c93b275de64a07bc68854f7567056452 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
// Copyright (C) 2009  Davis E. King (davis@dlib.net)
// License: Boost Software License   See LICENSE.txt for the full license.
#undef DLIB_SVm_SPARSE_VECTOR_ABSTRACT_
#ifdef DLIB_SVm_SPARSE_VECTOR_ABSTRACT_

#include <cmath>
#include "../algs.h"
#include "../serialize.h"
#include "../matrix.h"
#include <map>
#include <vector>
#include "../graph_utils/sample_pair_abstract.h"
#include "../graph_utils/ordered_sample_pair_abstract.h"

namespace dlib
{

// ----------------------------------------------------------------------------------------

    /*!A sparse_vectors

        In dlib, sparse vectors are represented using the container objects
        in the C++ STL.  In particular, a sparse vector is any container that 
        contains a range of std::pair<key, scalar_value> objects where:
            - key is an unsigned integral type 
            - scalar_value is float, double, or long double
            - the std::pair objects have unique key values
            - the std::pair objects are sorted such that small keys come first 

        Therefore, if an object satisfies the above requirements we call it a
        "sparse vector".  Additionally, we define the concept of an "unsorted sparse vector"
        to be a sparse vector that doesn't necessarily have sorted or unique key values.  
        Therefore, all sparse vectors are valid unsorted sparse vectors but not the other 
        way around.  

        An unsorted sparse vector with duplicate keys is always interpreted as
        a vector where each dimension contains the sum of all corresponding elements 
        of the unsorted sparse vector.  For example, an unsorted sparse vector 
        with the elements { (3,1), (0, 4), (3,5) } represents the 4D vector:
            [4, 0, 0, 1+5]



        Examples of valid sparse vectors are:    
            - std::map<unsigned long, double>
            - std::vector<std::pair<unsigned long, float> > where the vector is sorted.
              (you could make sure it was sorted by applying std::sort to it)


        Finally, by "dense vector" we mean a dlib::matrix object which represents
        either a row or column vector.

        The rest of this file defines a number of helper functions for doing normal 
        vector arithmetic things with sparse vectors.
    !*/

// ----------------------------------------------------------------------------------------

    /*!A has_unsigned_keys

        This is a template where has_unsigned_keys<T>::value == true when T is a
        sparse vector that contains unsigned integral keys and false otherwise.
    !*/

    template <typename T>
    struct has_unsigned_keys
    {
        static const bool value = is_unsigned_type<typename T::value_type::first_type>::value;
    };

// ----------------------------------------------------------------------------------------

    template <typename T, typename U>
    typename T::value_type::second_type distance_squared (
        const T& a,
        const U& b
    );
    /*!
        requires
            - a and b are sparse vectors
        ensures
            - returns the squared distance between the vectors
              a and b
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, typename U, typename V, typename W>
    typename T::value_type::second_type distance_squared (
        const V& a_scale,
        const T& a,
        const W& b_scale,
        const U& b
    );
    /*!
        requires
            - a and b are sparse vectors
        ensures
            - returns the squared distance between the vectors
              a_scale*a and b_scale*b
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, typename U>
    typename T::value_type::second_type distance (
        const T& a,
        const U& b
    );
    /*!
        requires
            - a and b are sparse vectors
        ensures
            - returns the distance between the vectors
              a and b.  (i.e. std::sqrt(distance_squared(a,b)))
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, typename U, typename V, typename W>
    typename T::value_type::second_type distance (
        const V& a_scale,
        const T& a,
        const W& b_scale,
        const U& b
    );
    /*!
        requires
            - a and b are sparse vectors
        ensures
            - returns the distance between the vectors
              a_scale*a and b_scale*b.  (i.e. std::sqrt(distance_squared(a_scale,a,b_scale,b)))
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, typename U>
    void assign (
        T& dest,
        const U& src
    );
    /*!
        requires
            - dest == a sparse vector or a dense vector
            - src == a sparse vector or a dense vector
            - dest is not dense when src is sparse
              (i.e. you can't assign a sparse vector to a dense vector.  This is
              because we don't know what the proper dimensionality should be for the
              dense vector)
        ensures
            - #src represents the same vector as dest.  
              (conversion between sparse/dense formats is done automatically)
    !*/


// ----------------------------------------------------------------------------------------

    template <typename T>
    typename T::value_type::second_type dot (
        const T& a,
        const T& b
    );
    /*!
        requires
            - a and b are sparse vectors 
        ensures
            - returns the dot product between the vectors a and b
    !*/

    template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
    T4 dot (
        const std::vector<T1,T2>& a,
        const std::map<T3,T4,T5,T6>& b
    );
    /*!
        requires
            - a and b are sparse vectors 
        ensures
            - returns the dot product between the vectors a and b
    !*/

    template <typename T1, typename T2, typename T3, typename T4, typename T5, typename T6>
    T4 dot (
        const std::map<T3,T4,T5,T6>& a,
        const std::vector<T1,T2>& b
    );
    /*!
        requires
            - a and b are sparse vectors 
        ensures
            - returns the dot product between the vectors a and b
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, typename EXP>
    typename T::value_type::second_type dot (
        const T& a,
        const matrix_exp<EXP>& b
    );
    /*!
        requires
            - a is an unsorted sparse vector
            - is_vector(b) == true
        ensures
            - returns the dot product between the vectors a and b.  
            - if (max_index_plus_one(a) >= b.size()) then
                - a's dimensionality is greater than b's dimensionality.  In this case we
                  pretend b is padded by as many zeros as is needed to make the dot product
                  work.  So this means that any elements in a that go beyond the length of
                  b are simply ignored.
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, typename EXP>
    typename T::value_type::second_type dot (
        const matrix_exp<EXP>& a,
        const T& b
    );
    /*!
        requires
            - b is an unsorted sparse vector
            - is_vector(a) == true
        ensures
            - returns the dot product between the vectors a and b
            - if (max_index_plus_one(b) >= a.size()) then
                - b's dimensionality is greater than a's dimensionality.  In this case we
                  pretend a is padded by as many zeros as is needed to make the dot product
                  work.  So this means that any elements in b that go beyond the length of
                  a are simply ignored.
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T>
    typename T::value_type::second_type length_squared (
        const T& a
    );
    /*!
        requires
            - a is a sparse vector
        ensures
            - returns dot(a,a) 
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T>
    typename T::value_type::second_type length (
        const T& a
    );
    /*!
        requires
            - a is a sparse vector
        ensures
            - returns std::sqrt(length_squared(a,a))
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, typename U>
    void scale_by (
        T& a,
        const U& value
    );
    /*!
        requires
            - a is an unsorted sparse vector or a dlib::matrix
        ensures
            - #a == a*value
              (i.e. multiplies every element of the vector a by value)
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T>
    T add (
        const T& a,
        const T& b
    );
    /*!
        requires
            - a is a sparse vector or dlib::matrix
            - b is a sparse vector or dlib::matrix
        ensures
            - returns a vector or matrix which represents a+b.  If the inputs are
              sparse vectors then the result is a sparse vector.
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T>
    T subtract (
        const T& a,
        const T& b
    );
    /*!
        requires
            - a is a sparse vector or dlib::matrix
            - b is a sparse vector or dlib::matrix
        ensures
            - returns a vector or matrix which represents a-b.  If the inputs are
              sparse vectors then the result is a sparse vector.
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T>
    unsigned long max_index_plus_one (
        const T& samples
    ); 
    /*!
        requires
            - samples == a single vector (either sparse or dense), or a container
              of vectors which is either a dlib::matrix of vectors or something 
              convertible to a dlib::matrix via mat() (e.g. a std::vector)
              Valid types of samples include (but are not limited to):
                - dlib::matrix<double,0,1>                      // A single dense vector 
                - std::map<unsigned int, double>                // A single sparse vector
                - std::vector<dlib::matrix<double,0,1> >        // An array of dense vectors
                - std::vector<std::map<unsigned int, double> >  // An array of sparse vectors
        ensures
            - This function tells you the dimensionality of a set of vectors.  The vectors
              can be either sparse or dense.  
            - if (samples.size() == 0) then
                - returns 0
            - else if (samples contains dense vectors or is a dense vector) then
                - returns the number of elements in the first sample vector.  This means
                  we implicitly assume all dense vectors have the same length)
            - else
                - In this case samples contains sparse vectors or is a sparse vector.  
                - returns the largest element index in any sample + 1.  Note that the element index values
                  are the values stored in std::pair::first.  So this number tells you the dimensionality
                  of a set of sparse vectors.
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, long NR, long NC, typename MM, typename L, typename SRC, typename U>
    inline void add_to (
        matrix<T,NR,NC,MM,L>& dest,
        const SRC& src,
        const U& C = 1
    );
    /*!
        requires
            - SRC == a matrix expression or an unsorted sparse vector
            - is_vector(dest) == true
            - Let MAX denote the largest element index in src.
              Then we require that:
                - MAX < dest.size()
                - (i.e. dest needs to be big enough to contain all the elements of src)
        ensures
            - #dest == dest + C*src
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T, long NR, long NC, typename MM, typename L, typename SRC, typename U>
    inline void subtract_from (
        matrix<T,NR,NC,MM,L>& dest,
        const SRC& src,
        const U& C = 1
    );
    /*!
        requires
            - SRC == a matrix expression or an unsorted sparse vector
            - is_vector(dest) == true
            - Let MAX denote the largest element index in src.
              Then we require that:
                - MAX < dest.size()
                - (i.e. dest needs to be big enough to contain all the elements of src)
        ensures
            - #dest == dest - C*src
    !*/

// ----------------------------------------------------------------------------------------

    template <typename T>
    typename T::value_type::second_type min (
        const T& vect
    );
    /*!
        requires
            - T == an unsorted sparse vector
        ensures
            - returns the minimum value in the sparse vector vect.  Note that
              this value is always <= 0 since a sparse vector has an unlimited number
              of 0 elements.
    !*/

// ------------------------------------------------------------------------------------

    template <typename T>
    typename T::value_type::second_type max (
        const T& vect
    );
    /*!
        requires
            - T == an unsorted sparse vector
        ensures
            - returns the maximum value in the sparse vector vect.  Note that
              this value is always >= 0 since a sparse vector has an unlimited number
              of 0 elements.
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename sample_type
        >
    matrix<typename sample_type::value_type::second_type,0,1> sparse_to_dense (
        const sample_type& vect
    );
    /*!
        requires
            - vect must be a sparse vector or a dense column vector.
        ensures
            - converts the single sparse or dense vector vect to a dense (column matrix form)
              representation.  That is, this function returns a vector V such that:
                - V.size() == max_index_plus_one(vect)
                - for all valid j:
                    - V(j) == The value of the j'th dimension of the vector vect.  Note 
                      that V(j) is zero if it is a sparse vector that doesn't contain an 
                      entry for the j'th dimension.
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename sample_type
        >
    matrix<typename sample_type::value_type::second_type,0,1> sparse_to_dense (
        const sample_type& vect,
        unsigned long num_dimensions 
    );
    /*!
        requires
            - vect must be a sparse vector or a dense column vector.
        ensures
            - converts the single sparse or dense vector vect to a dense (column matrix form)
              representation.  That is, this function returns a vector V such that:
                - V.size() == num_dimensions 
                - for all valid j:
                    - V(j) == The value of the j'th dimension of the vector vect.  Note 
                      that V(j) is zero if it is a sparse vector that doesn't contain an 
                      entry for the j'th dimension.
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename sample_type, 
        typename alloc
        >
    std::vector<matrix<typename sample_type::value_type::second_type,0,1> > sparse_to_dense (
        const std::vector<sample_type, alloc>& samples
    );
    /*!
        requires
            - all elements of samples must be sparse vectors or dense column vectors.
        ensures
            - converts from sparse sample vectors to dense (column matrix form)
            - That is, this function returns a std::vector R such that:
                - R contains column matrices    
                - R.size() == samples.size()
                - for all valid i: 
                    - R[i] == sparse_to_dense(samples[i], max_index_plus_one(samples))
                      (i.e. the dense (i.e. dlib::matrix) version of the sparse sample
                      given by samples[i].)
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename sample_type, 
        typename alloc
        >
    std::vector<matrix<typename sample_type::value_type::second_type,0,1> > sparse_to_dense (
        const std::vector<sample_type, alloc>& samples,
        unsigned long num_dimensions 
    );
    /*!
        requires
            - all elements of samples must be sparse vectors or dense column vectors.
        ensures
            - converts from sparse sample vectors to dense (column matrix form)
            - That is, this function returns a std::vector R such that:
                - R contains column matrices    
                - R.size() == samples.size()
                - for all valid i: 
                    - R[i] == sparse_to_dense(samples[i], num_dimensions)
                      (i.e. the dense (i.e. dlib::matrix) version of the sparse sample
                      given by samples[i].)
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    T make_sparse_vector (
        const T& v
    );
    /*!
        requires
            - v is an unsorted sparse vector
        ensures
            - returns a copy of v which is a sparse vector. 
              (i.e. it will be properly sorted and not have any duplicate key values but
              will still logically represent the same vector).
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename T
        >
    void make_sparse_vector_inplace(
        T& vect
    );
    /*!
        requires
            - v is an unsorted sparse vector
        ensures
            - vect == make_sparse_vector(vect)
            - This function is just an optimized version of make_sparse_vector(), in
              particular, when T is a std::vector<std::pair<>> type it is much more
              efficient.
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename EXP, 
        typename T, 
        long NR, 
        long NC, 
        typename MM, 
        typename L
        >
    void sparse_matrix_vector_multiply (
        const std::vector<sample_pair>& edges,
        const matrix_exp<EXP>& v,
        matrix<T,NR,NC,MM,L>& result
    );
    /*!
        requires
            - is_col_vector(v) == true
            - max_index_plus_one(edges) <= v.size()
        ensures
            - Interprets edges as representing a symmetric sparse matrix M.  The elements
              of M are defined such that, for all valid i,j:
                - M(i,j) == sum of edges[k].distance() for all k where edges[k]==sample_pair(i,j) 
                - This means that any element of M that doesn't have any edges associated
                  with it will have a value of 0.
            - #result == M*v
              (i.e. this function multiplies the vector v with the sparse matrix
              represented by edges and stores the output into result)
            - get_rect(#result) == get_rect(v)
              (i.e. result will have the same dimensions as v)
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename EXP, 
        typename T, 
        long NR, 
        long NC, 
        typename MM, 
        typename L
        >
    void sparse_matrix_vector_multiply (
        const std::vector<ordered_sample_pair>& edges,
        const matrix_exp<EXP>& v,
        matrix<T,NR,NC,MM,L>& result
    );
    /*!
        requires
            - is_col_vector(v) == true
            - max_index_plus_one(edges) <= v.size()
        ensures
            - Interprets edges as representing a square sparse matrix M.  The elements of M
              are defined such that, for all valid i,j:
                - M(i,j) == sum of edges[k].distance() for all k where edges[k]==ordered_sample_pair(i,j) 
                - This means that any element of M that doesn't have any edges associated
                  with it will have a value of 0.
            - #result == M*v
              (i.e. this function multiplies the vector v with the sparse matrix
              represented by edges and stores the output into result)
            - get_rect(#result) == get_rect(v)
              (i.e. result will have the same dimensions as v)
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename EXP
        >
    matrix<typename EXP::type,0,1> sparse_matrix_vector_multiply (
        const std::vector<sample_pair>& edges,
        const matrix_exp<EXP>& v
    );
    /*!
        requires
            - is_col_vector(v) == true
            - max_index_plus_one(edges) <= v.size()
        ensures
            - This is just a convenience routine for invoking the above
              sparse_matrix_vector_multiply() routine.  In particular, it just calls
              sparse_matrix_vector_multiply() with a temporary result matrix and then
              returns the result.
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename EXP
        >
    matrix<typename EXP::type,0,1> sparse_matrix_vector_multiply (
        const std::vector<ordered_sample_pair>& edges,
        const matrix_exp<EXP>& v
    );
    /*!
        requires
            - is_col_vector(v) == true
            - max_index_plus_one(edges) <= v.size()
        ensures
            - This is just a convenience routine for invoking the above
              sparse_matrix_vector_multiply() routine.  In particular, it just calls
              sparse_matrix_vector_multiply() with a temporary result matrix and then
              returns the result.
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename EXP, 
        typename sparse_vector_type,
        typename T,
        long NR,
        long NC,
        typename MM,
        typename L
        >
    void sparse_matrix_vector_multiply (
        const matrix_exp<EXP>& m,
        const sparse_vector_type& v,
        matrix<T,NR,NC,MM,L>& result
    );
    /*!
        requires
            - max_index_plus_one(v) <= m.nc()
            - v == an unsorted sparse vector
        ensures
            - #result == m*v
              (i.e. multiply m by the vector v and store the output in result)
    !*/

// ----------------------------------------------------------------------------------------

    template <
        typename EXP, 
        typename sparse_vector_type
        >
    matrix<typename EXP::type,0,1> sparse_matrix_vector_multiply (
        const matrix_exp<EXP>& m,
        const sparse_vector_type& v
    );
    /*!
        requires
            - max_index_plus_one(v) <= m.nc()
            - v == an unsorted sparse vector
        ensures
            - returns m*v
              (i.e. multiply m by the vector v and return the resulting vector)
    !*/

// ----------------------------------------------------------------------------------------

}

#endif // DLIB_SVm_SPARSE_VECTOR_ABSTRACT_