Machine Learning experiment 7 Matrix Inversion

Source: Internet
Author: User
#include"stdio.h"#include <math.h>double function(double matrix[3][6],double theta[3],int sample_i){        double ret=0.0;        for(int i=0;i<3;++i)        {                ret+=matrix[sample_i][i]*theta[i];        }        return ret;}double theta[3]={1,1,1};int main(void){        double matrix[3][6]={                        {1,2,3,1,0,0},                        {2,1,1,0,1,0},                        {3,1,2,0,0,1},                    };        double alfa = 0.1;        double c = 0;        double d = 1;        for(int z=0;z<3;++z)        {                double loss = 0.0;                for(int j = 0;j<3;++j)                {                        double sum=function(matrix,theta,j);                        loss += pow((pow(sum+c,d)-matrix[j][z+3]),2);                }//printf("loss : %lf\n",loss);                  for(int i=0;i<200;++i)                {                        for(int sample_i = 0; sample_i<3;sample_i++)                        {                                double result = function(matrix,theta,sample_i)+c;                                for(int j=0;j<3;++j)                                {                                        theta[j] = theta[j] - alfa*(pow(result,d)-matrix[sample_i][z+3])*d*pow((result),d-1)*matrix[sample_i][j];                                }                        }                        double loss = 0.0;                        for(int j = 0;j<3;++j)                        {                                double sum=function(matrix,theta,j);                                loss += pow((pow(sum+c,d)-matrix[j][z+3]),2);                        }                        //printf("%d,loss  now: %lf,%lf,%lf,%lf\n",i,loss,theta[0],theta[1],theta[2]);                }                printf("%lf,%lf,%lf\n",theta[0],theta[1],theta[2]);        }        return 0;}                             

The above code is slightly modified from lab 6.

Calculate the inverse matrix of a matrix, as shown in the following example:

1 2 3X1 Y1 Z1 1 0 0
2 1 1X2 Y2 Z2 0 1 0
3 1 2x3 Y3 Z3 0 0 1

The exact result should be:
-1/2 1/2 1/2
1/2 7/4 5/4
1/2-3/2 3/4

The iteration result is

-0.249835, 0.250747, 0.249447
0.247973, 1.740834,-1.243221
0.253544,-1.233976, 0.738149

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.