Skip to content
Snippets Groups Projects
Commit 97c023a3 authored by Benjamin-Sitbon's avatar Benjamin-Sitbon
Browse files

Matrix

parent d886cc7e
No related branches found
No related tags found
No related merge requests found
<?xml version="1.0" encoding="UTF-8"?>
<module type="CPP_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" addBOMForNewFiles="with NO BOM" />
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="ES6" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Projet.iml" filepath="$PROJECT_DIR$/.idea/Projet.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeRunConfigurationManager" shouldGenerate="true" shouldDeleteObsolete="true">
<generated />
</component>
<component name="CMakeSettings">
<configurations>
<configuration PROFILE_NAME="Debug" CONFIG_NAME="Debug" />
</configurations>
</component>
<component name="ChangeListManager">
<list default="true" id="a4729566-319a-40e2-ad79-c7ce5c27c976" name="Default Changelist" comment="">
<change beforePath="$PROJECT_DIR$/joseph.exe" beforeDir="false" afterPath="$PROJECT_DIR$/joseph.exe" afterDir="false" />
<change beforePath="$PROJECT_DIR$/matrix.c" beforeDir="false" afterPath="$PROJECT_DIR$/matrix.c" afterDir="false" />
<change beforePath="$PROJECT_DIR$/matrix.h" beforeDir="false" afterPath="$PROJECT_DIR$/matrix.h" afterDir="false" />
<change beforePath="$PROJECT_DIR$/matrix.o" beforeDir="false" afterPath="$PROJECT_DIR$/matrix.o" afterDir="false" />
<change beforePath="$PROJECT_DIR$/matrix_compute.c" beforeDir="false" afterPath="$PROJECT_DIR$/matrix_compute.c" afterDir="false" />
<change beforePath="$PROJECT_DIR$/matrix_compute.exe" beforeDir="false" afterPath="$PROJECT_DIR$/matrix_compute.exe" afterDir="false" />
<change beforePath="$PROJECT_DIR$/matrix_compute.o" beforeDir="false" afterPath="$PROJECT_DIR$/matrix_compute.o" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pointer_sort.exe" beforeDir="false" afterPath="$PROJECT_DIR$/pointer_sort.exe" afterDir="false" />
</list>
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="FileEditorManager">
<leaf>
<file pinned="false" current-in-tab="true">
<entry file="file://$PROJECT_DIR$/matrix_compute.c">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="102">
<caret line="6" column="8" selection-start-line="6" selection-start-column="8" selection-end-line="6" selection-end-column="8" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/matrix.c">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="263">
<caret line="117" lean-forward="true" selection-start-line="117" selection-end-line="117" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/matrix.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="11" column="45" selection-start-line="11" selection-start-column="45" selection-end-line="11" selection-end-column="45" />
</state>
</provider>
</entry>
</file>
<file pinned="false" current-in-tab="false">
<entry file="file://$PROJECT_DIR$/joseph.c">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="170">
<caret line="31" column="10" lean-forward="true" selection-start-line="31" selection-start-column="10" selection-end-line="31" selection-end-column="32" />
</state>
</provider>
</entry>
</file>
</leaf>
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/matrix.h" />
<option value="$PROJECT_DIR$/matrix.c" />
<option value="$PROJECT_DIR$/matrix_compute.c" />
</list>
</option>
</component>
<component name="ProjectFrameBounds" extendedState="6">
<option name="x" value="360" />
<option name="y" value="-23" />
<option name="width" value="1049" />
<option name="height" value="730" />
</component>
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes>
<pane id="ProjectPane">
<subPane>
<expand>
<path>
<item name="Projet" type="b2602c69:ProjectViewProjectNode" />
<item name="Projet" type="462c0819:PsiDirectoryNode" />
</path>
</expand>
<select />
</subPane>
</pane>
<pane id="Scope" />
</panes>
</component>
<component name="PropertiesComponent">
<property name="WebServerToolWindowFactoryState" value="false" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
<property name="nodejs_interpreter_path.stuck_in_default_project" value="undefined stuck path" />
<property name="nodejs_npm_path_reset_for_default_project" value="true" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="a4729566-319a-40e2-ad79-c7ce5c27c976" name="Default Changelist" comment="" />
<created>1557141417692</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1557141417692</updated>
<workItem from="1557141419149" duration="10738000" />
</task>
<servers />
</component>
<component name="TimeTrackingManager">
<option name="totallyTimeSpent" value="10738000" />
</component>
<component name="ToolWindowManager">
<frame x="-7" y="-7" width="1550" height="838" extended-state="6" />
<editor active="true" />
<layout>
<window_info id="Favorites" side_tool="true" />
<window_info content_ui="combo" id="Project" order="0" visible="true" weight="0.24966443" />
<window_info id="Structure" order="1" side_tool="true" weight="0.25" />
<window_info anchor="bottom" id="Database Changes" />
<window_info anchor="bottom" id="Version Control" />
<window_info active="true" anchor="bottom" id="Terminal" visible="true" weight="0.3286119" />
<window_info anchor="bottom" id="Event Log" side_tool="true" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.4" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="right" id="Database" />
<window_info anchor="right" id="Commander" internal_type="SLIDING" order="0" type="SLIDING" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
</layout>
</component>
<component name="TypeScriptGeneratedFilesManager">
<option name="version" value="1" />
</component>
<component name="editorHistoryManager">
<entry file="file://$PROJECT_DIR$/pointer_sort.c">
<provider selected="true" editor-type-id="text-editor" />
</entry>
<entry file="file://$PROJECT_DIR$/joseph.c">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="170">
<caret line="31" column="10" lean-forward="true" selection-start-line="31" selection-start-column="10" selection-end-line="31" selection-end-column="32" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/matrix.h">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="187">
<caret line="11" column="45" selection-start-line="11" selection-start-column="45" selection-end-line="11" selection-end-column="45" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/matrix.c">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="263">
<caret line="117" lean-forward="true" selection-start-line="117" selection-end-line="117" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/matrix_compute.c">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="102">
<caret line="6" column="8" selection-start-line="6" selection-start-column="8" selection-end-line="6" selection-end-column="8" />
</state>
</provider>
</entry>
</component>
</project>
\ No newline at end of file
No preview for this file type
2 4
1 2 3 4
2 3 4 5
\ No newline at end of file
4 2
1 2
2 3
3 4
4 5
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdio.h>
#include <string.h>
#include <assert.h>
#include "matrix.h" #include "matrix.h"
m_double* matrix_alloc(unsigned int row, unsigned int col){ m_double* matrix_alloc(unsigned int row, unsigned int col){
if(row == 0 || col == 0){ if (row <= 0 || col <= 0) return NULL;
return NULL; m_double* matrix = (m_double*) calloc(1, sizeof(m_double));
} matrix->row = row; matrix->col = col;
m_double* matrix = calloc(1,sizeof(m_double)); double* mat_ptr = (double*) malloc(row*col*sizeof(double));
double ** mat_ptr = malloc(row*col*sizeof(double)); matrix->content = (double*) malloc(row*sizeof(double));
matrix->content = malloc(row*col*sizeof(double*)) for (int i=0;i<row;i++) matrix->content[i] = mat_ptr+i*col;
for (int i=0;i<li;i++) mat->content[i] = data+i*co;
return matrix; return matrix;
} }
void matrix_free(m_double* matrix) {
free(matrix->content);
free(matrix);
matrix = NULL;
}
m_double* matrix_transpose(m_double* matrix) { m_double* matrix_transpose(m_double* matrix) {
assert(NULL != matrix);
m_double* transp = matrix_alloc(matrix->col,matrix->row);
m_double transp = matrix_alloc(matrix->col,matrix->row);
for (int i = 0; i<matrix->row; i++){ for (int i = 0; i<matrix->row; i++){
for (int j = 0; j<matrix->col; j++){ for (int j = 0; j<matrix->col; j++){
transp[j][i] = matrix[i][j]; transp->content[j][i] = matrix->content[i][j];
}
printf("\n");
}
return transp;
}
m_double* matrix_add(m_double* add1, m_double* add2) {
assert(NULL != add1 && NULL != add2);
assert(add1->row == add2->row && add1->col == add2->col);
m_double* add = matrix_alloc(add1->row, add1->col);
for (int i=0; i < add1->row; i++) {
for (int j=0; j < add1->col; j++) {
add->content[i][j] = add1->content[i][j] + add2->content[i][j];
}
}
return add;
}
m_double* matrix_mult(m_double* add1, m_double* add2) {
assert((NULL != add1 && NULL != add2) && add1->col == add2->row);
m_double* add = matrix_alloc(add1->row,add2->col);
for (int i = 0; i < add->row; i++) {
for (int j = 0; j < add->col; j++) {
double result = 0.0;
for (int k = 0; k < add1->col; k++) {
result += add1->content[i][k] * add2->content[k][j];
}
add->content[i][j] = result;
}
}
return add;
}
m_double* file2matrix(const char* filename) {
FILE *fp = fopen(filename,"r+");
if (NULL == fp) {
fprintf(stderr,"%s, file not found\n",filename);
exit(1);
}
unsigned int row = 0, col = 0;
fscanf(fp,"%d",&row);
fscanf(fp,"%d",&col);
m_double* mat = matrix_alloc(row, col);
for (int i=0;i<mat->row;i++) {
for (int j = 0; j < mat->col; j++) {
fscanf(fp, "%lf ", &(mat->content[i][j]));
} }
} }
fclose(fp);
return mat;
}
void matrix2file(m_double* m,char* filename) {
FILE *fp = stdout;
if (NULL != filename) fp = fopen(filename, "w+");
fprintf(fp, "%d %d\n", m->row, m->col);
for (int i = 0; i < m->row; i++){
for (int j = 0; j < m->col; j++){
fprintf(fp, "%g ", m->content[i][j]);
}
fprintf(fp, "\n");
}
}
void matrix_display(m_double* matrix) {
if (matrix == NULL){
exit(1);
}
printf("[%d,%d]\n", matrix->row, matrix->col);
for (int i = 0; i < matrix->row; i++){
printf("|");
for (int j = 0; j < matrix->col; j++){
printf("%g|", matrix->content[i][j]);
if(j=matrix->col-1) printf("%g", matrix->content[i][j]);
}
printf("|");
printf("\n");
}
} }
\ No newline at end of file
...@@ -6,3 +6,7 @@ typedef struct m_double { ...@@ -6,3 +6,7 @@ typedef struct m_double {
m_double* matrix_alloc(unsigned int row, unsigned int col); m_double* matrix_alloc(unsigned int row, unsigned int col);
m_double* matrix_transpose(m_double* m); m_double* matrix_transpose(m_double* m);
m_double* file2matrix(const char* filename);
void matrix2file(m_double* m,char* filename);
\ No newline at end of file
No preview for this file type
int main(){} #include <stdio.h>
\ No newline at end of file #include <stdlib.h>
#include <stdbool.h>
#include "matrix.h"
int main(int argc, char *argv[]){
m_double* m = file2matrix("mat.dat");
matrix_display(m);
m_double* ma = matrix_transpose(m);
matrix_display(ma);
matrix2file(ma,"mat_new.dat");
}
\ No newline at end of file
No preview for this file type
No preview for this file type
No preview for this file type
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment