Skip to content

Instantly share code, notes, and snippets.

@elnikkis
Created July 21, 2017 04:37
Show Gist options
  • Save elnikkis/f16a659967a78a48f4a9967b634ca445 to your computer and use it in GitHub Desktop.
Save elnikkis/f16a659967a78a48f4a9967b634ca445 to your computer and use it in GitHub Desktop.
#include <stdio.h>
int main(){
int a1 = 1;
int a2 = 3;
int a3 = 5;
int b1 = 0;
int b2 = 4;
int b3 = 6;
int c1 = 2;
int c2 = 5;
int c3 = 7;
printf("%d \n",(a1*b2*c3 + a2*b3*c1 + a3*b1*c2)-(a3*b2*c1 + a2*b1*c3 + a1*b3*c2));
return 0;
}
#include <stdio.h>
int main(){
int a1 = 2;
int a2 = 1;
int a3 = 3;
int b1 = 3;
int b2 = 2;
int b3 = 1;
int c1 = 1;
int c2 = 3;
int c3 = 2;
int d1 = 9;
int d2 = 6;
int d3 = 8;
int A = (a1*b2*c3 + a2*b3*c1 + a3*b1*c2)-(a3*b2*c1 + a2*b1*c3 + a1*b3*c2);
printf("x1 = %d / %d ,x2 = %d / %d, x3 = %d / %d \n",
((d1*b2*c3 + d2*b3*c1 + d3*b1*c2)-(d3*b2*c1 + d2*b1*c3 + d1*b3*c2)),A,
((a1*d2*c3 + a2*d3*c1 + a3*d1*c2)-(a3*d2*c1 + a2*d1*c3 + a1*d3*c2)),A,
((a1*b2*d3 + a2*b3*d1 + a3*b1*d2)-(a3*b2*d1 + a2*b1*d3 + a1*b3*d2)),A);
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<float.h>
#define PI 3.14159265358979323846
#define REAL 0 /* 実根 */
#define IMAGE 1 /* 虚数根 */
int main(void){
double a[3][3] = {
{1, 1, 2},
{0, 2, 2},
{-1, 1, 3}
};
eigen_value(a);
return 0;
}
void eigen_value(double **mat)
{
int a1 = mat[0][0]; //1;
int a2 = mat[0][1]; //1;
int a3 = mat[0][2]; //2;
int b1 = mat[1][0]; //0;
int b2 = mat[1][1]; //2;
int b3 = mat[1][2]; //2;
int c1 = mat[2][0]; //-1;
int c2 = mat[2][1]; //1;
int c3 = mat[2][2]; //3;
double a= -1 ; //t^3
double b= a1+b2+c3; //t^2
double c= -(a1*c3+a1*b2+b2*c3+a3*c1+c2*b3+a2*b1); //t
double d= (a1*b2*c3 + a2*b3*c1 + a3*b1*c2)-(a3*b2*c1 + a2*b1*c3 + a1*b3*c2); /* 3次方程式の係数 */
printf("( %d )*t^3 + ( %d )*t^2 + ( %d )*t +( %d) = 0\n",(int)a,(int)b,(int)c,(int)d);
double p, q;
double D;
double x1, x2, x3; /* 根の実部 */
double y1, y2, y3;
double Im; /* 根の虚部 */
double u, v;
double d_u, d_v;
double phi;
int kind; /* 根の種類: REAL, IMAGE */
if( a == 0.0 ){
printf("a(%g)がゼロなので,データエラーとみなし,\n", a );
printf("解を求めずに終了します.\n");
exit(1); /* 終了コードを1にして,プログラムを終了 */
}
p = ( 3*a*c - b*b ) / ( 9*a*a );
q = ( 2*b*b*b - 9*a*b*c + 27*a*a*d ) / ( 54*a*a*a );
D = q*q + p*p*p; /* Dには誤差が含まれてしまうことに注意 */
if( fabs(D) <= DBL_EPSILON ){ /* Dを数値的にゼロかどうかチェックする */
/* 3つの重根(2重根・3重根)を含む実根 */
y1 = 2*pow(-q, 1.0/3.0);
y2 = y3 = -y1/2;
kind = REAL;
}else if( D < 0 ){ /* 3つの異なる実根 */
phi = acos( -q / pow( -p*p*p, 1.0/3.0 ) );
y1 = 2*sqrt(-p)*cos(phi/3);
y2 = -2*sqrt(-p)*cos(phi/3+PI/3);
y3 = -2*sqrt(-p)*cos(phi/3-PI/3);
kind = REAL;
}else{ /* 1つの実根,2つの共役複素数の解 */
d_u = -q+sqrt(D);
d_v = -q-sqrt(D);
if( d_u >= 0 ) u = pow( d_u, 1.0/3.0 );
else u = -pow( -d_u, 1.0/3.0 );
if( d_v >= 0 ) v = pow( d_v, 1.0/3.0 );
else v = -pow( -d_v, 1.0/3.0 );
y1 = u+v;
y2 = y3 = -y1/2;
Im = sqrt(3)/2*(u-v);
kind = IMAGE;
}
x1 = y1 - b/(3*a);
x2 = y2 - b/(3*a);
x3 = y3 - b/(3*a);
if( kind == REAL ){
printf("t1=%g, t2=%g, t3=%g\n", x1, x2, x3 );
}else{
printf("t1=%g, t2=(%g + %g i), t3=(%g - %g i)\n", x1, x2, Im, x3, Im );
}
int t_hairetu[3] = {x1,x2,x3};
int i;
srand((unsigned)time(NULL));
for(i=0;i<3;i++){
double n1 = a1-t_hairetu[i];
double n2 = b1;
double n3 = c1;
int v_x1 = rand()%10;
int v_x2 = rand()%10;
double v_x3 = -(v_x1*n1+v_x2*n2)/n3;
printf("%d*%2.2f + %d*%2.1f + %2.1f*%2.1f =0 : ",v_x1,n1,v_x2,n2,v_x3,n3);
printf("x1 = %d, x2 = %d , x3 = %2.1f\n",v_x1,v_x2,v_x3);
}
}
int main(){
double a[4][4]={{2,-2,4,2},{2,-1,6,3},{3,-2,12,12},{-1,3,-4,4}};
double det=1.0,buf;
int n=4; //配列の次数
int i,j,k;
//三角行列を作成
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i<j){
buf=a[j][i]/a[i][i];
for(k=0;k<n;k++){
a[j][k]-=a[i][k]*buf;
}
}
}
}
//対角部分の積
for(i=0;i<n;i++){
det*=a[i][i];
}
printf("%f\n",det);
return 0;
}
#include <iostream>
#include "Matrix.h"
int main()
{
const double d[9] = {
1, 0, -1,
1, 2, 1,
2, 2, 3
};
Matrix a(3, 3, d);
const double d2[9] = {
1-1, 0, -1,
1, 2-1, 1,
2, 2, 3-1
};
Matrix b(3, 3, d2);
b.print();
//a.gauss();
double zero[3] = {0, 0, 0};
b.gauss(zero);
return 0;
}
/*
* */
#pragma once
#include <cmath>
using namespace std;
class Matrix
{
public:
//コピーコンストラクタ
Matrix(const Matrix &mat) : rows(mat.rows), cols(mat.cols)
{
data = new double[cols*rows];
for(int i=0; i<rows*cols; i++)
data[i] = mat.data[i];
}
// Matrix(M x N)
Matrix(const int m, const int n) : rows(m), cols(n)
{
data = new double[m*n];
for(int i=0; i<m*n; i++)
data[i] = 0;
}
//外部のデータを利用して生成
Matrix(const int m, const int n, const double *d) : rows(m), cols(n)
{
data = new double[m*n];
for(int i=0; i<m*n; i++)
data[i] = d[i];
}
//デストラクタ
~Matrix()
{
if(data != NULL){
delete[] data;
data = NULL;
}
}
// (Matrix)a = (Matrix)b
Matrix& operator=(const Matrix &b)
{
Matrix &a = *this;
for(int i=0; i<rows*cols; i++)
a.data[i] = b.data[i];
return *this;
}
// -(Matrix)a
const Matrix operator-() const
{
for(int i=0; i<rows*cols; i++)
data[i] = -data[i];
return *this;
}
// (Matrix)a + (Matrix)b
const Matrix operator+(const Matrix &b) const
{
const Matrix &a = *this;
// Size check
if(a.rows != b.rows || a.cols != b.cols){
cerr << "Warning!!! Matrix dimensions must agree." << endl;
return *this;
}
Matrix c(a.cols, a.rows);
for(int i=0; i<rows*cols; i++){
c.data[i] = a.data[i] + b.data[i];
}
return c;
}
// (Matrix)a * (double)b
const Matrix operator*(const double s) const
{
const Matrix &a = *this;
Matrix c(a.rows, a.cols); //c(a.cols, a.rows);
for(int i=0; i<cols*rows; i++)
c.data[i] = a.data[i] * s;
return c;
}
// (Matrix)a * (Matrix)b
const Matrix operator*(const Matrix &b) const
{
const Matrix &a = *this;
//Size check
if(a.cols!= b.rows){
cerr << "Warning!!! Inner matrix dimensions must agree." << endl;
cout << "(r, c) = " << a.rows << ", " << a.cols << endl;
cout << "(r, c) = " << b.rows << ", " << b.cols << endl;
return *this;
}
Matrix c(a.rows, b.cols);
/*
for(int i=0; i<a.cols; i++)
for(int k=0; k<a.cols; k++)
for(int j=0; j<b.cols; j++)
c.data[i*c.cols + j] += a.data[i*a.cols + k] * b.data[k*b.cols + j];
*/
for(int i=0; i<a.rows; i++)
for(int j=0; j<b.cols; j++)
for(int k=0; k<a.cols; k++)
c.data[i*c.cols+j] += a.data[i*a.cols+k] * b.data[k*b.cols+j];
return c;
}
// Read: (Matrix)a[n]
const double* operator[](const long i) const
{
return &data[i*rows];
}
// Write: (Matrix)a[n] = (double)p;
double* operator[](const long i)
{
return &data[i*rows];
}
int getRows() { return rows; }
int getCols() { return cols; }
const double get(const int r, const int c) const
{
return data[r*cols+c];
}
void print() const
{
for(int y=0; y<rows; y++){
cout << "|";
for(int x=0; x<cols; x++){
cout << " " << data[y*cols + x]; //get(y, x);//
}
cout << " |" << endl;
}
cout << endl;
}
//ガウスの消去法
void gauss(double *b)
{
if(rows != cols)
return;
//拡張係数行列をつくる
Matrix ex(rows, cols+1);
for(int r=0; r<rows; r++){
for(int c=0; c<cols; c++){
ex[r][c] = data[r*cols+c];
}
ex[r][cols] = b[r];
}
int N = rows;
for(int k=0; k<N; k++){
//change row
double max = 0;
int s = k;
for(int j=k; j<N; j++){
if(fabs(ex[j][k]) > max){
max = fabs(ex[j][k]);
s = j;
}
}
if(max == 0){
cout << "Can't solve!!" << endl;
return;
}
for(int j=0; j<=N; j++){
double tmp = ex[k][j];
ex[k][j] = ex[s][j];
ex[s][j] = tmp;
}
double pivot = ex[k][k];
for(int j=k; j<N+1; j++)
ex[k][j] = ex[k][j] / pivot;
for(int i=0; i<N; i++){
if(i != k){
double d = ex[i][k];
for(int j=k; j<N+1; j++)
ex[i][j] = ex[i][j] - d * ex[k][j];
}
}
}
ex.print();
// result copy to b
// no implementation
}
private:
double *data;
int rows, cols;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment