Daily programming challenge

Write a function that solves a system of 3 variables.
Example:

Please enter row1: 7,5,-3,16,EOF
Please enter row2: 3,-5,2,-8,EOF
Please enter row1: 5,3,-7, 0,EOF
Answer: (1,3,2)

Refer to sosmath.com/soe/SE311105/SE311105.html

Another example


Please enter row1: 4,-3,1,-10,EOF
Please enter row2: 2,1,3,0,EOF
Please enter row1: -1,2,-5,17,EOF
Answer: (1,4,-2)

Refer to
sparknotes.com/math/algebra2/systemsofthreeequations/section1.rhtml


>What is EOF
Short for End-Of-File, EOF is a code placed by a computer after a file's last byte of data. EOF marks are helpful in data transmission and storage. Files are stored in blocks, and the end marker helps the computer know it has allocated enough space to store the file.

The EOF is commonly represented by pressing and holding CTRL and pressing Z in DOS and OS/2 or pressing and holding CTRL and pressing D in Unix.

Other urls found in this thread:

en.wikipedia.org/wiki/Gaussian_elimination
purplemath.com/modules/cramers.htm
pastebin.com/9PHuD5S0
twitter.com/NSFWRedditImage

No fuck you.
I hate doing systems of equations on paper, I'm not gonna spend all afternoon writing your fucking homework.

How did you pass year 10?

idk lol

i never even took trig

Next /dpc/ will be on trig

wrong. 1, 3, and 5 should have the variable z, otherwise the question you're posing makes no sense (how can you output a three variable solution when you're only using two variables?).

Really, you've poorly defined the question. You should translate it like this:

1,2,3,4
translates to

1x+2y+3z+4=0

This whole eof thing is silly too and this is a poor way of implementing it into a problem to teach it to people. arguments should be sent to main or read from the input stream, no EOF is necessary (barring file reading), nor are commas, as that's up to your implementation choice.

I do like the idea of /dpc/ being a thing though, provided you're actually not just a bitch who can't do easy cs problems and is tryign to get g to do your homework

oh yes

4,-3,1,-10,EOF
2,1,3,0,EOF
-1,2,-5,17,EOF

translates to

4x-3y+z=-10
2x+y+3z=0
-x+2-5z=17

So the answer is: (x,y,z) = (1,4,-2)

Hey OP I'll play ball if you can explain how to solve systems of equations with matrices.

The example you posted introduces the number 17 out of nowhere, what the fuck.

Step 1: write system of equations in AX=B form
Step 2: look up and code how to inverse a matrix
Step 3: multiply inverse of A by B to get all X values

en.wikipedia.org/wiki/Gaussian_elimination

purplemath.com/modules/cramers.htm

>Gaussian elimination
I'd love to see an algorithm for that

Correction to my methods:
Step 1: write system of equations in AX=B form
Step 2: look up and code how to check determinants of matrices, if determinant not equal to 0: inverse matrix.
Step 3: look up and code how to inverse a matrix
Step 3: multiply inverse of A by B to get all X values

import Data.Matrix (Matrix, inverse, colVector, fromLists)
import Data.Vector (fromListN)

solve m c = (* colVector c) inverse m
inputMatrix i = fromLists mapM (const readLn) [1..i]
inputVector i = fromListN i readLn

main = do
ps

the Data.Matrix used is from the matrix package

why is your screenshot so small?

what's wrong with EOF?

This is actually really neat, thanks user!

I'll post my solution when I come home.

#include

typedef double vector3_t[3];

double det(vector3_t x, vector3_t y, vector3_t z) {
return x[0]*y[1]*z[2] + x[1]*y[2]*z[0] + x[2]*y[0]*z[1]
- z[0]*y[1]*x[2] - z[1]*y[2]*x[0] - z[2]*y[0]*x[1];
}

int main() {
vector3_t x, y, z, r;
scanf("%lf,%lf,%lf,%lf, ", &x[0], &y[0], &z[0], &r[0]);
scanf("%lf,%lf,%lf,%lf, ", &x[1], &y[1], &z[1], &r[1]);
scanf("%lf,%lf,%lf,%lf, ", &x[2], &y[2], &z[2], &r[2]);
double da = det(x, y, z);
if (da == 0)
printf("cannot solve\n");
else
printf("(%lf,%lf,%lf)\n", det(r, y, z)/da,
det(x, r, z)/da,
det(x, y, r)/da);
}

