How to get the maximum value of an Eigen tensor?
Asked Answered
R

1

6

I am trying to find the maximum value of an Eigen tensor. I know how to do this using Eigen matrices but obviously this is not working for a tensor:

Example

static const int nx = 4;  
static const int ny = 4; 
static const int nz = 4;

Eigen::Tensor<double, 3> Test(nx,ny,nz); 
Test.setZero();

Eigen::Tensor<double, 3> MaxTest(nx,ny,nz); 
MaxTest.setZero();  
MaxTest = Test.maxCoeff();

Usually for an Eigen matrix I would do the following:

MaxTest = Test.array().maxCoeff();

But this is not working, instead I get the error:

class "Eigen::Tensor<std::complex<double>, 3, 0, Eigen::DenseIndex>" has no member "maxCoeff"

Is there another way to get the maximum value of this tensor? Something equivalent to max(max(max(tensor))) in MATLAB.

Ravage answered 18/10, 2022 at 17:56 Comment(0)
A
2

You can use maximum(), see the documentation:

static const int nx = 4;  
static const int ny = 4; 
static const int nz = 4;

Eigen::Tensor<double, 3> MaxTest(nx,ny,nz); 
MaxTest.setZero();  
Eigen::Tensor<double, 0> MaxAsTensor = MaxTest.maximum();
double Max = MaxAsTensor(0);
std::cout << "Maximum = " << max_dbl << std::endl;
Affer answered 18/10, 2022 at 18:28 Comment(3)
So, this works with no errors Test.maximum(); but this does not double max = Test.maximum();Ravage
@Ravage Sorry, my mistake, maximum() returns something that yields a zero dimension tensor. You can then access its only element with (0). See my edited answer.Affer
I was looking for a way to extract the singleton from the zero-dimension tensor . Thanks for showing it! (although it feels strange to index into it).Huei

© 2022 - 2024 — McMap. All rights reserved.