[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

Daily programming challenge

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 38
Thread images: 5

File: 6AQjT.jpg (83KB, 1680x943px) Image search: [Google]
6AQjT.jpg
83KB, 1680x943px
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 http://www.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
http://www.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.
>>
No fuck you.
I hate doing systems of equations on paper, I'm not gonna spend all afternoon writing your fucking homework.
>>
>>59039175
How did you pass year 10?
>>
>>59039192
idk lol

i never even took trig
>>
>>59039206
Next /dpc/ will be on trig
>>
>>59038960
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.
>>
>>59039360
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
>>
>>59039360
oh yes

>>59038891

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.
>>
>>59038891
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
>>
>>59039424
https://en.wikipedia.org/wiki/Gaussian_elimination
>>
File: maxresdefault.jpg (62KB, 1920x1080px) Image search: [Google]
maxresdefault.jpg
62KB, 1920x1080px
>>59039424
http://www.purplemath.com/modules/cramers.htm
>>
>>59039503
>Gaussian elimination
I'd love to see an algorithm for that
>>
>>59039502
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
>>
File: solver.png (4KB, 211x250px) Image search: [Google]
solver.png
4KB, 211x250px
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 <- solve <$> inputMatrix 3 <*> inputVector 3
either putStrLn print (ps :: Either String (Matrix Double))


not the same interface but you get the point
>>
>>59039868
the Data.Matrix used is from the matrix package
>>
>>59039868
why is your screenshot so small?
>>
>>59039360
what's wrong with EOF?
>>
>>59039507
This is actually really neat, thanks anon!

I'll post my solution when I come home.
>>
>>59038891
#include <stdio.h>

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);
}
>>
>>59040492
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.
>>
>>59042766
No it's ok.
Knowing how to solve a system of equations is pretty useless in the real world.
>>
>>59042899
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)
>>
File: Godfather.jpg (200KB, 1131x1600px) Image search: [Google]
Godfather.jpg
200KB, 1131x1600px
[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;
>>
>>59045541
>-- uninteresting prints here
>>
>>59045555
But anon, it that part couldn't be less interesting.
>>
>>59045695
isn't it supposed to accept runtime input?
>>
File: a58.jpg (59KB, 590x589px) Image search: [Google]
a58.jpg
59KB, 590x589px
>>59038891
A = [7 5 3; 3 -5 2; 5 3 -7]
b = [16; -8; 0]
A\b
done.
>>
>>59038891
>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)
>>
http://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 <= 1 = m
| otherwise = [m!!0] ++
(map ([0]++) (gauss1 $ [[(m!!i!!j) - ((k!!i)*(m!!0!!j)) |
j <- cols, j > 0] | i <-rows, i > 0]))
where
k = [(m!!i!!0)/(m!!0!!0) | i <- rows]
cols = [0..((length $ m!!0)-1)]
rows = [0..((length m) -1)]

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

gauss = gauss2 . gauss1
>>
>>59042899
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.
>>
>>59048058
Nice

Problem is, that code is barely readable since I am not accustomed to functional programming
>>
>>59050691
I bet it's terrible for those who are accustomed too, I just wanted it to work.
Thread posts: 38
Thread images: 5


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.