MiniTensor Version of the Day
Loading...
Searching...
No Matches
MiniTensor_Norms.h
Go to the documentation of this file.
1// @HEADER
2// *****************************************************************************
3// MiniTensor Package
4//
5// Copyright 2016 NTESS and the MiniTensor contributors.
6// SPDX-License-Identifier: BSD-3-Clause
7// *****************************************************************************
8// @HEADER
9
10#if !defined(MiniTensor_Norms_h)
11#define MiniTensor_Norms_h
12
13// Scalar measures of tensors: norms, invariants, argmax queries.
14#include "MiniTensor_Tensor.h"
15
16namespace minitensor {
17
20
25template<typename T, Index N>
27T
28norm(Tensor<T, N> const & A);
29
34template<typename T, Index N>
36T
37norm_1(Tensor<T, N> const & A);
38
43template<typename T, Index N>
45T
47
55template<typename T, Index N>
58subtensor(Tensor<T, N> const & A, Index const i, Index const j);
59
64template<typename T, Index N>
66T
67det(Tensor<T, N> const & A);
68
73template<typename T, Index N>
75T
76trace(Tensor<T, N> const & A);
77
82template<typename T, Index N>
84T
85I1(Tensor<T, N> const & A);
86
91template<typename T, Index N>
93T
94I2(Tensor<T, N> const & A);
95
100template<typename T, Index N>
102T
103I3(Tensor<T, N> const & A);
104
110template<typename T, Index N>
112T
114
120template <typename T, Index N>
121std::pair<Index, Index> arg_max_abs(Tensor<T, N> const &A);
122
128template <typename T, Index N>
129std::pair<Index, Index> arg_max_off_diagonal(Tensor<T, N> const &A);
130
131//
132// R^N tensor Frobenius norm
133// \return \f$ \sqrt{A:A} \f$
134//
135template<typename T, Index N>
137T
139{
140 Index const
141 dimension = A.get_dimension();
142
143 T
144 s = 0.0;
145
146 switch (dimension) {
147
148 default:
149 s = norm_f_square(A);
150 break;
151
152 case 3:
153 s+= A(0,0)*A(0,0) + A(0,1)*A(0,1) + A(0,2)*A(0,2);
154 s+= A(1,0)*A(1,0) + A(1,1)*A(1,1) + A(1,2)*A(1,2);
155 s+= A(2,0)*A(2,0) + A(2,1)*A(2,1) + A(2,2)*A(2,2);
156 break;
157
158 case 2:
159 s+= A(0,0)*A(0,0) + A(0,1)*A(0,1);
160 s+= A(1,0)*A(1,0) + A(1,1)*A(1,1);
161 break;
162
163 case 1:
164 s+= A(0,0)*A(0,0);
165 break;
166
167 }
168
169 if (s > 0.0) return std::sqrt(s);
170 return 0.0;
171
172}
173
174//
175// R^N tensor 1-norm
176// \return \f$ \max_{j \in {0,\cdots,N}}\Sigma_{i=0}^N |A_{ij}| \f$
177//
178template<typename T, Index N>
180T
182{
183 Index const
184 dimension = A.get_dimension();
185
187 v(dimension);
188
189 T
190 s = 0.0;
191
192 switch (dimension) {
193
194 default:
195
196 for (Index i = 0; i < dimension; ++i) {
197 T t = 0.0;
198 for (Index j = 0; j < dimension; ++j) {
199 t += minitensor::abs(A(j, i));
200 }
201 v(i) = t;
202 }
203
204 for (Index i = 0; i < dimension; ++i) {
205 s = max(s, v(i));
206 }
207 break;
208
209 case 3:
210 v(0) = minitensor::abs(A(0, 0)) + minitensor::abs(A(1, 0)) + minitensor::abs(A(2, 0));
211 v(1) = minitensor::abs(A(0, 1)) + minitensor::abs(A(1, 1)) + minitensor::abs(A(2, 1));
212 v(2) = minitensor::abs(A(0, 2)) + minitensor::abs(A(1, 2)) + minitensor::abs(A(2, 2));
213
214 s = max(max(v(0), v(1)), v(2));
215 break;
216
217 case 2:
218 v(0) = minitensor::abs(A(0, 0)) + minitensor::abs(A(1, 0));
219 v(1) = minitensor::abs(A(0, 1)) + minitensor::abs(A(1, 1));
220
221 s = max(v(0), v(1));
222 break;
223
224 case 1:
225 s = minitensor::abs(A(0, 0));
226 break;
227 }
228
229 return s;
230}
231
232//
233// R^N tensor infinity-norm
234// \return \f$ \max_{i \in {0,\cdots,N}}\Sigma_{j=0}^N |A_{ij}| \f$
235//
236template<typename T, Index N>
238T
240{
241 Index const
242 dimension = A.get_dimension();
243
245 v(dimension);
246
247 T s = 0.0;
248
249 switch (dimension) {
250
251 default:
252 for (Index i = 0; i < dimension; ++i) {
253 T t = 0.0;
254 for (Index j = 0; j < dimension; ++j) {
255 t += minitensor::abs(A(i, j));
256 }
257 v(i) = t;
258 }
259
260 for (Index i = 0; i < dimension; ++i) {
261 s = max(s, v(i));
262 }
263 break;
264
265 case 3:
266 v(0) = minitensor::abs(A(0, 0)) + minitensor::abs(A(0, 1)) + minitensor::abs(A(0, 2));
267 v(1) = minitensor::abs(A(1, 0)) + minitensor::abs(A(1, 1)) + minitensor::abs(A(1, 2));
268 v(2) = minitensor::abs(A(2, 0)) + minitensor::abs(A(2, 1)) + minitensor::abs(A(2, 2));
269
270 s = max(max(v(0), v(1)), v(2));
271 break;
272
273 case 2:
274 v(0) = minitensor::abs(A(0, 0)) + minitensor::abs(A(0, 1));
275 v(1) = minitensor::abs(A(1, 0)) + minitensor::abs(A(1, 1));
276
277 s = max(v(0), v(1));
278 break;
279
280 case 1:
281 s = minitensor::abs(A(0, 0));
282 break;
283
284 }
285
286 return s;
287}
288
289//
290// R^N determinant
291// Laplace expansion. Warning: no pivoting.
292// Casual use only. Use Teuchos LAPACK interface for
293// more efficient and robust techniques.
294// \param A tensor
295// \return \f$ \det A \f$
296//
297template<typename T, Index N>
299T
301{
302 Index const
303 dimension = A.get_dimension();
304
305 T
306 s = 0.0;
307
308 // The dimension is a run-time quantity, so every branch of the
309 // dispatch below is compiled even for a statically sized tensor that
310 // can never reach it. The branches for the larger dimensions index
311 // past the storage of the smaller ones, which the compiler reports as
312 // an out-of-bounds access, so they are discarded here instead.
313
314 if (dimension == 1) {
315 s = A(0,0);
316 return s;
317 }
318
319 if constexpr (dimension_reachable<N, 2>) {
320 if (dimension == 2) {
321 s = A(0,0) * A(1,1) - A(1,0) * A(0,1);
322 return s;
323 }
324 }
325
326 if constexpr (dimension_reachable<N, 3>) {
327 if (dimension == 3) {
328 s = -A(0,2)*A(1,1)*A(2,0) + A(0,1)*A(1,2)*A(2,0) +
329 A(0,2)*A(1,0)*A(2,1) - A(0,0)*A(1,2)*A(2,1) -
330 A(0,1)*A(1,0)*A(2,2) + A(0,0)*A(1,1)*A(2,2);
331 return s;
332 }
333 }
334
335 // Laplace expansion along the first column: the cofactor of A(i,0) has
336 // the sign (-1)^i.
337 if constexpr (dimension_reachable<N, 4>) {
338 int sign = 1;
339 for (Index i = 0; i < dimension; ++i) {
340 const T d = det(subtensor(A, i, 0));
341 s += sign * d * A(i, 0);
342 sign *= -1;
343 }
344 }
345
346 return s;
347}
348
349//
350// R^N trace
351// \param A tensor
352// \return \f$ A:I \f$
353//
354template<typename T, Index N>
356T
358{
359 Index const
360 dimension = A.get_dimension();
361
362 T s = 0.0;
363
364 switch (dimension) {
365
366 default:
367 for (Index i = 0; i < dimension; ++i) {
368 s += A(i,i);
369 }
370 break;
371
372 case 3:
373 s = A(0,0) + A(1,1) + A(2,2);
374 break;
375
376 case 2:
377 s = A(0,0) + A(1,1);
378 break;
379
380 case 1:
381 s = A(0,0);
382 break;
383
384 }
385
386 return s;
387}
388
389//
390// R^N first invariant, trace
391// \param A tensor
392// \return \f$ I_A = A:I \f$
393//
394template<typename T, Index N>
396T
397I1(Tensor<T, N> const & A)
398{
399 return trace(A);
400}
401
402//
403// R^N second invariant
404// \param A tensor
405// \return \f$ II_A \f$
406//
407template<typename T, Index N>
409T
410I2(Tensor<T, N> const & A)
411{
412 Index const
413 dimension = A.get_dimension();
414
415 T
416 s = 0.0;
417
418 T const
419 trA = trace(A);
420
421 switch (dimension) {
422
423 default:
424#ifdef KOKKOS_ENABLE_CUDA
425 Kokkos::abort("I2 for N > 3 not implemented.");
426 return T();
427#else
428 std::cerr << "I2 for N > 3 not implemented." << std::endl;
429 exit(1);
430#endif
431 break;
432
433 case 3:
434 s = 0.5 * (trA*trA - A(0,0)*A(0,0) - A(1,1)*A(1,1) - A(2,2)*A(2,2)) -
435 A(0,1)*A(1,0) - A(0,2)*A(2,0) - A(1,2)*A(2,1);
436 break;
437
438 case 2:
439 s = - det(A);
440 break;
441
442 case 1:
443 s = 0.0;
444 break;
445
446 }
447
448 return s;
449}
450
451//
452// R^N third invariant
453// \param A tensor
454// \return \f$ III_A \f$
455//
456template<typename T, Index N>
458T
459I3(Tensor<T, N> const & A)
460{
461 Index const
462 dimension = A.get_dimension();
463
464 T
465 s = 0.0;
466
467 switch (dimension) {
468
469 default:
470#ifdef KOKKOS_ENABLE_CUDA
471 Kokkos::abort("I3 for N > 3 not implemented.");
472 return T();
473#else
474 std::cerr << "I3 for N > 3 not implemented." << std::endl;
475 exit(1);
476#endif
477 break;
478
479 case 3:
480 s = det(A);
481 break;
482
483 case 2:
484 s = 0.0;
485 break;
486
487 case 1:
488 s = 0.0;
489 break;
490
491 }
492
493 return s;
494}
495
496//
497// R^N Subtensor
498// \param A tensor
499// \param i index
500// \param j index
501// \return Subtensor with i-row and j-col deleted.
502//
503template<typename T, Index N>
505Tensor<T, N>
506subtensor(Tensor<T, N> const & A, Index const i, Index const j)
507{
508 Index const
509 dimension = A.get_dimension();
510
511 assert(i < dimension);
512 assert(j < dimension);
513
515 B(dimension - 1);
516
517 Index p = 0;
518 for (Index m = 0; m < dimension; ++m) {
519 if (m == i) continue;
520 Index q = 0;
521 for (Index n = 0; n < dimension; ++n) {
522 if (n == j) continue;
523 B(p, q) = A(m, n);
524 ++q;
525 }
526 ++p;
527 }
528
529 return B;
530}
531
532//
533// R^N off-diagonal norm. Useful for SVD and other algorithms
534// that rely on Jacobi-type procedures.
535// \param A
536// \return \f$ \sqrt(\sum_i \sum_{j, j\neq i} a_{ij}^2) \f$
537//
538template<typename T, Index N>
540T
542{
543 Index const
544 dimension = A.get_dimension();
545
546 T
547 s = 0.0;
548
549 switch (dimension) {
550
551 default:
552 for (Index i = 0; i < dimension; ++i) {
553 for (Index j = 0; j < dimension; ++j) {
554 if (i != j) s += A(i,j)*A(i,j);
555 }
556 }
557 break;
558
559 case 3:
560 s = A(0,1)*A(0,1) + A(0,2)*A(0,2) + A(1,2)*A(1,2) +
561 A(1,0)*A(1,0) + A(2,0)*A(2,0) + A(2,1)*A(2,1);
562 break;
563
564 case 2:
565 s = A(0,1)*A(0,1) + A(1,0)*A(1,0);
566 break;
567
568 case 1:
569 s = 0.0;
570 break;
571
572 }
573 return std::sqrt(s);
574}
575
576//
577// R^N arg max abs. Useful for inverse and other algorithms
578// that rely on Jacobi-type procedures.
579// \param A
580// \return \f$ (p,q) = arg max_{i,j} |a_{ij}| \f$
581//
582template <typename T, Index N>
583std::pair<Index, Index> arg_max_abs(Tensor<T, N> const &A) {
584
585 Index p = 0;
586 Index q = 0;
587
588 T
589 s = std::abs(A(p,q));
590
591 Index const
592 dimension = A.get_dimension();
593
594 for (Index i = 0; i < dimension; ++i) {
595 for (Index j = 0; j < dimension; ++j) {
596 if (std::abs(A(i,j)) > s) {
597 p = i;
598 q = j;
599 s = std::abs(A(i,j));
600 }
601 }
602 }
603
604 return std::make_pair(p,q);
605}
606
607//
608// R^N arg max off-diagonal. Useful for SVD and other algorithms
609// that rely on Jacobi-type procedures.
610// \param A
611// \return \f$ (p,q) = arg max_{i \neq j} |a_{ij}| \f$
612//
613template <typename T, Index N>
614std::pair<Index, Index> arg_max_off_diagonal(Tensor<T, N> const &A) {
615 Index p = 0;
616 Index q = 1;
617
618 T s = std::abs(A(p,q));
619
620 Index const
621 dimension = A.get_dimension();
622
623 for (Index i = 0; i < dimension; ++i) {
624 for (Index j = 0; j < dimension; ++j) {
625 if (i != j && std::abs(A(i,j)) > s) {
626 p = i;
627 q = j;
628 s = std::abs(A(i,j));
629 }
630 }
631 }
632
633 return std::make_pair(p,q);
634}
635
637} // namespace minitensor
638
639#endif // MiniTensor_Norms_h
#define KOKKOS_INLINE_FUNCTION
KOKKOS_INLINE_FUNCTION Index get_dimension() const
KOKKOS_INLINE_FUNCTION T norm_f_square(TensorBase< T, ST > const &X)
KOKKOS_INLINE_FUNCTION T I1(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION Tensor< T, N > subtensor(Tensor< T, N > const &A, Index const i, Index const j)
KOKKOS_INLINE_FUNCTION T norm(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T det(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T norm_off_diagonal(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T norm_1(Tensor< T, N > const &A)
std::pair< Index, Index > arg_max_abs(Tensor< T, N > const &A)
std::pair< Index, Index > arg_max_off_diagonal(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T I3(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T norm_infinity(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T I2(Tensor< T, N > const &A)
KOKKOS_INLINE_FUNCTION T trace(Tensor< T, N > const &A)
constexpr bool dimension_reachable
KOKKOS_INLINE_FUNCTION T abs(T const &a)
KOKKOS_INLINE_FUNCTION T max(T const &a, T const &b)
uint32_t Index
Indexing type.