diff --git a/.idea/Projet.iml b/.idea/Projet.iml new file mode 100644 index 0000000000000000000000000000000000000000..bc2cd87409057301f546d83bd548111b9a241cb1 --- /dev/null +++ b/.idea/Projet.iml @@ -0,0 +1,8 @@ +<?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 diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 0000000000000000000000000000000000000000..15a15b218a29e09c9190992732698d646e4d659a --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,4 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project version="4"> + <component name="Encoding" addBOMForNewFiles="with NO BOM" /> +</project> \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000000000000000000000000000000000000..28a804d8932aba40f168fd757a74cb718a955a1a --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ +<?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 diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000000000000000000000000000000000000..141a619e356bbca537196b59c79269564971ac6e --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ +<?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 diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000000000000000000000000000000000000..94a25f7f4cb416c083d265558da75d457237d671 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ +<?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 diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000000000000000000000000000000000000..8c618d905b76337eb24480c00a1b4d6a381649a5 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,200 @@ +<?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 diff --git a/joseph.exe b/joseph.exe index 1a921245da6d6d3bb3b20ddad95a114102965370..e5e002dd178c53f0f4c302519a449b5fa43f026e 100644 Binary files a/joseph.exe and b/joseph.exe differ diff --git a/mat.dat b/mat.dat new file mode 100644 index 0000000000000000000000000000000000000000..0c8e0bd02b7b8ebf557e60adccd2961f11cdf6a3 --- /dev/null +++ b/mat.dat @@ -0,0 +1,3 @@ +2 4 +1 2 3 4 +2 3 4 5 \ No newline at end of file diff --git a/mat_new.dat b/mat_new.dat new file mode 100644 index 0000000000000000000000000000000000000000..52083353748303ac66a87dff912fe2ceca79289d --- /dev/null +++ b/mat_new.dat @@ -0,0 +1,5 @@ +4 2 +1 2 +2 3 +3 4 +4 5 diff --git a/matrix.c b/matrix.c index fa60b0b92e58580b99147f9c931b4c09219db6c2..9f3ab0bceea9a98517c5439705452a5e26aa26bf 100644 --- a/matrix.c +++ b/matrix.c @@ -1,30 +1,135 @@ -#include <stdio.h> + #include <stdlib.h> -#include <stdbool.h> +#include <stdio.h> +#include <string.h> +#include <assert.h> #include "matrix.h" + m_double* matrix_alloc(unsigned int row, unsigned int col){ - if(row == 0 || col == 0){ - return NULL; - } - m_double* matrix = calloc(1,sizeof(m_double)); - double ** mat_ptr = malloc(row*col*sizeof(double)); - matrix->content = malloc(row*col*sizeof(double*)) - for (int i=0;i<li;i++) mat->content[i] = data+i*co; + if (row <= 0 || col <= 0) return NULL; + m_double* matrix = (m_double*) calloc(1, sizeof(m_double)); + matrix->row = row; matrix->col = col; + double* mat_ptr = (double*) malloc(row*col*sizeof(double)); + matrix->content = (double*) malloc(row*sizeof(double)); + for (int i=0;i<row;i++) matrix->content[i] = mat_ptr+i*col; return matrix; +} + +void matrix_free(m_double* matrix) { + + free(matrix->content); + free(matrix); + matrix = NULL; } +m_double* matrix_transpose(m_double* matrix) { + assert(NULL != matrix); + m_double* transp = matrix_alloc(matrix->col,matrix->row); + + for (int i = 0; i<matrix->row; i++){ + for (int j = 0; j<matrix->col; j++){ + transp->content[j][i] = matrix->content[i][j]; + } + printf("\n"); + } + + return transp; +} -m_double* matrix_transpose(m_double* matrix){ - m_double transp = matrix_alloc(matrix->col,matrix->row); - for(int i=0;i<matrix->row;i++){ - for(int j=0;j<matrix->col;j++){ - transp[j][i] = matrix[i][j]; +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 diff --git a/matrix.h b/matrix.h index 4905544f7123c7376aa57c91749afbe7881f374e..0c589450fb113c3f88d1f7e5dd77a91b05a59866 100644 --- a/matrix.h +++ b/matrix.h @@ -6,3 +6,7 @@ typedef struct m_double { m_double* matrix_alloc(unsigned int row, unsigned int col); 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 diff --git a/matrix.o b/matrix.o index 4180cf6ac3e7f81eeca9ac7669e9928d8e04b74a..a992237fd6cce6412596cf3bc9be23b135a15ebe 100644 Binary files a/matrix.o and b/matrix.o differ diff --git a/matrix_compute.c b/matrix_compute.c index dbbd85ecccc5d2448d1ec6789047642fe726d971..29bb3ccafeb351964ea437623eba7036624d17ea 100644 --- a/matrix_compute.c +++ b/matrix_compute.c @@ -1 +1,13 @@ -int main(){} \ No newline at end of file +#include <stdio.h> +#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 diff --git a/matrix_compute.exe b/matrix_compute.exe index 2c3ce2bb599db4eeeaaf55a5829244da5d48c2d6..466c48cc43569509b9a25cd6407405a30d242680 100644 Binary files a/matrix_compute.exe and b/matrix_compute.exe differ diff --git a/matrix_compute.o b/matrix_compute.o index 8bdd7c1e0f37ce1dc7641608c1d1ebaaf04530c3..b1a7c647abc23595c1b6e4e233d709b31f117233 100644 Binary files a/matrix_compute.o and b/matrix_compute.o differ diff --git a/pointer_sort.exe b/pointer_sort.exe index 8d039c0d7d3fc7583840abc83733a9b2ef334fe1..8cc18ba5fc95872cf6b72e239126baf054cb4e36 100644 Binary files a/pointer_sort.exe and b/pointer_sort.exe differ