Need explanation: Equation solving code

Hello

A few months ago I found a code which was able to solve equations.
At first I didn't understood it at all, I tried examining the code and with some support I now understand how the operators, structs, overloads, hooking (with line 59 - via overloading),... works

I accomplished to understand the whole code, EXCEPT the mathematical and logical part - the actually most important part -.

I'd really appreciate it, if someone was able to explain me what the math-part in the constructors +,-,/,* do, and work.

How is this piece code for example, able to sum up two terms?
1
2
3
4
5
6
7
8
LINE operator + (LINE A,LINE B) {	//assumes that A.x == 0 or B.x == 0 or A.x == B.x
	LINE R;
	R.a = A.a + B.a;
	R.k = A.k + B.k;
	if(A.x) R.x = A.x;
	else R.x = B.x;
	return R;
}


Full code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <iostream>
#include <math.h>

struct VAR{
	float i;
};

struct LINE{	//k*x+a
	float a,k;
	VAR* x;

	LINE(){}
	LINE(int a): a(a),k(0),x(0) {}
	LINE(VAR& v): a(0),k(1),x(&v) {}
};

LINE operator + (LINE A,LINE B) {	//assumes that A.x == 0 or B.x == 0 or A.x == B.x
	LINE R;
	R.a = A.a + B.a;
	R.k = A.k + B.k;
	if(A.x) R.x = A.x;
	else R.x = B.x;
	return R;
}

LINE operator - (LINE A,LINE B) {	//same as +
	LINE R;
	R.a = A.a - B.a;
	R.k = A.k - B.k;
	if(A.x) R.x = A.x;
	else R.x = B.x;
	return R;
}

LINE operator * (LINE A,LINE B) {	//assumes that A.x == 0 or B.x == 0
	LINE R;
	R.a = A.a * B.a;
	R.k = A.k * B.a + B.k * A.a;
	if(A.x) R.x = A.x;
	else R.x = B.x;
	return R;
}

LINE operator / (LINE A,LINE B) {	//assumes that B.x == 0
	LINE R;
	R.a = A.a / B.a;
	R.k = A.k / B.a;
	R.x = A.x;
	return R;
}

void operator == (LINE A,LINE B)	{
	LINE C = A - B;
	C.x->i = -C.a/C.k;
}

int main(){
	VAR x;
	4*x == 2*2;

	std::cout << "x = " << x.i;
	std::cin.get();

	return 0;
}



Other things which would be nice to have it explained:
- Line 12 - 14 (the last three lines of where the struct LINE is initialised),
- Some more info about the operator overloading part here,
- More explanation about the constructors used in this code;

I'd really like to understand how this code works.
Thanks a lot all!
Last edited on
What have you found?

It looks like some sort of mx + b type of equation. Though I'm not really sure beyond that. What exactly is the program this struct is used in?
1
2
3
4
5
6
7
8
LINE operator + (LINE A,LINE B) {	//assumes that A.x == 0 or B.x == 0 or A.x == B.x
	LINE R;
	R.a = A.a + B.a;
	R.k = A.k + B.k;
	if(A.x) R.x = A.x;
	else R.x = B.x;
	return R;
}


Line 1: operator+ first takes in two parameters of object LINE

Line 2: declare a local LINE object

Line 3: call the constructor and initialize values to float a

Line 4: same thing as line 3, but with k and 0

Line 5-6: I'm thinking that's for if A.x has a value other than 0, then do first line, otherwise do the else

- Line 12 - 14 (the last three lines of where the struct LINE is initialised),
- Some more info about the operator overloading part here,
- More explanation about the constructors used in this code;


Last 3 lines are just different constructors. The colon and all the variables with the parentheses are just shorthand for initializing variables to a certain value within the constructor.

Operators are functions too, if you don't define them for custom object classes, you can't use them. You can't call int multiply( int n ) and then try to insert a string in there.

@Kean: the full code is posted above.
The equation in this code:
4x = 2*2

The result is of course 1.

@YFGHN:
Thanks a lot for your explanation!
Still I'd like to know a step-by-step explanation on how it's calculated. How does this program calculates the equation from the equation to finding 'x'.
Sometimes it can help to just step through the code and work out what's going on.

On line 59, 4 and x get implicitly converted to LINEs so as to provide a valid multiplication/equality operator, and the same for the 2*2 on the other side.

So, on one side we have a LINE with the value a=0;k=4, and on the other side wie have a LINE with the value a=4;k=0. That is, we have represented the equation 4x=4.

Now, to work out x in this circumstance is down to the == operator. We get the LINE C, with values a=-4;k=4. Finally, we set the value of our variable x to -(-4)/4, and we get the result 1.

---

Basically, each LINE has a variable which is passed around as a pointer, and whenever we do an operation on it we modify k (the coefficient) and a (the offset).

Finally, at the end it all culminates with k1*x+a1 == k2*x+a2, where we move all x to one side and all a to the other, and then store the result in our pointer to x, which we can retrieve at our leisure.

Does that help?
Last edited on
Topic archived. No new replies allowed.