summaryrefslogtreecommitdiffstats
path: root/include/2geom/numeric/vector.h
blob: b66115bc3cf12dbd9708ca4b672aabb96a3f370d (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
/*
 * Vector, VectorView, ConstVectorView classes wrap the gsl vector routines;
 * "views" mimic the semantic of C++ references: any operation performed
 * on a "view" is actually performed on the "viewed object"
 *
 * Authors:
 * 		Marco Cecchetti <mrcekets at gmail.com>
 *
 * Copyright 2008  authors
 *
 * This library is free software; you can redistribute it and/or
 * modify it either under the terms of the GNU Lesser General Public
 * License version 2.1 as published by the Free Software Foundation
 * (the "LGPL") or, at your option, under the terms of the Mozilla
 * Public License Version 1.1 (the "MPL"). If you do not alter this
 * notice, a recipient may use your version of this file under either
 * the MPL or the LGPL.
 *
 * You should have received a copy of the LGPL along with this library
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * You should have received a copy of the MPL along with this library
 * in the file COPYING-MPL-1.1
 *
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
 * the specific language governing rights and limitations.
 */




#ifndef _NL_VECTOR_H_
#define _NL_VECTOR_H_

#include <cassert>
#include <algorithm> // for std::swap
#include <vector>
#include <sstream>
#include <string>


#include <gsl/gsl_vector.h>
#include <gsl/gsl_blas.h>


namespace Geom { namespace NL {

namespace detail
{

class BaseVectorImpl
{
  public:
	double const& operator[](size_t i) const
	{
		return *gsl_vector_const_ptr(m_vector, i);
	}

	const gsl_vector* get_gsl_vector() const
	{
		return m_vector;
	}
	bool is_zero() const
	{
		return gsl_vector_isnull(m_vector);
	}

	bool is_positive() const
	{
	    for ( size_t i = 0; i < size(); ++i )
	    {
	        if ( (*this)[i] <= 0 ) return false;
	    }
		return true;
	}

	bool is_negative() const
	{
        for ( size_t i = 0; i < size(); ++i )
        {
            if ( (*this)[i] >= 0 ) return false;
        }
        return true;
	}

	bool is_non_negative() const
	{
		for ( size_t i = 0; i < size(); ++i )
		{
			if ( (*this)[i] < 0 ) return false;
		}
		return true;
	}

	double max() const
	{
		return gsl_vector_max(m_vector);
	}

	double min() const
	{
		return gsl_vector_min(m_vector);
	}

	size_t max_index() const
	{
		return gsl_vector_max_index(m_vector);
	}

	size_t min_index() const
	{
		return gsl_vector_min_index(m_vector);
	}

	size_t size() const
	{
		return m_size;
	}

	std::string str() const;

	virtual ~BaseVectorImpl()
	{
	}

  protected:
	size_t m_size;
	gsl_vector* m_vector;

};  // end class BaseVectorImpl


inline
bool operator== (BaseVectorImpl const& v1, BaseVectorImpl const& v2)
{
	if (v1.size() != v2.size())	return false;

	for (size_t i = 0; i < v1.size(); ++i)
	{
		if (v1[i] != v2[i])  return false;
	}
	return true;
}

template< class charT >
inline
std::basic_ostream<charT> &
operator<< (std::basic_ostream<charT> & os, const BaseVectorImpl & _vector)
{
	if (_vector.size() == 0 ) return os;
	os << "[" << _vector[0];
	for (unsigned int i = 1; i < _vector.size(); ++i)
	{
		os << ", " << _vector[i];
	}
	os << "]";
	return os;
}

inline
std::string BaseVectorImpl::str() const
{
	std::ostringstream oss;
	oss << (*this);
	return oss.str();
}

inline
double dot(BaseVectorImpl const& v1, BaseVectorImpl const& v2)
{
    double result;
    gsl_blas_ddot(v1.get_gsl_vector(), v2.get_gsl_vector(), &result);
    return result;
}


class VectorImpl : public BaseVectorImpl
{
  public:
	typedef BaseVectorImpl base_type;

  public:
	void set_all(double x)
	{
		gsl_vector_set_all(m_vector, x);
	}

	void set_basis(size_t i)
	{
		gsl_vector_set_basis(m_vector, i);
	}

	using base_type::operator[];

	double & operator[](size_t i)
	{
		return *gsl_vector_ptr(m_vector, i);
	}

	using base_type::get_gsl_vector;

	gsl_vector* get_gsl_vector()
	{
		return m_vector;
	}

	void swap_elements(size_t i, size_t j)
	{
		gsl_vector_swap_elements(m_vector, i, j);
	}

	void reverse()
	{
		gsl_vector_reverse(m_vector);
	}

	VectorImpl & scale(double x)
	{
		gsl_vector_scale(m_vector, x);
		return (*this);
	}

	VectorImpl & translate(double x)
	{
		gsl_vector_add_constant(m_vector, x);
		return (*this);
	}

	VectorImpl & operator+=(base_type const& _vector)
	{
		gsl_vector_add(m_vector, _vector.get_gsl_vector());
		return (*this);
	}

	VectorImpl & operator-=(base_type const& _vector)
	{
		gsl_vector_sub(m_vector, _vector.get_gsl_vector());
		return (*this);
	}

};  // end class VectorImpl

}  // end namespace detail


using detail::operator==;
using detail::operator<<;

class Vector : public detail::VectorImpl
{
  public:
	typedef detail::VectorImpl base_type;

  public:
	Vector(size_t n)
	{
		m_size = n;
		m_vector = gsl_vector_alloc(n);
	}

	Vector(size_t n, double x)
	{
		m_size = n;
		m_vector = gsl_vector_alloc(n);
		gsl_vector_set_all(m_vector, x);
	}

	// create a vector with n elements all set to zero
	// but the i-th that is set to 1
	Vector(size_t n, size_t i)
	{
		m_size = n;
		m_vector = gsl_vector_alloc(n);
		gsl_vector_set_basis(m_vector, i);
	}

	Vector(Vector const& _vector)
        : base_type()
	{
		m_size = _vector.size();
		m_vector = gsl_vector_alloc(size());
		gsl_vector_memcpy(m_vector, _vector.m_vector);
	}

	explicit
	Vector(base_type::base_type const& _vector)
	{
		m_size = _vector.size();
		m_vector = gsl_vector_alloc(size());
		gsl_vector_memcpy(m_vector, _vector.get_gsl_vector());
	}

	~Vector() override
	{
		gsl_vector_free(m_vector);
	}


	Vector & operator=(Vector const& _vector)
	{
		assert( size() == _vector.size() );
		gsl_vector_memcpy(m_vector, _vector.m_vector);
		return (*this);
	}

	Vector & operator=(base_type::base_type const& _vector)
	{
		assert( size() == _vector.size() );
		gsl_vector_memcpy(m_vector, _vector.get_gsl_vector());
		return (*this);
	}

	Vector & scale(double x)
	{
		return static_cast<Vector&>( base_type::scale(x) );
	}

	Vector & translate(double x)
	{
		return static_cast<Vector&>( base_type::translate(x) );
	}

	Vector & operator+=(base_type::base_type const& _vector)
	{
		return static_cast<Vector&>( base_type::operator+=(_vector) );
	}

	Vector & operator-=(base_type::base_type const& _vector)
	{
		return static_cast<Vector&>( base_type::operator-=(_vector) );
	}

	friend
	void swap(Vector & v1, Vector & v2);
	friend
	void swap_any(Vector & v1, Vector & v2);

}; // end class Vector


// warning! these operations invalidate any view of the passed vector objects
inline
void swap(Vector & v1, Vector & v2)
{
    assert(v1.size() == v2.size());
    using std::swap;
    swap(v1.m_vector, v2.m_vector);
}

inline
void swap_any(Vector & v1, Vector & v2)
{
	using std::swap;
    swap(v1.m_vector, v2.m_vector);
    swap(v1.m_size, v2.m_size);
}


class ConstVectorView : public detail::BaseVectorImpl
{
  public:
	typedef detail::BaseVectorImpl base_type;

  public:
	ConstVectorView(const base_type & _vector, size_t n, size_t offset = 0)
		: m_vector_view( gsl_vector_const_subvector(_vector.get_gsl_vector(), offset, n) )
	{
		m_size = n;
		m_vector = const_cast<gsl_vector*>( &(m_vector_view.vector) );
	}

	ConstVectorView(const base_type & _vector, size_t n, size_t offset , size_t stride)
		: m_vector_view( gsl_vector_const_subvector_with_stride(_vector.get_gsl_vector(), offset, stride, n) )
	{
		m_size = n;
		m_vector = const_cast<gsl_vector*>( &(m_vector_view.vector) );
	}

    ConstVectorView(const double* _vector, size_t n, size_t offset = 0)
        : m_vector_view( gsl_vector_const_view_array(_vector + offset, n) )
    {
        m_size = n;
        m_vector = const_cast<gsl_vector*>( &(m_vector_view.vector) );
    }

    ConstVectorView(const double* _vector, size_t n, size_t offset, size_t stride)
        : m_vector_view( gsl_vector_const_view_array_with_stride(_vector + offset, stride, n) )
    {
        m_size = n;
        m_vector = const_cast<gsl_vector*>( &(m_vector_view.vector) );
    }

	explicit
	ConstVectorView(gsl_vector_const_view  _gsl_vector_view)
		: m_vector_view(_gsl_vector_view)
	{
		m_vector = const_cast<gsl_vector*>( &(m_vector_view.vector) );
		m_size = m_vector->size;
	}

    explicit
    ConstVectorView(const std::vector<double>&  _vector)
        : m_vector_view( gsl_vector_const_view_array(&(_vector[0]), _vector.size()) )
    {
        m_vector = const_cast<gsl_vector*>( &(m_vector_view.vector) );
        m_size = _vector.size();
    }

	ConstVectorView(const ConstVectorView & _vector)
		: base_type(),
		  m_vector_view(_vector.m_vector_view)
	{
		m_size = _vector.size();
		m_vector = const_cast<gsl_vector*>( &(m_vector_view.vector) );
	}

	ConstVectorView(const base_type & _vector)
		: m_vector_view(gsl_vector_const_subvector(_vector.get_gsl_vector(), 0, _vector.size()))
	{
		m_size = _vector.size();
		m_vector = const_cast<gsl_vector*>( &(m_vector_view.vector) );
	}

  private:
	gsl_vector_const_view m_vector_view;

}; // end class  ConstVectorView




class VectorView : public detail::VectorImpl
{
  public:
	typedef detail::VectorImpl base_type;

  public:
	VectorView(base_type & _vector, size_t n, size_t offset = 0, size_t stride = 1)
	{
		m_size = n;
		if (stride == 1)
		{
			m_vector_view
				= gsl_vector_subvector(_vector.get_gsl_vector(), offset, n);
			m_vector = &(m_vector_view.vector);
		}
		else
		{
			m_vector_view
				= gsl_vector_subvector_with_stride(_vector.get_gsl_vector(), offset, stride, n);
			m_vector = &(m_vector_view.vector);
		}
	}

    VectorView(double* _vector, size_t n, size_t offset = 0, size_t stride = 1)
    {
        m_size = n;
        if (stride == 1)
        {
            m_vector_view
                = gsl_vector_view_array(_vector + offset, n);
            m_vector = &(m_vector_view.vector);
        }
        else
        {
            m_vector_view
                = gsl_vector_view_array_with_stride(_vector + offset, stride, n);
            m_vector = &(m_vector_view.vector);
        }

    }

	VectorView(const VectorView & _vector)
        : base_type()
	{
		m_size = _vector.size();
		m_vector_view = _vector.m_vector_view;
		m_vector = &(m_vector_view.vector);
	}

	VectorView(Vector & _vector)
	{
		m_size = _vector.size();
		m_vector_view = gsl_vector_subvector(_vector.get_gsl_vector(), 0, size());
		m_vector = &(m_vector_view.vector);
	}

	explicit
	VectorView(gsl_vector_view _gsl_vector_view)
		: m_vector_view(_gsl_vector_view)
	{
		m_vector = &(m_vector_view.vector);
		m_size = m_vector->size;
	}

	explicit
	VectorView(std::vector<double> & _vector)
	{
	    m_size = _vector.size();
	    m_vector_view = gsl_vector_view_array(&(_vector[0]), _vector.size());
	    m_vector = &(m_vector_view.vector);
	}

	VectorView & operator=(VectorView const& _vector)
	{
		assert( size() == _vector.size() );
		gsl_vector_memcpy(m_vector, _vector.get_gsl_vector());
		return (*this);
	}

	VectorView & operator=(base_type::base_type const& _vector)
	{
		assert( size() == _vector.size() );
		gsl_vector_memcpy(m_vector, _vector.get_gsl_vector());
		return (*this);
	}

	VectorView & scale(double x)
	{
		return static_cast<VectorView&>( base_type::scale(x) );
	}

	VectorView & translate(double x)
	{
		return static_cast<VectorView&>( base_type::translate(x) );
	}

	VectorView & operator+=(base_type::base_type const& _vector)
	{
		return static_cast<VectorView&>( base_type::operator+=(_vector) );
	}

	VectorView & operator-=(base_type::base_type const& _vector)
	{
		return static_cast<VectorView&>( base_type::operator-=(_vector) );
	}

	friend
	void swap_view(VectorView & v1, VectorView & v2);

  private:
	gsl_vector_view m_vector_view;

}; // end class VectorView


inline
void swap_view(VectorView & v1, VectorView & v2)
{
	assert( v1.size() == v2.size() );
	using std::swap;
	swap(v1.m_vector_view, v2.m_vector_view); // not swap m_vector too
}

inline
const VectorView & const_vector_view_cast (const ConstVectorView & view)
{
    const detail::BaseVectorImpl & bvi
        = static_cast<const detail::BaseVectorImpl &>(view);
    const VectorView & vv = reinterpret_cast<const VectorView &>(bvi);
    return vv;
}

inline
VectorView & const_vector_view_cast (ConstVectorView & view)
{
    detail::BaseVectorImpl & bvi
        = static_cast<detail::BaseVectorImpl &>(view);
    VectorView & vv = reinterpret_cast<VectorView &>(bvi);
    return vv;
}


} } // end namespaces


#endif /*_NL_VECTOR_H_*/

/*
  Local Variables:
  mode:c++
  c-file-style:"stroustrup"
  c-file-offsets:((innamespace . 0)(inline-open . 0)(case-label . +))
  indent-tabs-mode:nil
  fill-column:99
  End:
*/
// vim: filetype=cpp:expandtab:shiftwidth=4:tabstop=8:softtabstop=4:fileencoding=utf-8:textwidth=99 :