idk why these stuffs get stashed for so long and I didn't ever commit them
This commit is contained in:
130
Chapter4/MatrixTrans/MatrixTrans.cpp
Normal file
130
Chapter4/MatrixTrans/MatrixTrans.cpp
Normal file
@@ -0,0 +1,130 @@
|
||||
#include<iostream>//cout,cin
|
||||
using namespace std;
|
||||
|
||||
|
||||
struct MNode //三元组结点
|
||||
{
|
||||
int i, j; //行号,列号
|
||||
int e; //非零元
|
||||
};
|
||||
|
||||
|
||||
struct TSMatrix
|
||||
{
|
||||
int mu, nu, tu; // 行数、列数、非零元个数
|
||||
MNode* data; // 三元组表
|
||||
};
|
||||
|
||||
TSMatrix MCreate(int d[][3], int m, int n, int k) //由三元组的二维数组生成行向量稀疏存储矩阵
|
||||
{
|
||||
TSMatrix M = { m,n,k,NULL };
|
||||
if (k != 0)
|
||||
M.data = new MNode[k];
|
||||
for (int i = 0; i < k; i++)
|
||||
{
|
||||
M.data[i].i = d[i][0];
|
||||
M.data[i].j = d[i][1];
|
||||
M.data[i].e = d[i][2];
|
||||
}
|
||||
return M;
|
||||
}//
|
||||
|
||||
|
||||
void MDisp(TSMatrix a) //矩阵显示
|
||||
{
|
||||
MNode p;
|
||||
int i, j, k = 0, c = 0;
|
||||
p = a.data[k];
|
||||
for (i = 0; i < a.mu; i++)
|
||||
{
|
||||
for (j = 0; j < a.nu; j++)
|
||||
{
|
||||
if (k < a.tu && p.i == i && p.j == j)
|
||||
{
|
||||
cout << '\t' << p.e;
|
||||
k++;
|
||||
if (k < a.tu) p = a.data[k];
|
||||
}
|
||||
else
|
||||
{
|
||||
cout << '\t' << c;
|
||||
}
|
||||
}//for
|
||||
cout << endl;
|
||||
cout << endl;
|
||||
}//for
|
||||
}//MatrixDisp
|
||||
|
||||
//算法4.1 转置算法一
|
||||
void MatrixTrans_1(TSMatrix A, TSMatrix& B)
|
||||
{//
|
||||
int col, p, q;
|
||||
B.mu = A.nu; B.nu = A.mu; B.tu = A.tu; //B的行数、列数、非零元个数分别等于A的列数、行数和非零元个数
|
||||
if (B.tu) // 矩阵B有非零元
|
||||
{
|
||||
q = 0;
|
||||
for (col = 0; col < A.nu; ++col) // 按列扫描A矩阵
|
||||
for (p = 0; p < A.tu; ++p)
|
||||
if (A.data[p].j == col) // col列有非零元
|
||||
{
|
||||
B.data[q].i = A.data[p].j; // 交换行、列号,生成转置元素
|
||||
B.data[q].j = A.data[p].i;
|
||||
B.data[q].e = A.data[p].e;
|
||||
++q;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
//算法4.2 快速转置
|
||||
void MatrixTrans_2(TSMatrix A, TSMatrix& B)
|
||||
{
|
||||
int col, i, k, q;
|
||||
int* num, * cpot;
|
||||
B.mu = A.nu; B.nu = A.mu; B.tu = A.tu; //B的行数、列数、非零元个数分别等于A的列数、行数和非零元个数
|
||||
num = new int[B.nu];
|
||||
cpot = new int[B.nu];
|
||||
if (B.tu) // 非零元个数不为零,实施转置
|
||||
{
|
||||
for (col = 0; col < A.nu; col++) //A中每一列非零元个数初始化为0
|
||||
num[col] = 0;
|
||||
for (i = 0; i < A.tu; i++) //求矩阵A中每一列非零元个数
|
||||
num[A.data[i].j]++;
|
||||
cpot[0] = 0; //A中第0列首个非零元在B中的下标
|
||||
for (col = 1; col <= A.nu; ++col) // 求A中每一列首个非零元在B中的下标
|
||||
cpot[col] = cpot[col - 1] + num[col - 1];
|
||||
for (k = 0; k < A.tu; ++k) //扫描A的三元组表
|
||||
{
|
||||
col = A.data[k].j; //当前三元组列号
|
||||
q = cpot[col]; //当前三元组在B中的下标
|
||||
B.data[q].i = A.data[k].j;
|
||||
B.data[q].j = A.data[k].i;
|
||||
B.data[q].e = A.data[k].e;
|
||||
cpot[col]++; // 预置同一列下一个三元组在B中的下标
|
||||
}//for
|
||||
}//if
|
||||
}//
|
||||
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
TSMatrix ma, mb1, mb2;
|
||||
int m1 = 4, n1 = 6, k1 = 6; //被乘矩阵行数,列数,非零元个数
|
||||
int da[6][3] = { {0,2,11},{0,4,12},{1,3,22},
|
||||
{2,1,31}, {2,3,32},{3,0,41} }; //ma阵的非零元
|
||||
ma = MCreate(da, m1, n1, k1); //生成三元组顺序存储的ma阵
|
||||
cout << "ma=" << endl;
|
||||
MDisp(ma); //显示ma阵
|
||||
mb1.data = new MNode[ma.tu];
|
||||
mb2.data = new MNode[ma.tu];
|
||||
cout << "方法一:直接取,顺序存" << endl; //用方法一转置矩阵
|
||||
MatrixTrans_1(ma, mb1);
|
||||
cout << "mb=" << endl; //显示转置矩阵
|
||||
MDisp(mb1);
|
||||
cout << "方法二:顺序取,直接存" << endl; //用方法二转置矩阵
|
||||
MatrixTrans_2(ma, mb2);
|
||||
cout << "mb=" << endl; //显示转置矩阵
|
||||
MDisp(mb2);
|
||||
}//
|
||||
135
Chapter4/MatrixTrans/MatrixTrans.vcxproj
Normal file
135
Chapter4/MatrixTrans/MatrixTrans.vcxproj
Normal file
@@ -0,0 +1,135 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup Label="ProjectConfigurations">
|
||||
<ProjectConfiguration Include="Debug|Win32">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|Win32">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Win32</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Debug|x64">
|
||||
<Configuration>Debug</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
<ProjectConfiguration Include="Release|x64">
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>x64</Platform>
|
||||
</ProjectConfiguration>
|
||||
</ItemGroup>
|
||||
<PropertyGroup Label="Globals">
|
||||
<VCProjectVersion>17.0</VCProjectVersion>
|
||||
<Keyword>Win32Proj</Keyword>
|
||||
<ProjectGuid>{ae81732d-d011-4a36-9d89-0c258adb5627}</ProjectGuid>
|
||||
<RootNamespace>MatrixTrans</RootNamespace>
|
||||
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>true</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
|
||||
<ConfigurationType>Application</ConfigurationType>
|
||||
<UseDebugLibraries>false</UseDebugLibraries>
|
||||
<PlatformToolset>v143</PlatformToolset>
|
||||
<WholeProgramOptimization>true</WholeProgramOptimization>
|
||||
<CharacterSet>Unicode</CharacterSet>
|
||||
</PropertyGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
|
||||
<ImportGroup Label="ExtensionSettings">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="Shared">
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
|
||||
</ImportGroup>
|
||||
<PropertyGroup Label="UserMacros" />
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>WIN32;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
<ClCompile>
|
||||
<WarningLevel>Level3</WarningLevel>
|
||||
<FunctionLevelLinking>true</FunctionLevelLinking>
|
||||
<IntrinsicFunctions>true</IntrinsicFunctions>
|
||||
<SDLCheck>true</SDLCheck>
|
||||
<PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
|
||||
<ConformanceMode>true</ConformanceMode>
|
||||
</ClCompile>
|
||||
<Link>
|
||||
<SubSystem>Console</SubSystem>
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="MatrixTrans.cpp" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
|
||||
<ImportGroup Label="ExtensionTargets">
|
||||
</ImportGroup>
|
||||
</Project>
|
||||
22
Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters
Normal file
22
Chapter4/MatrixTrans/MatrixTrans.vcxproj.filters
Normal file
@@ -0,0 +1,22 @@
|
||||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<ItemGroup>
|
||||
<Filter Include="源文件">
|
||||
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
|
||||
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="头文件">
|
||||
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
|
||||
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
|
||||
</Filter>
|
||||
<Filter Include="资源文件">
|
||||
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
|
||||
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
|
||||
</Filter>
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<ClCompile Include="MatrixTrans.cpp">
|
||||
<Filter>源文件</Filter>
|
||||
</ClCompile>
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
Reference in New Issue
Block a user