Nacho: A lightweight C + + Matrix operations Class Library

Source: Internet
Author: User

Project background

Building a mathematical model of a pipe network from the point of view of connectivity is actually simple:

The A is referred to as the connected matrix of the pipe network. It can be seen from the definition that the pipe network is considered a one-way map here.

According to the law of conservation of the node mass, in each node of the water supply network, the quality of the flow into the node should be equal to the quality of the node, so that the nodal flow balance equations of the whole pipe network can be set up:

This equation group is the basis of hydraulic analysis of water supply network.

This is the overall structure of the hydraulic analysis program:

In the calculation process, a large number of matrices are used for addition, subtraction, multiplication, transpose and other operations. Nacho is designed to meet these computational needs.



Design ideas

Using the object-oriented design pattern, the matrix is encapsulated into a Cmatrix class, The matrix transpose, Norm, zero and other operations are provided by the member methods of Cmatrix , and then by defining "+", "-", "*", "/", "^" and other operators to complete the matrix addition, subtraction, multiplication, In addition to the operation of the element exponentiation, finally, the Cmatrix console output operator is defined separately, and the matrix can be easily printed out.

The definition of Cmatrix is given below:

 CMatrix.h#ifndef CMATRIX_H#define CMATRIX_H#include <iostream>using  Namespace std;class cmatrix {public:        cmatrix (int &NBSP;M,&NBSP;INT&NBSP;N);          //constructs a m*n full 0 matrix  cmatrix ( INT&NBSP;N);                 // Build a n*n full 0 matrix    cmatrix (const cmatrix &);       // Copy constructor, deep copy                   ~cmatrix (); static bool printwhencreateanddelete;    //control whether to print construction and destruction int  Getrownum ()  const;             // Returns the number of rows of a matrix int getcolnum ()  const;              //returns the number of columns in the matrix Bool gettransState ()  const;        //returns the transpose status of the Matrix Cmatrix trans ()  const;                       //Transpose The Matrix Double norm ()  const;                        //solution Matrix F-Norm Double get ( INT&NBSP;I,&NBSP;INT&NBSP;J)  const;             //returns the matrix I row J column element Void set (int i, int j, double val);         //Set Matrix I row J column element is valvoid diagunitize ();                         // Set the diagonal elements of the Phalanx all to 1void clear ();                                //sets all elements of the matrix to zero Cmatrix operator  + (Const cmatrix &mat);        //two matrix addition CMatrix  operator -(Const cmatrix &mat);        //two matrix subtraction CMatrix  operator * (Const cmatrix &mat);        // Two matrix multiplication cmatrix operator * (const double f);             //matrix multiplied by constant cmatrix operator /(const double f);             //matrix divided by constant cmatrix operator ^ (const double &NBSP;F);             //matrix elements to power Void operator respectively  = (Const cmatrix &mat);            // Assigning a matrix to another private:double *start;//refers to theThe pointer to the first element of the matrix int rownum;//the number of rows of the matrix int colnum;//matrix columns bool istransposed;//whether the record matrix is transpose Void rowcolswap (); /transpose is used to exchange values of row_num and Col_num};//print the Matrix ostream & operator << in a canonical format in the console (Ostream &  const CMatrix &); #endif


Open Source Projects

Project Address: Https://github.com/lbbc1117/Nacho

A simple API document is included in the project.

I have also defined a CTest class, which contains different methods for testing the various operations of Cmatrix and outputting the test results to the console.

CTest is also included in the Open source project.

Nacho: A lightweight C + + Matrix operations Class Library

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.