Recent

Author Topic: Pascal Gauss-Jordan  (Read 8791 times)

JhonDayStanley

  • Newbie
  • Posts: 5
Pascal Gauss-Jordan
« on: August 24, 2016, 08:37:27 pm »
I need help to make a program that uses the gauss jordan method to solve an equation and prints all the matrix , and the left pivot 1.

Example
4
1 2 3 4
8 14 15 3
9 2 5 8
7 0 2 5
5
6
2
1
End.
It is always a square matrix.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Pascal Gauss-Jordan
« Reply #1 on: August 24, 2016, 08:45:17 pm »

JhonDayStanley

  • Newbie
  • Posts: 5
Re: Pascal Gauss-Jordan
« Reply #2 on: August 25, 2016, 01:59:04 am »
I need it on pascal , here is the program explained, I think is for somebody that has advanced programming skills in pascal , I can't do it and really need help im a newbie.

Linear algebra

You want to create a system that solves systems of equations or inverse matrix (as appropriate). These systems have a square matrix up to 15x15 squares.

The information is given to read a file with the following format:
{Beginning of the input file format}
[N-side size of the matrix]
[Row 1 separate with spaces]
[Row2 separated with spaces]
.
.
.
[Row n separated with spaces]
[Data 1]
[Data 2]
.
.
.
[Data n]
{End of the input file format}

Example:
} {Start of example
4
1 2 3 4
8 14 15 3
9 2 5 8
7 0 2 5
5
6
2
1
{So} example

In the case of inverse matrix solve terms [Data 1] ... [Data n] they are not used.


The system must have the minimum options:

1) Read keyboard matrix
2) Read array from file
3) Solve problem (show the intermediate matrix and corresponding solution)
4) Exit the system.
« Last Edit: August 25, 2016, 02:01:18 am by JhonDayStanley »

lainz

  • Hero Member
  • *****
  • Posts: 4468
    • https://lainz.github.io/
Re: Pascal Gauss-Jordan
« Reply #3 on: August 25, 2016, 03:16:05 am »
What you know about Pascal programming in first place?

Readln writeln..

I can tell how i can i think how it can be solved

Create a tstringlist to load the file
Read first line to know how much you need to read
Create another string list and asign with commatext property the next line you need
Convert each string ítem from the secondary string list into an 2d array of integer
Repeat until you finish trading all lines you need, not more not less

From keyboard you can use readln to read the next int one by one use a For loop to set the appropriate ammount of reads
Of course store then in a secondary 2d array of int

You need a third 2d array of int to store the solución but is optional you can only do writeln to show the output
(I think, but i will use the array better For me)

JhonDayStanley

  • Newbie
  • Posts: 5
Re: Pascal Gauss-Jordan
« Reply #4 on: August 25, 2016, 03:29:19 am »
What you know about Pascal programming in first place?

Readln writeln..

I can tell how i can i think how it can be solved

Create a tstringlist to load the file
Read first line to know how much you need to read
Create another string list and asign with commatext property the next line you need
Convert each string ítem from the secondary string list into an 2d array of integer
Repeat until you finish trading all lines you need, not more not less

From keyboard you can use readln to read the next int one by one use a For loop to set the appropriate ammount of reads
Of course store then in a secondary 2d array of int

You need a third 2d array of int to store the solución but is optional you can only do writeln to show the output
(I think, but i will use the array better For me)

Well I know the basic , you know cycles , string , arrays , files , records.

I think it needs to be done with arrays , I have seem some codes and they use arrays , a friend found a program that is very similar to the one I want to do but is in french and it doesnt print the pivots , I mean the process in the which it is solving the matrix.

Handoko

  • Hero Member
  • *****
  • Posts: 5154
  • My goal: build my own game engine using Lazarus
Re: Pascal Gauss-Jordan
« Reply #5 on: August 25, 2016, 03:55:49 am »
I need it on pascal ...

Delphi is Pascal too, I'm I wrong?

The Gaussian elimination example in link I gave you has code in Delphi.

JhonDayStanley

  • Newbie
  • Posts: 5
Re: Pascal Gauss-Jordan
« Reply #6 on: August 25, 2016, 04:38:06 am »
I need it on pascal ...

Delphi is Pascal too, I'm I wrong?

The Gaussian elimination example in link I gave you has code in Delphi.

Yeah I saw , thank you bro , It's not the same I need to do but it helps a little bit :) Mine needs to do all the 0s and the 1s stuff.

Thaddy

  • Hero Member
  • *****
  • Posts: 14374
  • Sensorship about opinions does not belong here.
Re: Pascal Gauss-Jordan
« Reply #7 on: August 25, 2016, 10:45:16 am »
I attach a working FPC version. It is also multi-platform.
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Pascal Gauss-Jordan
« Reply #8 on: August 25, 2016, 11:26:19 am »
Thaddy, thanks for providing this implementation.
You forgot to free the matrices A, b, x, which gives a memory leak.
Also the TMatrix destructor is redundant, since the compiler already inserts code to clean up the dynamic arrays when they go out of scope.

Thaddy

  • Hero Member
  • *****
  • Posts: 14374
  • Sensorship about opinions does not belong here.
Re: Pascal Gauss-Jordan
« Reply #9 on: August 25, 2016, 11:46:08 am »
Thaddy, thanks for providing this implementation.
You forgot to free the matrices A, b, x, which gives a memory leak.
Also the TMatrix destructor is redundant, since the compiler already inserts code to clean up the dynamic arrays when they go out of scope.
Yes, I saw it. But these bugs are in the original code from rosettacode. I just made it compile in both delphi and fpc.
I will fix these (also on rosettacode). I left in/replaced some code for cleanup, because it may be redundant but good practise.
« Last Edit: August 25, 2016, 12:08:16 pm by Thaddy »
Object Pascal programmers should get rid of their "component fetish" especially with the non-visuals.

 

TinyPortal © 2005-2018