completely unnecessary in this use case. For example in C++ you can designate the delim character, so why the hell would you make the user type EOF (in whatever incarnation that takes in your operating system) when you #1 aren't using a file #2 are separating your inputs into three chunks.

It's a bad use case for EOF and concepts should be taught in use cases where they shine (provided said cases aren't extremely complex)

Do I win?

int det3(int mat[3][4])
{
/* get 2x2 matrix determinants */
unsigned i, j, r, x = 0, det = 0;
for (r = 0; r < 3; r++) /* current row */
{
unsigned a[4], idx = 0;
for (i = 0; i < 3; i++)
for (j = 0; j < 3; j++)
if (r != i && r != j)
a[idx++] = mat[i][j];
int neg = (r == 1) ? -1 : 1;
det += neg * (mat[r][x++] * ((a[0] * a[3]) - (a[1] * a[2])));
}
return det;
}

struct int3 systemofeq(int m[3][4])
{
int d[3], det = det3(m);
unsigned i, j;
for (i = 0; i < 3; i++)
{
int z[3][4];
unsigned k, l;
for (k = 0; k < 3; k++) /* copy */
for (l = 0; l < 4; l++)
z[k][l] = m[k][l];
for (j = 0; j < 3; j++) /* overwrite */
z[j][i] = m[j][3];
d[i] = det3(z);
}
struct int3 ans = { det / d[0], det / d[1], det / d[2] };
return ans;
}

There are times I wish I didn't drop out of college. This is one of them.

No it's ok.
Knowing how to solve a system of equations is pretty useless in the real world.

kek

import numpy as np

inp = np.zeros((3, 4))
for i in range(3):
inp[i, :] = eval(input())

mat = inp[:, :3]
vec = inp[:, 3]

np.linalg.solve(mat, vec)

[spoiler] bump [/spoiler]

Is this feels like cheating...
with Ada.Numerics.Generic_Real_Arrays;
with Ada.Text_IO; use Ada.Text_IO;

procedure System_of_Equations is
package RA is new Ada.Numerics.Generic_Real_Arrays(Long_Float);
use type RA.Real_Matrix;
use type RA.Real_Vector;
package Float_IO is new Ada.Text_IO.Float_IO(Long_Float);

A : RA.Real_Matrix(1 .. 3, 1 .. 3) := (
(7.0, 5.0, -3.0),
(3.0, -5.0, 2.0),
(5.0, 3.0, -7.0));

B : RA.Real_Vector(1 .. 3) := (16.0, -8.0, 0.0);

X_Linear_Algebra : RA.Real_Vector(1 .. 3) := RA.Inverse(A) * B;
X_Idiot_No_Maths : RA.Real_Vector(1 .. 3) := RA.Solve(A, B);

begin

-- uninteresting prints here

end;

>-- uninteresting prints here

But user, it that part couldn't be less interesting.

isn't it supposed to accept runtime input?

A = [7 5 3; 3 -5 2; 5 3 -7]
b = [16; -8; 0]
A\b
done.

>Not using matlab
for j=1:3
for i=1:4
prompt='enter input';
X(j,i)=input(prompt);
end
end

X(1:3,1:3)\X(1:3,4)

pastebin.com/9PHuD5S0

Lack of real matrix libraries isn't going stop ME from wasting my time solving the general case.

Gaussian elimination.

gauss1 :: (Eq a, Fractional a) => [[a]] -> [[a]]
gauss1 m
| length m [a]
gauss2 m
| length m == 1 = [(m!!0!!1)/(m!!0!!0)]
| otherwise = [newsolution] ++ solutions
where
solutions = gauss2 [[m!!i!!j | j 0] | i 0]
newsolution = ((m!!0!!(cols-1)) - sum [(m!!0!!j)*(solutions!!(j-1)) | j

You must not get those retarded FB posts that are basically linear algebra problems in picture form, or you've never been in a high paying field where applied math is the core of everything you do.

Nice

Problem is, that code is barely readable since I am not accustomed to functional programming

I bet it's terrible for those who are accustomed too, I just wanted it to work.