added cramer alg to solve lgs
This commit is contained in:
parent
dd17162f29
commit
6a5f8113ed
17 changed files with 1039 additions and 739 deletions
1175
versuch07/.cproject
1175
versuch07/.cproject
File diff suppressed because it is too large
Load diff
|
@ -1,70 +1,82 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Versuch07</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
||||
<triggers>clean,full,incremental,</triggers>
|
||||
<arguments>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.contents</key>
|
||||
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.buildLocation</key>
|
||||
<value>${workspace_loc:/Versuch07/Debug}</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
|
||||
<value>false</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.append_environment</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>?name?</key>
|
||||
<value></value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.stopOnError</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.buildArguments</key>
|
||||
<value></value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.buildCommand</key>
|
||||
<value>make</value>
|
||||
</dictionary>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.cdt.core.ccnature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
|
||||
<nature>org.eclipse.cdt.core.cnature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>Versuch07</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.genmakebuilder</name>
|
||||
<triggers>clean,full,incremental,</triggers>
|
||||
<arguments>
|
||||
<dictionary>
|
||||
<key>?name?</key>
|
||||
<value></value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.append_environment</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.autoBuildTarget</key>
|
||||
<value>all</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.buildArguments</key>
|
||||
<value></value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.buildCommand</key>
|
||||
<value>make</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.buildLocation</key>
|
||||
<value>${workspace_loc:/versuch07/Debug}</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.cleanBuildTarget</key>
|
||||
<value>clean</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.contents</key>
|
||||
<value>org.eclipse.cdt.make.core.activeConfigSettings</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.enableAutoBuild</key>
|
||||
<value>false</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.enableCleanBuild</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.enableFullBuild</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.fullBuildTarget</key>
|
||||
<value>all</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.stopOnError</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
<dictionary>
|
||||
<key>org.eclipse.cdt.make.core.useDefaultBuildCmd</key>
|
||||
<value>true</value>
|
||||
</dictionary>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.cdt.core.ccnature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.ScannerConfigNature</nature>
|
||||
<nature>org.eclipse.cdt.managedbuilder.core.managedBuildNature</nature>
|
||||
<nature>org.eclipse.cdt.core.cnature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
|
|
59
versuch07/Debug/makefile
Normal file
59
versuch07/Debug/makefile
Normal file
|
@ -0,0 +1,59 @@
|
|||
################################################################################
|
||||
# Automatically-generated file. Do not edit!
|
||||
################################################################################
|
||||
|
||||
-include ../makefile.init
|
||||
|
||||
RM := rm -rf
|
||||
|
||||
# All of the sources participating in the build are defined here
|
||||
-include sources.mk
|
||||
-include subdir.mk
|
||||
-include src/subdir.mk
|
||||
-include objects.mk
|
||||
|
||||
ifneq ($(MAKECMDGOALS),clean)
|
||||
ifneq ($(strip $(C++_DEPS)),)
|
||||
-include $(C++_DEPS)
|
||||
endif
|
||||
ifneq ($(strip $(C_DEPS)),)
|
||||
-include $(C_DEPS)
|
||||
endif
|
||||
ifneq ($(strip $(CC_DEPS)),)
|
||||
-include $(CC_DEPS)
|
||||
endif
|
||||
ifneq ($(strip $(CPP_DEPS)),)
|
||||
-include $(CPP_DEPS)
|
||||
endif
|
||||
ifneq ($(strip $(CXX_DEPS)),)
|
||||
-include $(CXX_DEPS)
|
||||
endif
|
||||
ifneq ($(strip $(C_UPPER_DEPS)),)
|
||||
-include $(C_UPPER_DEPS)
|
||||
endif
|
||||
endif
|
||||
|
||||
-include ../makefile.defs
|
||||
|
||||
# Add inputs and outputs from these tool invocations to the build variables
|
||||
|
||||
# All Target
|
||||
all: versuch07
|
||||
|
||||
# Tool invocations
|
||||
versuch07: $(OBJS) $(USER_OBJS)
|
||||
@echo 'Building target: $@'
|
||||
@echo 'Invoking: GCC C++ Linker'
|
||||
g++ -o"versuch07" $(OBJS) $(USER_OBJS) $(LIBS)
|
||||
@echo 'Finished building target: $@'
|
||||
@echo ' '
|
||||
|
||||
# Other Targets
|
||||
clean:
|
||||
-$(RM) $(OBJS)$(C++_DEPS)$(C_DEPS)$(CC_DEPS)$(CPP_DEPS)$(EXECUTABLES)$(CXX_DEPS)$(C_UPPER_DEPS) versuch07
|
||||
-@echo ' '
|
||||
|
||||
.PHONY: all clean dependents
|
||||
.SECONDARY:
|
||||
|
||||
-include ../makefile.targets
|
8
versuch07/Debug/objects.mk
Normal file
8
versuch07/Debug/objects.mk
Normal file
|
@ -0,0 +1,8 @@
|
|||
################################################################################
|
||||
# Automatically-generated file. Do not edit!
|
||||
################################################################################
|
||||
|
||||
USER_OBJS :=
|
||||
|
||||
LIBS :=
|
||||
|
27
versuch07/Debug/sources.mk
Normal file
27
versuch07/Debug/sources.mk
Normal file
|
@ -0,0 +1,27 @@
|
|||
################################################################################
|
||||
# Automatically-generated file. Do not edit!
|
||||
################################################################################
|
||||
|
||||
O_SRCS :=
|
||||
CPP_SRCS :=
|
||||
C_UPPER_SRCS :=
|
||||
C_SRCS :=
|
||||
S_UPPER_SRCS :=
|
||||
OBJ_SRCS :=
|
||||
ASM_SRCS :=
|
||||
CXX_SRCS :=
|
||||
C++_SRCS :=
|
||||
CC_SRCS :=
|
||||
OBJS :=
|
||||
C++_DEPS :=
|
||||
C_DEPS :=
|
||||
CC_DEPS :=
|
||||
CPP_DEPS :=
|
||||
EXECUTABLES :=
|
||||
CXX_DEPS :=
|
||||
C_UPPER_DEPS :=
|
||||
|
||||
# Every subdirectory with source files must be described here
|
||||
SUBDIRS := \
|
||||
src \
|
||||
|
42
versuch07/Debug/src/subdir.mk
Normal file
42
versuch07/Debug/src/subdir.mk
Normal file
|
@ -0,0 +1,42 @@
|
|||
################################################################################
|
||||
# Automatically-generated file. Do not edit!
|
||||
################################################################################
|
||||
|
||||
# Add inputs and outputs from these tool invocations to the build variables
|
||||
CPP_SRCS += \
|
||||
../src/Cramer.cpp \
|
||||
../src/Gauss.cpp \
|
||||
../src/LGSLoeser.cpp \
|
||||
../src/LR.cpp \
|
||||
../src/QMatrix.cpp \
|
||||
../src/Vektor.cpp \
|
||||
../src/main.cpp
|
||||
|
||||
OBJS += \
|
||||
./src/Cramer.o \
|
||||
./src/Gauss.o \
|
||||
./src/LGSLoeser.o \
|
||||
./src/LR.o \
|
||||
./src/QMatrix.o \
|
||||
./src/Vektor.o \
|
||||
./src/main.o
|
||||
|
||||
CPP_DEPS += \
|
||||
./src/Cramer.d \
|
||||
./src/Gauss.d \
|
||||
./src/LGSLoeser.d \
|
||||
./src/LR.d \
|
||||
./src/QMatrix.d \
|
||||
./src/Vektor.d \
|
||||
./src/main.d
|
||||
|
||||
|
||||
# Each subdirectory must supply rules for building sources it contributes
|
||||
src/%.o: ../src/%.cpp
|
||||
@echo 'Building file: $<'
|
||||
@echo 'Invoking: GCC C++ Compiler'
|
||||
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o"$@" "$<"
|
||||
@echo 'Finished building: $<'
|
||||
@echo ' '
|
||||
|
||||
|
Binary file not shown.
|
@ -1,6 +1,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: Cramer.cpp
|
||||
// Inhalt: Cramer-Klasse
|
||||
|
@ -20,5 +20,22 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
Vektor Cramer::loese(QMatrix A, Vektor b)
|
||||
{
|
||||
int dim = A.getDim();
|
||||
double det = A.determinante();
|
||||
Vektor result(dim);
|
||||
|
||||
for (int l = 0; l < dim; l++)
|
||||
{
|
||||
QMatrix Ai = A;
|
||||
for (int k = 0; k < dim; k++) /* konstruiere Ai: i-te Spalte wird durch b ersetzt */
|
||||
{
|
||||
Ai.set(k, l, b.get(k));
|
||||
}
|
||||
|
||||
result.set(l, Ai.determinante() / det);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: Cramer.h
|
||||
// Inhalt: Cramer-Klasse
|
||||
|
@ -19,8 +19,10 @@
|
|||
///////////////////////// CLASS DECLARATION //////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
|
||||
|
||||
class Cramer : public LGSLoeser
|
||||
{
|
||||
public:
|
||||
Vektor loese(QMatrix A, Vektor b);
|
||||
};
|
||||
|
||||
#endif /*CRAMER_H_*/
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: Gauss.cpp
|
||||
// Inhalt: Gauss-Klasse
|
||||
|
@ -22,5 +22,5 @@ using namespace std;
|
|||
|
||||
|
||||
//////////////////////////////////////
|
||||
////Hier Implementierung einfügen ////
|
||||
////Hier Implementierung einfügen ////
|
||||
//////////////////////////////////////
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: LGSLoeser.h
|
||||
// Inhalt: Abstrakte Oberklasse LGSLoeser
|
||||
|
@ -19,14 +19,14 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////// CLASS DECLARATION //////////////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
|
||||
// Abstrakte Oberklasse
|
||||
class LGSLoeser
|
||||
{
|
||||
public:
|
||||
// Lösungsmethode, übernimmt Matrix A und Vektor b und gibt die Lösung x zurück
|
||||
// Lösungsmethode, übernimmt Matrix A und Vektor b und gibt die Lösung x zurück
|
||||
virtual Vektor loese(QMatrix A, Vektor b)=0;
|
||||
|
||||
|
||||
protected:
|
||||
Vektor* x;
|
||||
};
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: LR.cpp
|
||||
// Inhalt: LR-Klasse
|
||||
|
@ -42,22 +42,44 @@ Vektor LR::loese(QMatrix A, Vektor b)
|
|||
erzeugeLundR(A);
|
||||
vorwaertsEinsetzen(b);
|
||||
rueckwaertsEinsetzen();
|
||||
|
||||
|
||||
return *x;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
// Der PseudoCode funktioniert nur, wenn nicht durch 0 dividiert wird.
|
||||
void LR::zerlege(QMatrix& A)
|
||||
{
|
||||
// TODO Der PseudoCode funktioniert nur, wenn nicht durch 0 dividiert wird.
|
||||
for (int i = 1; i <= A.getDim(); i++)
|
||||
{
|
||||
for (int j = i + 1; j <= A.getDim(); j++)
|
||||
{
|
||||
A.set(j, i, A.get(j, i) / A.get(i, i));
|
||||
|
||||
for (int k = i + 1; k <= A.getDim(); k++)
|
||||
{
|
||||
A.set(j, k, A.get(j, k) - (A.get(j, i) * A.get(i, k)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Schreibe aus der Matrix A in die Matrizen L und R
|
||||
void LR::erzeugeLundR(QMatrix& A)
|
||||
{
|
||||
for (int i = 1; i <= A.getDim(); i++)
|
||||
{
|
||||
for (int j = 1; j <= A.getDim(); j++)
|
||||
{
|
||||
if (i == j) /* Diagonale */
|
||||
{
|
||||
L->set(i, j, 1.0);
|
||||
}
|
||||
|
||||
R->set(i, j, A.get(i, j));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -73,16 +95,3 @@ void LR::rueckwaertsEinsetzen()
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: LR.h
|
||||
// Inhalt: LR-Klasse
|
||||
|
@ -24,18 +24,18 @@
|
|||
class LR: public LGSLoeser
|
||||
{
|
||||
public:
|
||||
// Konstruktor, übernimmt die Dimension
|
||||
// Konstruktor, übernimmt die Dimension
|
||||
LR(int n);
|
||||
// Lösungsmethode, übernimmt Matrix A und Vektor b und gibt die Lösung x zurück
|
||||
// Lösungsmethode, übernimmt Matrix A und Vektor b und gibt die Lösung x zurück
|
||||
Vektor loese(QMatrix A, Vektor b);
|
||||
// Destruktor
|
||||
~LR();
|
||||
|
||||
private:
|
||||
// Unterteilung des Lösungsvorgangs in 4 Schritte:
|
||||
// Unterteilung des Lösungsvorgangs in 4 Schritte:
|
||||
// Zerlegt Matrix A, Die zerlegten Matrizen L und R sind danach in A gespeichert
|
||||
void zerlege(QMatrix& A);
|
||||
// Herauskopieren der bedeutenden Einträge in L und R
|
||||
// Herauskopieren der bedeutenden Einträge in L und R
|
||||
void erzeugeLundR(QMatrix& A);
|
||||
// Vorwaertseinsetzen(berechnen von Vektor y aus L und b)
|
||||
void vorwaertsEinsetzen(Vektor& b);
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: QMatrix.cpp
|
||||
// Inhalt: Matrix-Klasse zum speichern von quadratischen Matrizen variabler Größe
|
||||
// Inhalt: Matrix-Klasse zum speichern von quadratischen Matrizen variabler Größe
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -12,6 +12,8 @@
|
|||
|
||||
#include "QMatrix.h"
|
||||
|
||||
#include <cmath>
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
///////////////////////// Funktions-Implementierung //////////////////////////
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -22,12 +24,12 @@
|
|||
QMatrix::QMatrix(int n)
|
||||
{
|
||||
dim = n;
|
||||
A = new double[n*n];
|
||||
A = new double[n * n];
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
A[i*dim+j] = 0.0;
|
||||
A[i * dim + j] = 0.0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -37,12 +39,12 @@ QMatrix::QMatrix(int n)
|
|||
QMatrix::QMatrix(const QMatrix& alteMatrix)
|
||||
{
|
||||
dim = alteMatrix.getDim();
|
||||
A = new double[dim*dim];
|
||||
A = new double[dim * dim];
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
A[i*dim+j] = alteMatrix.get(i,j);
|
||||
A[i * dim + j] = alteMatrix.get(i, j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -56,9 +58,10 @@ QMatrix& QMatrix::operator=(const QMatrix& B)
|
|||
{
|
||||
for (int j = 0; j < dim; j++)
|
||||
{
|
||||
this->A[i*dim+j] = B.get(i,j);
|
||||
this->A[i * dim + j] = B.get(i, j);
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
@ -73,18 +76,18 @@ QMatrix::~QMatrix()
|
|||
// Auf das Element in der i-ten Zeile und j-ten Spalte zurgreifen
|
||||
double QMatrix::get(const int i, const int j) const
|
||||
{
|
||||
return A[i*dim+j];
|
||||
return A[i * dim + j];
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Setze Zahl an die i-te Zeile und j-te Spalte
|
||||
void QMatrix::set(const int i, const int j, const double zahl)
|
||||
{
|
||||
A[i*dim+j]=zahl;
|
||||
A[i * dim + j] = zahl;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Getmethode für die Dimension (wichtig für fast alle for-Schleifen)
|
||||
// Getmethode für die Dimension (wichtig für fast alle for-Schleifen)
|
||||
int QMatrix::getDim() const
|
||||
{
|
||||
return dim;
|
||||
|
@ -97,49 +100,115 @@ void QMatrix::transponiere()
|
|||
double temp;
|
||||
for (int i = 0; i < dim; i++)
|
||||
{
|
||||
for (int j = i+1; j < dim; j++)
|
||||
for (int j = i + 1; j < dim; j++)
|
||||
{
|
||||
temp = A[i*dim+j];
|
||||
A[i*dim+j] = A[j*dim+i];
|
||||
A[j*dim+i] = temp;
|
||||
temp = A[i * dim + j];
|
||||
A[i * dim + j] = A[j * dim + i];
|
||||
A[j * dim + i] = temp;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Erstelle einer Matrix mit einer Spalte und Zeile weniger in der die i-te Zeile und j-te Spalte der
|
||||
// ursprünglichen Matrix fehlt und gebe diese neue Matrix zurück
|
||||
// ursprünglichen Matrix fehlt und gebe diese neue Matrix zurück
|
||||
QMatrix QMatrix::untermatrix(int i, int j)
|
||||
{
|
||||
QMatrix untermat(this->getDim()-1);
|
||||
int n=0;
|
||||
QMatrix untermat(this->getDim() - 1);
|
||||
int n = 0;
|
||||
for (int k = 0; k < this->getDim(); k++)
|
||||
{
|
||||
for (int l = 0; l < this->getDim(); l++)
|
||||
{
|
||||
if ( (i != k) && (j != l) )
|
||||
if ((i != k) && (j != l))
|
||||
{
|
||||
untermat.A[n] = this->get(k,l);
|
||||
untermat.A[n] = this->get(k, l);
|
||||
n++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return untermat;
|
||||
|
||||
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Ausgabeoperator, außerhalb der Klasse implementiert
|
||||
QMatrix QMatrix::inverse()
|
||||
{
|
||||
double det = determinante();
|
||||
QMatrix adj = adjunkte();
|
||||
|
||||
return adj * (1 / det);
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
double QMatrix::determinante()
|
||||
{
|
||||
int dim = getDim();
|
||||
|
||||
if (dim == 1)
|
||||
{
|
||||
return get(0, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
double det = 0;
|
||||
|
||||
// Laplace Entwicklung nach erster Spalte
|
||||
for (int i = 0; i < getDim(); i++)
|
||||
{
|
||||
det += pow(-1, i+2) * get(i, 0) * untermatrix(i, 0).determinante();
|
||||
}
|
||||
|
||||
return det;
|
||||
}
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
QMatrix QMatrix::adjunkte()
|
||||
{
|
||||
QMatrix mat(getDim());
|
||||
|
||||
for (int i = 0; i < getDim(); i++)
|
||||
{
|
||||
for (int j = 0; j < getDim(); j++)
|
||||
{
|
||||
mat.set(i, j, pow(-1, i+j) * untermatrix(i, j).determinante());
|
||||
}
|
||||
}
|
||||
|
||||
mat.transponiere();
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
QMatrix QMatrix::operator*(double scalar)
|
||||
{
|
||||
QMatrix mat(getDim());
|
||||
|
||||
for (int i = 0; i < getDim(); i++)
|
||||
{
|
||||
for (int j = 0; j < getDim(); j++)
|
||||
{
|
||||
mat.set(i, j, get(i, j) * scalar);
|
||||
}
|
||||
}
|
||||
|
||||
return mat;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Ausgabeoperator, außerhalb der Klasse implementiert
|
||||
ostream& operator<<(ostream& Stream, const QMatrix& A)
|
||||
{
|
||||
for (int i = 0; i < A.getDim(); i++)
|
||||
{
|
||||
for (int j = 0; j < A.getDim(); j++)
|
||||
{
|
||||
Stream << setprecision(4) << A.get(i,j) << "\t";
|
||||
Stream << setprecision(4) << A.get(i, j) << "\t";
|
||||
}
|
||||
Stream << endl;
|
||||
}
|
||||
Stream << endl;
|
||||
return Stream;
|
||||
return Stream;
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: QMatrix.h
|
||||
// Inhalt: Matrix-Klasse zum speichern von quadratischen Matrizen variabler Größe
|
||||
// Inhalt: Matrix-Klasse zum speichern von quadratischen Matrizen variabler Gr<EFBFBD><EFBFBD>e
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
#ifndef QMATRIX_H_
|
||||
|
@ -24,13 +24,17 @@ using namespace std;
|
|||
class QMatrix
|
||||
{
|
||||
public:
|
||||
// Konstruktor, übernimmt die Dimension
|
||||
// Konstruktor, übernimmt die Dimension
|
||||
QMatrix(int n);
|
||||
//Kopierkonstruktor
|
||||
|
||||
// Kopierkonstruktor
|
||||
QMatrix(const QMatrix&);
|
||||
// Zuweisungsoperator
|
||||
|
||||
// Operatoren Überladen
|
||||
QMatrix& operator=(const QMatrix& B);
|
||||
//Destruktor
|
||||
QMatrix operator*(const double scalar);
|
||||
|
||||
// Destruktor
|
||||
~QMatrix();
|
||||
|
||||
// Set- und Getmethoden zum Bearbeiten und Lesen der Inhalte des Vektors
|
||||
|
@ -38,9 +42,12 @@ public:
|
|||
void set(const int i, const int j, const double zahl);
|
||||
int getDim() const;
|
||||
|
||||
// Hilfsmethoden für die Cramersche Regel
|
||||
// Hilfsmethoden für die Cramersche Regel
|
||||
void transponiere();
|
||||
QMatrix untermatrix(int i, int j);
|
||||
QMatrix inverse();
|
||||
QMatrix adjunkte();
|
||||
double determinante();
|
||||
|
||||
private:
|
||||
double* A; // Matrix
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: Vektor.cpp
|
||||
// Inhalt: Vektor-Klasse zum speichern von Vektoren variabler Größe
|
||||
// Inhalt: Vektor-Klasse zum speichern von Vektoren variabler Gr<EFBFBD><EFBFBD>e
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -60,7 +60,7 @@ Vektor::~Vektor()
|
|||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Getmethode für den i-ten Eintrag
|
||||
// Getmethode f<EFBFBD>r den i-ten Eintrag
|
||||
double Vektor::get(const int i) const
|
||||
{
|
||||
return v[i];
|
||||
|
@ -70,7 +70,7 @@ double Vektor::get(const int i) const
|
|||
// Schreibt Zahl an den i-ten Eintrag
|
||||
void Vektor::set(const int i, const double zahl)
|
||||
{
|
||||
v[i]=zahl;
|
||||
v[i] = zahl;
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
@ -80,7 +80,7 @@ int Vektor::getDim() const
|
|||
}
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Ausgabeoperator, außerhalb der Klasse implementiert
|
||||
// Ausgabeoperator, au<EFBFBD>erhalb der Klasse implementiert
|
||||
ostream& operator<<(ostream& Stream, const Vektor& v)
|
||||
{
|
||||
for (int i = 0; i < v.getDim(); i++)
|
||||
|
@ -88,5 +88,5 @@ ostream& operator<<(ostream& Stream, const Vektor& v)
|
|||
Stream << setprecision(3) << v.get(i) << endl;
|
||||
}
|
||||
Stream << endl;
|
||||
return Stream;
|
||||
return Stream;
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
// Praktikum Informatik 1
|
||||
// Versuch 7: Lösung eines mathematischen Anwendungsproblems
|
||||
// Versuch 7: L<EFBFBD>sung eines mathematischen Anwendungsproblems
|
||||
//
|
||||
// Datei: main.cpp
|
||||
// Inhalt: Hauptprogramm
|
||||
|
@ -8,9 +8,9 @@
|
|||
|
||||
#include "QMatrix.h"
|
||||
#include "Vektor.h"
|
||||
#include "LR.h"
|
||||
//#include "LR.h"
|
||||
//#include "Gauss.h"
|
||||
//#include "Cramer.h"
|
||||
#include "Cramer.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
@ -22,30 +22,51 @@ void Eingabe(Vektor& b, int n);
|
|||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
/*
|
||||
Die Benennung der Variablen entspricht, in diesem Versuch,
|
||||
den mathematischen Bezeichnungen im Script.
|
||||
Matrizen haben einen Großbuchstaben und
|
||||
Vektoren einen Kleinbuchstaben.
|
||||
*/
|
||||
Die Benennung der Variablen entspricht, in diesem Versuch,
|
||||
den mathematischen Bezeichnungen im Script.
|
||||
Matrizen haben einen Großbuchstaben und
|
||||
Vektoren einen Kleinbuchstaben.
|
||||
*/
|
||||
|
||||
//////////////////////////////////////////////////////////////////////////////
|
||||
|
||||
int main()
|
||||
{
|
||||
cout << "Wieviele Gleichungen/Unbekannte hat ihr LGS.." ;
|
||||
int dim;
|
||||
cin >> dim;
|
||||
int dim = 3;
|
||||
//cout << "Wieviele Gleichungen/Unbekannte hat ihr LGS?: "; cin >> dim;
|
||||
QMatrix A(dim);
|
||||
Vektor b(dim);
|
||||
Vektor x(dim);
|
||||
Eingabe(A,dim);
|
||||
Eingabe(b,dim);
|
||||
cout << A;
|
||||
|
||||
LR system1(dim);
|
||||
x = system1.loese(A,b);
|
||||
cout << x;
|
||||
|
||||
//Eingabe(A, dim);
|
||||
//Eingabe(b, dim);
|
||||
|
||||
/* Beispiel Daten. Ergebnis (4, -2, -1) */
|
||||
A.set(0, 0, 1);
|
||||
A.set(0, 1, 2);
|
||||
A.set(0, 2, 2);
|
||||
A.set(1, 0, 2);
|
||||
A.set(1, 1, 3);
|
||||
A.set(1, 2, 1);
|
||||
A.set(2, 0, 3);
|
||||
A.set(2, 1, 4);
|
||||
A.set(2, 2, 1);
|
||||
|
||||
b.set(0, -2);
|
||||
b.set(1, 1);
|
||||
b.set(2, 3);
|
||||
|
||||
cout << "A = " << endl << A;
|
||||
cout << "b = " << endl << b;
|
||||
//cout << "Dim(A) = " << A.getDim() << endl;
|
||||
//cout << "Det(A) = " << A.determinante() << endl;
|
||||
//cout << "Adj(A) = " << endl << A.adjunkte() << endl;
|
||||
//cout << "A^-1 = " << endl << A.inverse() << endl;
|
||||
|
||||
Cramer lgs1;
|
||||
x = lgs1.loese(A, b);
|
||||
|
||||
cout << "x =" << endl << x;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@ -53,14 +74,15 @@ int main()
|
|||
void Eingabe(QMatrix& A, int n)
|
||||
{
|
||||
double zahl;
|
||||
cout << "Bitte geben sie die Matrix(" << n << " Zeilen/Spalten) ein..." << endl;
|
||||
cout << "Bitte geben sie die Matrix(" << n << " Zeilen/Spalten) ein..."
|
||||
<< endl;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
cout << "a" << (i+1) << (j+1) << ": ";
|
||||
cout << "a" << (i + 1) << (j + 1) << ": ";
|
||||
cin >> zahl;
|
||||
A.set(i,j, zahl);
|
||||
A.set(i, j, zahl);
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
|
@ -70,12 +92,13 @@ void Eingabe(QMatrix& A, int n)
|
|||
void Eingabe(Vektor& b, int n)
|
||||
{
|
||||
double zahl;
|
||||
cout << "Bitte geben sie den Vektor b (" << n << " Eintraege) ein..." << endl;
|
||||
cout << "Bitte geben sie den Vektor b (" << n << " Eintraege) ein..."
|
||||
<< endl;
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
cout << "b" << (i+1) << ": ";
|
||||
cout << "b" << (i + 1) << ": ";
|
||||
cin >> zahl;
|
||||
b.set(i, zahl);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue