www.pudn.com > Tensor_demo.zip > Tensor.cpp


/* Tensor.H                                 Version from  05/02/97 17:07PM  */ 
/* Contents ----------------------------------------------------------------** 
**									                                                        ** 
**  Class 	Tensor                                                          ** 
**																		                                      ** 
**--------------------------------------------------------------------------** 
**                                                                          ** 
** COPYRIGHT (C) 1997 by Melnikov Mike. All rights reserved.                ** 
** For any comments or suggestions mailto:zmike@andnow.ru                   ** 
**                                                                          ** 
** -------------------------------------------------------------------------*/ 
 
#include "stdafx.h" 
 
#include "Tensor.h" 
 
#ifdef _DEBUG 
#define new DEBUG_NEW 
#undef THIS_FILE                
static char THIS_FILE[] = __FILE__; 
#endif 
 
 
TensorFormatString&  TensorFormatString::operator =( TensorFormatString& str) 
{  
  operator=(str.getFormat());  
  //setm_beginSymbol(str.getm_beginSymbol()); 
  return *this; 
} 
 
TensorFormatString& TensorFormatString::operator =( const char* str )  
{  
  strcpy(m_format,str);  
  return *this; 
} 
 
TensorFormatString& TensorFormatString::operator =( const int  m_value ) 
{  
  m_format[0] = m_beginSymbol + m_value;  
  return *this; 
} 
 
int TensorFormatString::search(char symbol) 
{ 
  for(int i=strlen(m_format)-1; i >=0; i--) 
    if( m_format[i] == symbol) 
      return i; 
    return -1; 
} 
 
void convolution( TensorObject& result, TensorObject& tenA, TensorObject& tenB ) 
{ 
  if( result.getLevel() == 0 &&  tenA.getLevel() == 0 &&  tenB.getLevel() == 0 ) 
  { 
    result.internal_add(tenA,tenB); 
    return; 
  } 
   
  TensorFormatString *newFormatThis, *newFormatThat; 
  int changePosResult, changePosThat; 
  TensorObject* ten = NULL; 
  TensorFormatString newFormatResult(result.getFormat()), 
                     newFormatA(tenA.getFormat()), 
                     newFormatB(tenB.getFormat()); 
   
  if( tenA.getLevel() )                     
  { 
    ten = &tenA; 
    newFormatThis = &newFormatA; 
    newFormatThat = &newFormatB; 
    changePosThat   = newFormatThat->search(ten->getFormat()); 
    changePosResult = newFormatResult.search(ten->getFormat()); 
  } 
  else //if( tenB.getLevel() ) 
  { 
    ten = &tenB; 
    newFormatThis = &newFormatB; 
    newFormatThat = &newFormatA; 
     
    changePosThat   = newFormatThat->search(ten->getFormat()); 
    changePosResult = newFormatResult.search(ten->getFormat()); 
  } 
   
  for( int i=0; i < 2; i++ ) 
  { 
    if( changePosThat>=0 )   newFormatThat->setValue(changePosThat, i); 
    if( changePosResult>=0 ) newFormatResult.setValue(changePosResult, i); 
     
    newFormatThis->setValue(0,i); 
     
    convolution( result[newFormatResult], tenA[newFormatA], tenB[newFormatB]); 
  } 
}          
 
Tensor4 inverse(Tensor4& t) 
{ 
  assert(2==t.getDim()); 
  Tensor4 tmp(t.getDim()); 
  double opr; 
  int i,j,dim = t.getDim(); 
  for(i=0;i