wip implemented new union for solve
This commit is contained in:
@@ -172,10 +172,11 @@ void launch_computorv1(char *input)
|
||||
print_reduced_form(polynom);
|
||||
print_degree(polynom, degree);
|
||||
|
||||
// // solve
|
||||
// s_solution solution[1];
|
||||
// solution_g_err = solution;
|
||||
// solve(polynom, solution);
|
||||
// solve
|
||||
s_solution solution[1];
|
||||
solution_g_err = solution;
|
||||
solution[0].degree = degree;
|
||||
solve(polynom, solution);
|
||||
|
||||
// print solution
|
||||
|
||||
|
||||
@@ -58,7 +58,7 @@ int reduce(s_term *terms, s_polynom *polynom, int max_exponent)
|
||||
// fill polynom
|
||||
polynom[i].coefficient = tmp_coefficient;
|
||||
polynom[i].exponent = max_exponent;
|
||||
polynom[i].sign = ft_sign_f(tmp_coefficient) == 1 ? TERM_PLUS : TERM_MINUS;
|
||||
polynom[i].sign = ft_fsign(tmp_coefficient) == 1 ? TERM_PLUS : TERM_MINUS;
|
||||
|
||||
max_exponent--;
|
||||
i++;
|
||||
|
||||
70
src/solver.c
70
src/solver.c
@@ -69,32 +69,43 @@ static int find_gcd(double numerator, double denominator)
|
||||
}
|
||||
|
||||
/*
|
||||
e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
|
||||
double delta_absolute; // |Δ| == |b² - 4ac|
|
||||
int first_term_gcd; // gcd(b, 2a)
|
||||
int first_term_numerator; // -b / gcd
|
||||
int first_term_denominator; // 2a / gcd
|
||||
double first_term; // double (-b / 2a)
|
||||
int second_term_gcd; // gcd(√|Δ|, 2a)
|
||||
int second_term_numerator; // √|Δ| / gcd
|
||||
int second_term_denominator; // 2a / gcd
|
||||
double second_term; // double (√|Δ| / 2a)
|
||||
double solution1; // first_term + second_term
|
||||
double solution2; // first_term - second_term
|
||||
typedef struct
|
||||
{
|
||||
double a; // a in "ax + b"
|
||||
double b; // b in "ax + b"
|
||||
int solution_gcd; // gcd(b, a)
|
||||
int solution_numerator; // -b / gcd
|
||||
int solution_denominator; // a / gcd
|
||||
double solution; // double (-b / a)
|
||||
} s_solution_degree_1;
|
||||
*/
|
||||
/*
|
||||
typedef struct
|
||||
{
|
||||
double a; // a in "ax² + bx + c"
|
||||
double b; // b in "ax² + bx + c"
|
||||
double c; // c in "ax² + bx + c"
|
||||
e_delta_sign delta_sign; // DELTA_PLUS or DELTA_MINUS or DELTA_ZERO
|
||||
double delta_absolute; // |Δ| == |b² - 4ac|
|
||||
int first_term_gcd; // gcd(b, 2a)
|
||||
int first_term_numerator; // -b / gcd
|
||||
int first_term_denominator; // 2a / gcd
|
||||
double first_term; // double (-b / 2a)
|
||||
int second_term_gcd; // gcd(√|Δ|, 2a)
|
||||
int second_term_numerator; // √|Δ| / gcd
|
||||
int second_term_denominator; // 2a / gcd
|
||||
double second_term; // double (√|Δ| / 2a)
|
||||
double solution1; // first_term + second_term
|
||||
double solution2; // first_term - second_term (not if DELTA_ZERO)
|
||||
} s_solution_degree_2;
|
||||
*/
|
||||
|
||||
void solve(const s_polynom *polynom, s_solution *solution)
|
||||
static void solve_degree_2(s_solution_degree_2 *solution, double a, double b, double c)
|
||||
{
|
||||
double delta;
|
||||
double a;
|
||||
double b;
|
||||
double c;
|
||||
|
||||
a = polynom[2].coefficient;
|
||||
b = polynom[1].coefficient;
|
||||
c = polynom[0].coefficient;
|
||||
delta = b * b - 4 * a * c;
|
||||
solution->delta_sign = ft_sign_f(delta);
|
||||
solution->delta_sign = ft_fsign(delta);
|
||||
solution->delta_absolute = ft_fabs(delta);
|
||||
solution->first_term_gcd = find_gcd(b, 2 * a);
|
||||
solution->first_term_numerator = -b / solution->first_term_gcd;
|
||||
@@ -114,3 +125,22 @@ void solve(const s_polynom *polynom, s_solution *solution)
|
||||
// solution->solution2 = 0.0;
|
||||
// }
|
||||
}
|
||||
|
||||
void solve(const s_polynom *polynom, s_solution *solution)
|
||||
{
|
||||
double a;
|
||||
double b;
|
||||
double c;
|
||||
|
||||
if (solution->degree == 1)
|
||||
{
|
||||
}
|
||||
else if (solution->degree == 2)
|
||||
{
|
||||
a = polynom[0].coefficient;
|
||||
b = polynom[1].coefficient;
|
||||
c = polynom[2].coefficient;
|
||||
|
||||
solve_degree_2(&solution->solution_degree_2, a, b, c);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -71,18 +71,27 @@ static void print_context_polynom()
|
||||
|
||||
static void print_context_solution()
|
||||
{
|
||||
dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_g_err->delta_sign));
|
||||
dprintf(STDERR_FILENO, "delta_absolute : %25g (|b² - 4ac|)\n", solution_g_err->delta_absolute);
|
||||
dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_g_err->first_term_gcd);
|
||||
dprintf(STDERR_FILENO, "first_term_numerator : %25i (-b / gcd)\n", solution_g_err->first_term_numerator);
|
||||
dprintf(STDERR_FILENO, "first_term_denominator : %25i (2a / gcd)\n", solution_g_err->first_term_denominator);
|
||||
dprintf(STDERR_FILENO, "first_term : %25g (-b / 2a)\n", solution_g_err->first_term);
|
||||
// dprintf(STDERR_FILENO, "second_term_gcd : %25g\n", solution_g_err->second_term_gcd);
|
||||
// dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_g_err->second_term_numerator);
|
||||
// dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_g_err->second_term_denominator);
|
||||
// dprintf(STDERR_FILENO, "second_term : %25g\n", solution_g_err->second_term);
|
||||
// dprintf(STDERR_FILENO, "solution1 : %25g\n", solution_g_err->solution1);
|
||||
// dprintf(STDERR_FILENO, "solution2 : %25g\n", solution_g_err->solution2);
|
||||
s_solution_degree_2 solution_2;
|
||||
|
||||
if (solution_g_err->degree == 2)
|
||||
{
|
||||
solution_2 = solution_g_err->solution_degree_2;
|
||||
dprintf(STDERR_FILENO, "degree 2 : delta > 0 ( -b/2a +- √Δ/2a )\n");
|
||||
dprintf(STDERR_FILENO, " delta == 0 ( -b/2a )\n");
|
||||
dprintf(STDERR_FILENO, " delta < 0 ( -b/2a +- i√|Δ|/2a )\n");
|
||||
dprintf(STDERR_FILENO, "delta_sign : %25s\n", delta_sign_to_str(solution_2.delta_sign));
|
||||
dprintf(STDERR_FILENO, "delta_absolute : %25g (|b² - 4ac|)\n", solution_2.delta_absolute);
|
||||
dprintf(STDERR_FILENO, "first_term_gcd : %25i\n", solution_2.first_term_gcd);
|
||||
dprintf(STDERR_FILENO, "first_term_numerator : %25i (-b / gcd)\n", solution_2.first_term_numerator);
|
||||
dprintf(STDERR_FILENO, "first_term_denominator : %25i (2a / gcd)\n", solution_2.first_term_denominator);
|
||||
dprintf(STDERR_FILENO, "first_term : %25g (-b / 2a)\n", solution_2.first_term);
|
||||
// dprintf(STDERR_FILENO, "second_term_gcd : %25g\n", solution_2.second_term_gcd);
|
||||
// dprintf(STDERR_FILENO, "second_term_numerator : %25g\n", solution_2.second_term_numerator);
|
||||
// dprintf(STDERR_FILENO, "second_term_denominator: %25g\n", solution_2.second_term_denominator);
|
||||
// dprintf(STDERR_FILENO, "second_term : %25g\n", solution_2.second_term);
|
||||
// dprintf(STDERR_FILENO, "solution1 : %25g\n", solution_2.solution1);
|
||||
// dprintf(STDERR_FILENO, "solution2 : %25g\n", solution_2.solution2);
|
||||
}
|
||||
|
||||
ft_putchar_fd('\n', STDERR_FILENO);
|
||||
}
|
||||
|
||||
@@ -49,24 +49,24 @@ const char *term_position_to_str(e_term_position enum_value)
|
||||
|
||||
const char *term_sign_to_str(e_term_sign enum_value)
|
||||
{
|
||||
if (enum_value > TERM_SIGN_END || enum_value < TERM_PLUS)
|
||||
if (enum_value == TERM_PLUS)
|
||||
return "TERM_PLUS";
|
||||
else if (enum_value == TERM_MINUS)
|
||||
return "TERM_MINUS";
|
||||
else if (enum_value == TERM_SIGN_END)
|
||||
return "TERM_SIGN_END";
|
||||
else
|
||||
return "invalid enum value";
|
||||
|
||||
static const char *const term_sign_str[TERM_SIGN_END + 1] = {
|
||||
[TERM_PLUS] = "TERM_PLUS",
|
||||
[TERM_MINUS] = "TERM_MINUS",
|
||||
[TERM_SIGN_END] = "TERM_SIGN_END"};
|
||||
return term_sign_str[enum_value];
|
||||
}
|
||||
|
||||
const char *delta_sign_to_str(e_delta_sign enum_value)
|
||||
{
|
||||
if (enum_value > DELTA_ZERO || enum_value < DELTA_PLUS)
|
||||
if (enum_value == DELTA_PLUS)
|
||||
return "DELTA_PLUS";
|
||||
else if (enum_value == DELTA_MINUS)
|
||||
return "DELTA_MINUS";
|
||||
else if (enum_value == DELTA_ZERO)
|
||||
return "DELTA_ZERO";
|
||||
else
|
||||
return "invalid enum value";
|
||||
|
||||
static const char *const delta_sign_str[DELTA_ZERO + 1] = {
|
||||
[DELTA_PLUS] = "DELTA_PLUS",
|
||||
[DELTA_MINUS] = "DELTA_MINUS",
|
||||
[DELTA_ZERO] = "DELTA_ZERO"};
|
||||
return delta_sign_str[enum_value];
|
||||
}
|
||||
Reference in New Issue
Block a user