Italiano
HOME | PROGETTI | CLASSI C++ | Sorgenti c\c++ | Galleria | Guestbook | Server BW | Contattami | Login

Internet:
HTTP Download

Matematica:
Matrici

Windows:
WIN Audio


<< Matrici >>

visiste: 1749


Matrici una classe scritta in C++ per la gestione delle matrici. Sono implementate le operazioni piu comuni ed molto semplice da usare.

  1. //********************************************//
  2. //************* matrici v2.02.02 *************//
  3. //**************** by mamo139 ****************//
  4. //******* http://mamo139.altervista.org ******//
  5. //********************************************//
  6.  
  7. #ifndef _MATRICI_H
  8. #define _MATRICI_H 1
  9.  
  10. #include <cstdlib>
  11. #include <iostream>
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <math.h>
  16.  
  17.  
  18. //costanti
  19. typedef enum {
  20. MATRICI_CUT_ROW,
  21. MATRICI_CUT_COLUMN
  22. } matrici_cut_mode;
  23.  
  24. typedef enum {
  25. MATRICI_JOIN_HORIZONTALLY,
  26. MATRICI_JOIN_VERTICALLY
  27. } matrici_join_mode;
  28.  
  29. //**************** class ******************//
  30.  
  31. class matrice {
  32. private:
  33. int r;
  34. int c;
  35. double ** m;
  36.  
  37. public:
  38. //costruttore e distruttore
  39. matrice();
  40. matrice(const matrice& o);//copy constructor
  41. ~matrice();
  42.  
  43. //crea matrice
  44. void create(int r, int c);
  45. void create_with_value(int r, int c, double value);
  46.  
  47. //visualizza matrice
  48. void print();
  49.  
  50. //info matrice
  51. int rows();
  52. int columns();
  53. double get_value(int r0, int c0);
  54. void set_value(int r0, int c0, double value);
  55. double *operator[](int i);
  56.  
  57. //caricamento - salvataggio matrici
  58. void load_from_file(char * file);
  59. void save_in_file(char * file);
  60.  
  61. //assignment operator
  62. matrice operator=(const matrice o);
  63.  
  64. //operazioni matrici con matrici
  65. friend matrice operator+(matrice a, matrice b);
  66. friend matrice operator-(matrice a, matrice b);
  67. friend matrice operator*(matrice a,matrice b);
  68.  
  69. //operazioni matrici con scalari
  70. friend matrice operator+(double scal,matrice a);
  71. friend matrice operator+(matrice a, double scal);
  72. friend matrice operator-(double scal,matrice a);
  73. friend matrice operator-(matrice a, double scal);
  74. friend matrice operator*(double scal,matrice a);
  75. friend matrice operator*(matrice a, double scal);
  76.  
  77. //altre operazioni
  78. friend matrice matrici_transpose(matrice a);
  79. friend double matrici_determinant(matrice a);
  80. friend matrice matrici_invert(matrice a);
  81.  
  82. //modifica matrici
  83. friend matrice matrici_cut(matrice a, matrici_cut_mode mode, int pos);
  84. friend matrice matrici_join(matrice a, matrice b, matrici_join_mode mode);
  85.  
  86.  
  87. };
  88.  
  89.  
  90. #endif
  91.  


  1. #include "matrici.h"
  2.  
  3. //costruttore
  4. matrice::matrice(){
  5. r = 0;
  6. c = 0;
  7. }
  8. matrice::matrice(const matrice& o) { //copy constructor
  9.  
  10. create(o.r,o.c);
  11. for(int x=0;x<r;x++)
  12. for(int y=0;y<c;y++)
  13. m[x][y] = o.m[x][y];
  14. }
  15.  
  16. //distruttore
  17. matrice::~matrice(){
  18. int x;
  19.  
  20. if(r > 0){
  21. for(x=0;x<r;x++)
  22. delete[] m[x];
  23. delete[] m;
  24. }
  25. }
  26.  
  27. //creazione matrice
  28. void matrice::create(int r, int c){ // r = righe, c = colonne
  29.  
  30. int x;
  31.  
  32. this->r = r;
  33. this->c = c;
  34.  
  35. m = new double *[r];
  36. for(x=0;x<r;x++)
  37. m[x] = new double[c];
  38.  
  39.  
  40. }
  41. void matrice::create_with_value(int r, int c, double value){
  42.  
  43. int x,y;
  44.  
  45. this->r = r;
  46. this->c = c;
  47.  
  48. m = new double *[r];
  49. for(x=0;x<r;x++)
  50. m[x] = new double[c];
  51.  
  52. for(x=0;x<r;x++)
  53. for(y=0;y<c;y++)
  54. m[x][y] = value;
  55.  
  56. }
  57.  
  58. //visualizza matrice
  59. void matrice::print(){
  60.  
  61. int x,y;
  62.  
  63. for(x=0;x<r;x++){
  64. for(y=0;y<c;y++)
  65. printf("%12.6f\x20",m[x][y]);
  66. printf("\n");
  67. }
  68. printf("\n");
  69.  
  70. }
  71.  
  72. //info matrice
  73. int matrice::rows(){
  74. return r;
  75. }
  76. int matrice::columns(){
  77. return c;
  78. }
  79.  
  80. double matrice::get_value(int r0, int c0){
  81. if(r0 >= r || c0 >= c)
  82. exit(0);
  83. return m[r0][c0];
  84. }
  85. void matrice::set_value(int r0, int c0, double value){
  86. if(r0 >= r || c0 >= c)
  87. exit(0);
  88. m[r0][c0] = value;
  89. }
  90. double *matrice::operator[](int i){
  91. return m[i];
  92. }
  93.  
  94. //caricamento - salvataggio matrici
  95. void matrice::load_from_file(char * file){
  96.  
  97. FILE *stream;
  98. char *buffer, *number;
  99. long filesize, readed;
  100. long x, y=0, mr=0, mc=0, r=0, c=0;
  101.  
  102. stream = fopen(file,"rb");
  103. fseek(stream,0,SEEK_END);
  104. filesize = ftell(stream);
  105. rewind(stream);
  106.  
  107. buffer = new char[filesize];
  108. number = new char[100];
  109. readed = fread(buffer,1,filesize,stream);
  110.  
  111. fclose(stream);
  112.  
  113. for(x=0; x<filesize ;x++){ //conta colonne
  114. if(buffer[x] == '\x09')
  115. mc++;
  116. if(buffer[x] == '\x0D'){
  117. mc++;
  118. break;
  119. }
  120. }
  121. for(x=0; x<filesize ;x++){ //conta righe
  122. if(buffer[x] == '\x0D')
  123. mr++;
  124. }
  125.  
  126. create(mr, mc);
  127.  
  128. for(x=0; x<filesize ;x++){
  129. if(buffer[x] == '\x0A')
  130. continue;
  131. if(buffer[x] == '\x09' || buffer[x] == '\x0D'){
  132. number[y] = '\x00';
  133. this->m[r][c] = atof(number);
  134. y=0;
  135. c++;
  136. if(buffer[x] == '\x0D'){
  137. r++;
  138. c=0;
  139. }
  140. }
  141. else{
  142. number[y] = buffer[x];
  143. y++;
  144. }
  145. }
  146.  
  147. delete [] buffer;
  148. delete [] number;
  149.  
  150. }
  151.  
  152. void matrice::save_in_file(char * file){
  153.  
  154. int x,y;
  155. FILE * stream;
  156. stream = fopen(file, "w+");
  157.  
  158. for(x=0;x<r;x++){
  159. for(y=0;y<c;y++){
  160. fprintf(stream,"%.6f",m[x][y]);
  161. if(y<c-1) fprintf(stream,"\x09");
  162. }
  163. fprintf(stream,"\n");
  164. }
  165. fclose(stream);
  166.  
  167. }
  168.  
  169. //assignment operator
  170. matrice matrice::operator=(const matrice o) {
  171.  
  172. int x;
  173.  
  174. if (this == &o) { //per non copiare se stessi :)
  175. return *this;
  176. }
  177. //cancelliamo le cose che ci sono adesso
  178.  
  179. if(r > 0){
  180. for(x=0;x<r;x++)
  181. delete[] m[x];
  182. delete[] m;
  183. }
  184.  
  185. //creiamo la nuova matrice e ci copiamo dentro i dati
  186. create(o.r,o.c);
  187. for(int x=0;x<r;x++)
  188. for(int y=0;y<c;y++)
  189. m[x][y] = o.m[x][y];
  190.  
  191. return *this;
  192. }
  193.  
  194. //operazioni matrici con matrici
  195. matrice operator+(matrice a,matrice b){
  196.  
  197. matrice c;
  198. int x,y;
  199.  
  200. if(a.r != b.r || a.c != b.c)
  201. exit(0);
  202.  
  203. c.create(a.r, a.c);
  204.  
  205. for(x=0;x<c.r;x++)
  206. for(y=0;y<c.c;y++)
  207. c.m[x][y] = a.m[x][y]+b.m[x][y];
  208.  
  209. return c;
  210. }
  211.  
  212. matrice operator-(matrice a,matrice b){
  213.  
  214. matrice c;
  215. int x,y;
  216.  
  217. if(a.r != b.r || a.c != b.c)
  218. exit(0);
  219.  
  220. c.create(a.r, a.c);
  221.  
  222. for(x=0;x<c.r;x++)
  223. for(y=0;y<c.c;y++)
  224. c.m[x][y] = a.m[x][y]-b.m[x][y];
  225.  
  226. return c;
  227. }
  228.  
  229. matrice operator*(matrice a,matrice b){
  230.  
  231. matrice c;
  232. int x,y,i;
  233.  
  234. if(a.c != b.r)
  235. exit(0);
  236.  
  237. c.create(a.r, b.c);
  238.  
  239. for(x=0;x<c.r;x++)
  240. for(y=0;y<c.c;y++){
  241. c.m[x][y] = 0;
  242. for(i=0;i<a.c;i++)
  243. c.m[x][y] = c.m[x][y] + a.m[x][i]*b.m[i][y];
  244. }
  245.  
  246. return c;
  247. }
  248.  
  249. //operazioni matrici con scalari
  250. matrice operator+(double scal,matrice a){
  251.  
  252. matrice c;
  253. int x,y;
  254.  
  255. c.create(a.r, a.c);
  256.  
  257. for(x=0;x<c.r;x++)
  258. for(y=0;y<c.c;y++)
  259. c.m[x][y] = a.m[x][y]+scal;
  260.  
  261. return c;
  262.  
  263. }
  264. matrice operator+(matrice a, double scal){
  265.  
  266. matrice c;
  267. int x,y;
  268.  
  269. c.create(a.r, a.c);
  270.  
  271. for(x=0;x<c.r;x++)
  272. for(y=0;y<c.c;y++)
  273. c.m[x][y] = a.m[x][y]+scal;
  274.  
  275. return c;
  276.  
  277. }
  278. matrice operator-(double scal,matrice a){
  279.  
  280. matrice c;
  281. int x,y;
  282.  
  283. c.create(a.r, a.c);
  284.  
  285. for(x=0;x<c.r;x++)
  286. for(y=0;y<c.c;y++)
  287. c.m[x][y] = scal-a.m[x][y];
  288.  
  289. return c;
  290.  
  291. }
  292. matrice operator-(matrice a, double scal){
  293.  
  294. matrice c;
  295. int x,y;
  296.  
  297. c.create(a.r, a.c);
  298.  
  299. for(x=0;x<c.r;x++)
  300. for(y=0;y<c.c;y++)
  301. c.m[x][y] = a.m[x][y]-scal;
  302.  
  303. return c;
  304.  
  305. }
  306. matrice operator*(double scal,matrice a){
  307.  
  308. matrice c;
  309. int x,y;
  310.  
  311. c.create(a.r, a.c);
  312.  
  313. for(x=0;x<c.r;x++)
  314. for(y=0;y<c.c;y++)
  315. c.m[x][y] = a.m[x][y]*scal;
  316.  
  317. return c;
  318.  
  319. }
  320. matrice operator*(matrice a, double scal){
  321.  
  322. matrice c;
  323. int x,y;
  324.  
  325. c.create(a.r, a.c);
  326.  
  327. for(x=0;x<c.r;x++)
  328. for(y=0;y<c.c;y++)
  329. c.m[x][y] = a.m[x][y]*scal;
  330.  
  331. return c;
  332.  
  333. }
  334.  
  335. //altre operazioni
  336. matrice matrici_transpose(matrice a){
  337.  
  338. int x,y;
  339. matrice c;
  340.  
  341. c.create(a.c, a.r);
  342.  
  343. for(x=0;x<a.r;x++)
  344. for(y=0;y<a.c;y++)
  345. c.m[y][x] = a.m[x][y];
  346.  
  347. return c;
  348.  
  349. }
  350.  
  351. double matrici_determinant(matrice a){
  352.  
  353. long x,riga=0;
  354. double determinante=0;
  355. matrice b;
  356.  
  357. if(a.r != a.c)
  358. exit(0);
  359.  
  360. if(a.r == 2){
  361. return (a.m[0][0]*a.m[1][1]-a.m[0][1]*a.m[1][0]);
  362. }
  363. else{
  364. for(x=0;x<a.c;x++){
  365. b = matrici_cut(a, MATRICI_CUT_ROW, riga);
  366. b = matrici_cut(b, MATRICI_CUT_COLUMN, x);
  367.  
  368. determinante = determinante + a.m[riga][x]*matrici_determinant(b)*pow((double)-1,(x+1)+1);
  369.  
  370. }
  371. return determinante;
  372. }
  373. }
  374.  
  375. matrice matrici_invert(matrice a){
  376.  
  377. long x,y;
  378. double det;
  379. matrice b,c;
  380.  
  381. det = matrici_determinant(a);
  382.  
  383. if(det == 0)
  384. return b;
  385.  
  386. b.create(a.r,a.c);
  387.  
  388. for(x=0;x<a.r;x++)
  389. for(y=0;y<a.c;y++){
  390. c = matrici_cut(a, MATRICI_CUT_ROW, x);
  391. c = matrici_cut(c, MATRICI_CUT_COLUMN, y);
  392. b.m[x][y] = matrici_determinant(c)*pow((double)-1,((x+1)+(y+1)))/det;
  393. }
  394.  
  395. b = matrici_transpose(b);
  396.  
  397. return b;
  398. }
  399.  
  400. matrice matrici_cut(matrice a, matrici_cut_mode mode, int pos){
  401.  
  402. matrice c;
  403. int x,y,x1=0,y1=0;
  404.  
  405. if(mode == MATRICI_CUT_ROW)
  406. c.create(a.r-1,a.c);
  407. else if(mode == MATRICI_CUT_COLUMN)
  408. c.create(a.r,a.c-1);
  409. else
  410. exit(0);
  411.  
  412. for(x=0; x<a.r ;x++){
  413. if(mode == MATRICI_CUT_ROW && pos == x) continue;
  414. y1=0;
  415. for(y=0; y<a.c ;y++){
  416. if(mode == MATRICI_CUT_COLUMN && pos == y)continue;
  417. c.m[x1][y1] = a.m[x][y];
  418. if(!(mode == MATRICI_CUT_COLUMN && pos == y))y1++;
  419. }
  420. if(!(mode == MATRICI_CUT_ROW && pos == x)) x1++;
  421. }
  422.  
  423. return c;
  424.  
  425. }
  426.  
  427. matrice matrici_join(matrice a, matrice b, matrici_join_mode mode){
  428.  
  429. matrice c;
  430. int x,y;
  431.  
  432. if(mode == MATRICI_JOIN_HORIZONTALLY){
  433. if(a.r != b.r)
  434. exit(0);
  435.  
  436. c.create(a.r, a.c + b.c);
  437.  
  438. for(x=0;x<c.r;x++)
  439. for(y=0;y<c.c;y++)
  440. if(y < a.c)
  441. c.m[x][y] = a.m[x][y];
  442. else
  443. c.m[x][y] = b.m[x][y - a.c];
  444. }
  445. else if(mode == MATRICI_JOIN_VERTICALLY){
  446. if(a.c != b.c)
  447. exit(0);
  448.  
  449. c.create(a.r + b.r, a.c);
  450.  
  451. for(x=0;x<c.r;x++)
  452. for(y=0;y<c.c;y++)
  453. if(x < a.r)
  454. c.m[x][y] = a.m[x][y];
  455. else
  456. c.m[x][y] = b.m[x - a.r][y];
  457. }
  458.  
  459. return c;
  460. }
  461.  
Versione sito: 1.03.01 by mamo139. - Tutti i sorgenti presenti su questo sito sono rilasciati sotto licenza "GNU GPL version 3".