📄 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<dim;i++)
{
for(j=0;j<dim;j++)
{
opr=t[i][0][j][0] * t[i][1][j][1]-t[i][0][j][1]*t[i][1][j][0];
if ( opr)
{
tmp[i][0][j][0] = t[i][1][j][1]/opr;
tmp[i][1][j][1] = t[i][0][j][0]/opr;
tmp[i][1][j][0] = t[i][0][j][1]/opr;
tmp[i][0][j][1] = t[i][1][j][0]/opr;
}
else
{
tmp[i][0][j][0]=0;
tmp[i][1][j][1]=0;
tmp[i][1][j][0]=0;
tmp[i][0][j][1]=0;
}
}
}
return(tmp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -