-
Notifications
You must be signed in to change notification settings - Fork 7
/
DenseArray.h
556 lines (475 loc) · 21.9 KB
/
DenseArray.h
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
//=================================================================================================
/*!
// \file blaze_tensor/math/dense/DenseArray.h
// \brief Header file for utility functions for dense arrays
//
// Copyright (C) 2012-2018 Klaus Iglberger - All Rights Reserved
// Copyright (C) 2018-2019 Hartmut Kaiser - All Rights Reserved
//
// This file is part of the Blaze library. You can redistribute it and/or modify it under
// the terms of the New (Revised) BSD License. Redistribution and use in source and binary
// forms, with or without modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
// 3. Neither the names of the Blaze development group nor the names of its contributors
// may be used to endorse or promote products derived from this software without specific
// prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
// OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
// SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
// BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
// DAMAGE.
*/
//=================================================================================================
#ifndef _BLAZE_TENSOR_MATH_DENSE_DENSEARRAY_H_
#define _BLAZE_TENSOR_MATH_DENSE_DENSEARRAY_H_
//*************************************************************************************************
// Includes
//*************************************************************************************************
#include <blaze/math/Aliases.h>
#include <blaze/math/constraints/RequiresEvaluation.h>
#include <blaze/math/shims/Equal.h>
#include <blaze/math/shims/IsDefault.h>
#include <blaze/math/shims/IsNaN.h>
#include <blaze/math/shims/IsOne.h>
#include <blaze/math/shims/IsReal.h>
#include <blaze/math/shims/IsZero.h>
#include <blaze/math/typetraits/IsExpression.h>
#include <blaze/math/typetraits/IsRestricted.h>
#include <blaze/util/Assert.h>
#include <blaze/util/EnableIf.h>
#include <blaze/util/FalseType.h>
#include <blaze/util/mpl/If.h>
#include <blaze/util/TrueType.h>
#include <blaze/util/Types.h>
#include <blaze/util/typetraits/IsBuiltin.h>
#include <blaze/util/typetraits/IsNumeric.h>
#include <blaze/util/typetraits/RemoveCV.h>
#include <blaze/util/typetraits/RemoveReference.h>
// #include <blaze_tensor/math/expressions/DTensDTensAddExpr.h>
#include <blaze_tensor/math/expressions/DArrDArrEqualExpr.h>
#include <blaze_tensor/math/expressions/DArrDArrMapExpr.h>
// #include <blaze_tensor/math/expressions/DTensDTensMultExpr.h>
// #include <blaze_tensor/math/expressions/DTensDTensSchurExpr.h>
// #include <blaze_tensor/math/expressions/DTensDTensSubExpr.h>
// #include <blaze_tensor/math/expressions/DTensEvalExpr.h>
#include <blaze_tensor/math/expressions/DArrMapExpr.h>
#include <blaze_tensor/math/expressions/DTensNormExpr.h>
#include <blaze_tensor/math/expressions/DArrReduceExpr.h>
#include <blaze_tensor/math/expressions/DArrScalarDivExpr.h>
#include <blaze_tensor/math/expressions/DArrScalarMultExpr.h>
// #include <blaze_tensor/math/expressions/DTensSerialExpr.h>
#include <blaze_tensor/math/expressions/DenseArray.h>
#include <blaze_tensor/util/ArrayForEach.h>
namespace blaze {
//=================================================================================================
//
// GLOBAL OPERATORS
//
//=================================================================================================
//*************************************************************************************************
/*!\name DenseArray operators */
//@{
template< typename T1, typename T2 >
inline auto operator==( const DenseArray<T1>& arr, T2 scalar )
-> EnableIf_t< IsNumeric_v<T2>, bool >;
template< typename T1, typename T2 >
inline auto operator==( T1 scalar, const DenseArray<T2>& arr )
-> EnableIf_t< IsNumeric_v<T2>, bool >;
template< typename T1, typename T2 >
inline auto operator!=( const DenseArray<T1>& arr, T2 scalar )
-> EnableIf_t< IsNumeric_v<T2>, bool >;
template< typename T1, typename T2 >
inline auto operator!=( T1 scalar, const DenseArray<T2>& arr )
-> EnableIf_t< IsNumeric_v<T2>, bool >;
template< typename TT, typename ST >
inline auto operator*=( DenseArray<TT>& arr, ST scalar )
-> EnableIf_t< IsNumeric_v<ST>, TT& >;
template< typename TT, typename ST >
inline auto operator/=( DenseArray<TT>& arr, ST scalar )
-> EnableIf_t< IsNumeric_v<ST>, TT& >;
//@}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Equality operator for the comparison of a row-major dense array and a scalar value.
// \ingroup dense_tensor
//
// \param arr The left-hand side row-major dense array for the comparison.
// \param scalar The right-hand side scalar value for the comparison.
// \return \a true if all elements of the array are equal to the scalar, \a false if not.
//
// If all values of the array are equal to the scalar value, the equality test returns \a true,
// otherwise \a false. Note that this function can only be used with built-in, numerical data
// types!
*/
template< typename T1 // Type of the left-hand side dense array
, typename T2 > // Type of the right-hand side scalar
inline auto operator==( const DenseArray<T1>& arr, T2 scalar )
-> EnableIf_t< IsNumeric_v<T2>, bool >
{
using CT1 = CompositeType_t<T1>;
constexpr size_t N =
RemoveCV_t< RemoveReference_t< decltype( ~arr ) > >::num_dimensions;
// Evaluation of the dense array operand
CT1 A( ~arr );
// In order to compare the array and the scalar value, the data values of the lower-order
// data type are converted to the higher-order data type within the equal function.
return ArrayForEachGroupedAllOf(
( ~arr ).dimensions(), [&]( std::array< size_t, N > const& dims ) {
return equal( A( dims ), scalar );
} );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Equality operator for the comparison of a scalar value and a dense array.
// \ingroup dense_tensor
//
// \param scalar The left-hand side scalar value for the comparison.
// \param arr The right-hand side dense array for the comparison.
// \return \a true if all elements of the array are equal to the scalar, \a false if not.
//
// If all values of the array are equal to the scalar value, the equality test returns \a true,
// otherwise \a false. Note that this function can only be used with built-in, numerical data
// types!
*/
template< typename T1 // Type of the left-hand side scalar
, typename T2 > // Type of the right-hand side dense array
inline auto operator==( T1 scalar, const DenseArray<T2>& arr )
-> EnableIf_t< IsNumeric_v<T1>, bool >
{
return ( arr == scalar );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Inequality operator for the comparison of a dense array and a scalar value.
// \ingroup dense_tensor
//
// \param arr The left-hand side dense array for the comparison.
// \param scalar The right-hand side scalar value for the comparison.
// \return \a true if at least one element of the array is different from the scalar, \a false if not.
//
// If one value of the array is inequal to the scalar value, the inequality test returns \a true,
// otherwise \a false. Note that this function can only be used with built-in, numerical data
// types!
*/
template< typename T1 // Type of the left-hand side scalar
, typename T2 > // Type of the right-hand side dense array
inline auto operator!=( const DenseArray<T1>& arr, T2 scalar )
-> EnableIf_t< IsNumeric_v<T2>, bool >
{
return !( arr == scalar );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Inequality operator for the comparison of a scalar value and a dense array.
// \ingroup dense_tensor
//
// \param scalar The left-hand side scalar value for the comparison.
// \param arr The right-hand side dense array for the comparison.
// \return \a true if at least one element of the array is different from the scalar, \a false if not.
//
// If one value of the array is inequal to the scalar value, the inequality test returns \a true,
// otherwise \a false. Note that this function can only be used with built-in, numerical data
// types!
*/
template< typename T1 // Type of the left-hand side scalar
, typename T2 > // Type of the right-hand side dense array
inline auto operator!=( T1 scalar, const DenseArray<T2>& arr )
-> EnableIf_t< IsNumeric_v<T1>, bool >
{
return !( arr == scalar );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Multiplication assignment operator for the multiplication of a dense array and
// a scalar value (\f$ A*=s \f$).
// \ingroup dense_tensor
//
// \param arr The left-hand side dense array for the multiplication.
// \param scalar The right-hand side scalar value for the multiplication.
// \return Reference to the left-hand side dense array.
// \exception std::invalid_argument Invalid scaling of restricted array.
//
// In case the array \a TT is restricted and the assignment would violate an invariant of the
// array, a \a std::invalid_argument exception is thrown.
*/
template< typename TT // Type of the left-hand side dense array
, typename ST > // Data type of the right-hand side scalar
inline auto operator*=( DenseArray<TT>& arr, ST scalar )
-> EnableIf_t< IsNumeric_v<ST>, TT& >
{
if( IsRestricted_v<TT> ) {
constexpr size_t N =
RemoveCV_t< RemoveReference_t< decltype( ~arr ) > >::num_dimensions;
std::array< size_t, N > dims{};
if( !tryMult( ~arr, dims, (~arr).dimensions(), scalar ) ) {
BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted array" );
}
}
decltype(auto) left( derestrict( ~arr ) );
smpAssign( left, left * scalar );
BLAZE_INTERNAL_ASSERT( isIntact( ~arr ), "Invariant violation detected" );
return ~arr;
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Multiplication assignment operator for the multiplication of a temporary dense array
// and a scalar value (\f$ A*=s \f$).
// \ingroup dense_tensor
//
// \param arr The left-hand side temporary dense array for the multiplication.
// \param scalar The right-hand side scalar value for the multiplication.
// \return Reference to the left-hand side dense array.
// \exception std::invalid_argument Invalid scaling of restricted array.
//
// In case the array \a TT is restricted and the assignment would violate an invariant of the
// array, a \a std::invalid_argument exception is thrown.
*/
template< typename TT // Type of the left-hand side dense array
, typename ST > // Data type of the right-hand side scalar
inline auto operator*=( DenseArray<TT>&& arr, ST scalar )
-> EnableIf_t< IsNumeric_v<ST>, TT& >
{
return operator*=( ~arr, scalar );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Division assignment operator for the division of a dense array by a scalar value
// (\f$ A/=s \f$).
// \ingroup dense_tensor
//
// \param arr The left-hand side dense array for the division.
// \param scalar The right-hand side scalar value for the division.
// \return Reference to the left-hand side dense array.
// \exception std::invalid_argument Invalid scaling of restricted array.
//
// In case the array \a TT is restricted and the assignment would violate an invariant of the
// array, a \a std::invalid_argument exception is thrown.
//
// \note A division by zero is only checked by an user assert.
*/
template< typename TT // Type of the left-hand side dense array
, typename ST > // Data type of the right-hand side scalar
inline auto operator/=( DenseArray<TT>& arr, ST scalar )
-> EnableIf_t< IsNumeric_v<ST>, TT& >
{
// BLAZE_CONSTRAINT_MUST_NOT_BE_UNITRIANGULAR_TENSOR_TYPE( TT );
BLAZE_USER_ASSERT( !isZero( scalar ), "Division by zero detected" );
if( IsRestricted_v<TT> ) {
constexpr size_t N =
RemoveCV_t< RemoveReference_t< decltype( ~arr ) > >::num_dimensions;
std::array< size_t, N > dims{};
if( !tryDiv( ~arr, dims, (~arr).dimensions(), scalar ) ) {
BLAZE_THROW_INVALID_ARGUMENT( "Invalid scaling of restricted array" );
}
}
decltype(auto) left( derestrict( ~arr ) );
smpAssign( left, left / scalar );
BLAZE_INTERNAL_ASSERT( isIntact( ~arr ), "Invariant violation detected" );
return ~arr;
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Division assignment operator for the division of a temporary dense array by a scalar
// value (\f$ A/=s \f$).
// \ingroup dense_tensor
//
// \param arr The left-hand side temporary dense array for the division.
// \param scalar The right-hand side scalar value for the division.
// \return Reference to the left-hand side dense array.
// \exception std::invalid_argument Invalid scaling of restricted array.
//
// In case the array \a TT is restricted and the assignment would violate an invariant of the
// array, a \a std::invalid_argument exception is thrown.
//
// \note A division by zero is only checked by an user assert.
*/
template< typename TT // Type of the left-hand side dense array
, typename ST > // Data type of the right-hand side scalar
inline auto operator/=( DenseArray<TT>&& arr, ST scalar )
-> EnableIf_t< IsNumeric_v<ST>, TT& >
{
return operator/=( ~arr, scalar );
}
//*************************************************************************************************
//=================================================================================================
//
// GLOBAL FUNCTIONS
//
//=================================================================================================
//*************************************************************************************************
/*!\name DenseArray functions */
//@{
template< typename TT >
bool isnan( const DenseArray<TT>& dm );
// template< bool RF, typename MT >
// bool isSymmetric( const DenseArray<MT>& dm );
//
// template< bool RF, typename MT >
// bool isHermitian( const DenseArray<MT>& dm );
template< bool RF, typename MT >
bool isUniform( const DenseArray<MT>& dm );
// template< bool RF, typename MT >
// bool isLower( const DenseArray<MT>& dm );
//
// template< bool RF, typename MT >
// bool isUniLower( const DenseArray<MT>& dm );
//
// template< bool RF, typename MT >
// bool isStrictlyLower( const DenseArray<MT>& dm );
//
// template< bool RF, typename MT >
// bool isUpper( const DenseArray<MT>& dm );
//
// template< bool RF, typename MT >
// bool isUniUpper( const DenseArray<MT>& dm );
//
// template< bool RF, typename MT >
// bool isStrictlyUpper( const DenseArray<MT>& dm );
//
// template< bool RF, typename MT >
// bool isDiagonal( const DenseArray<MT>& dm );
//
// template< bool RF, typename MT >
// bool isIdentity( const DenseArray<MT>& dm );
template< typename MT >
auto softmax( const DenseArray<MT>& dm );
//@}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Checks the given dense array for not-a-number elements.
// \ingroup dense_tensor
//
// \param dm The array to be checked for not-a-number elements.
// \return \a true if at least one element of the array is not-a-number, \a false otherwise.
//
// This function checks the dense array for not-a-number (NaN) elements. If at least one
// element of the array is not-a-number, the function returns \a true, otherwise it returns
// \a false.
\code
blaze::DynamicArray<double> A( 5UL, 3UL, 4UL );
// ... Initialization
if( isnan( A ) ) { ... }
\endcode
// Note that this function only works for arrays with floating point elements. The attempt to
// use it for a array with a non-floating point element type results in a compile time error.
*/
template< typename TT > // Type of the dense array
bool isnan( const DenseArray<TT>& dm )
{
using CT = CompositeType_t<TT>;
constexpr size_t N =
RemoveCV_t< RemoveReference_t< decltype( ~dm ) > >::num_dimensions;
CT A( ~dm ); // Evaluation of the dense array operand
return ArrayForEachGroupedAnyOf(
( ~dm ).dimensions(), [&]( std::array< size_t, N > const& dims ) {
return isnan( A( dims ) );
} );
}
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Computes the softmax function for the given dense array.
// \ingroup dense_tensor
//
// \param dm The given dense array for the softmax computation.
// \return The resulting array.
//
// This function computes the softmax function (i.e. the normalized exponential function) for
// the given dense array \a dm (see also https://en.wikipedia.org/wiki/Softmax_function). The
// resulting dense array consists of real values in the range (0..1], which add up to 1.
*/
template< typename MT > // Type of the dense array
auto softmax( const DenseArray<MT>& dm )
{
auto tmp( evaluate( exp( ~dm ) ) );
const auto scalar( sum( ~tmp ) );
tmp /= scalar;
return tmp;
}
//*************************************************************************************************
//*************************************************************************************************
/*! \cond BLAZE_INTERNAL */
/*!\brief Checks if the given row-major general dense array is a uniform array.
// \ingroup dense_tensor
//
// \param dm The dense array to be checked.
// \return \a true if the array is a uniform array, \a false if not.
*/
template< bool RF // Relaxation flag
, typename MT > // Type of the dense array
bool isUniform_backend( const DenseArray<MT>& dm )
{
BLAZE_CONSTRAINT_MUST_NOT_REQUIRE_EVALUATION( MT );
#if defined(BLAZE_INTERNAL_ASSERTION)
ArrayDimForEach( ( ~dm ).dimensions(), [&]( size_t, size_t dim ) {
BLAZE_INTERNAL_ASSERT( dim != 0, "Invalid array dimension detected" );
} );
#endif
constexpr size_t N =
RemoveCV_t< RemoveReference_t< decltype( ~dm ) > >::num_dimensions;
std::array< size_t, N > dims{};
const auto& cmp( (~dm)( dims ) );
return ArrayForEachGroupedAllOf( ( ~dm ).dimensions(),
[&]( std::array< size_t, N > const& dims ) {
return equal< RF >( ( ~dm )( dims ), cmp );
} );
}
/*! \endcond */
//*************************************************************************************************
//*************************************************************************************************
/*!\brief Checks if the given dense array is a uniform array.
// \ingroup dense_tensor
//
// \param dm The dense array to be checked.
// \return \a true if the array is a uniform array, \a false if not.
//
// This function checks if the given dense array is a uniform array. The array is considered
// to be uniform if all its elements are identical. The following code example demonstrates the
// use of the function:
\code
blaze::DynamicArray<int,blaze::rowMajor> A, B;
// ... Initialization
if( isUniform( A ) ) { ... }
\endcode
// Optionally, it is possible to switch between strict semantics (blaze::strict) and relaxed
// semantics (blaze::relaxed):
\code
if( isUniform<relaxed>( A ) ) { ... }
\endcode
// It is also possible to check if a array expression results is a uniform array:
\code
if( isUniform( A * B ) ) { ... }
\endcode
// However, note that this might require the complete evaluation of the expression, including
// the generation of a temporary array.
*/
template< bool RF // Relaxation flag
, typename MT > // Type of the dense array
bool isUniform( const DenseArray<MT>& dm )
{
if( IsUniform_v< MT > ||
ArrayDimAnyOf( ( ~dm ).dimensions(),
[]( size_t, size_t dim ) { return dim == 0; } ) ||
ArrayDimAllOf( ( ~dm ).dimensions(),
[]( size_t, size_t dim ) { return dim == 1; } ) ) {
return true;
}
if( IsUniTriangular_v<MT> )
return false;
CompositeType_t<MT> A( ~dm ); // Evaluation of the dense array operand
return isUniform_backend<RF>( A );
}
//*************************************************************************************************
} // namespace blaze
#endif