Understanding the math behind the code - c

I have a really basic and simple question but I am having problems with understanding this C code.
#define POLYNOMIAL(x) \
(((((3.0 * (x) + 2.0) * (x) - 5.0) * (x) - 1.0) * (x) + 7.0) * (x) - 6.0)
This definition is for this polynomial: 3x5+2x4-5x3-x2+7x-6
How can I convert this polynomial into the form shown in the #define? Is there any trick for this?

Your polynomial:
3x5 + 2x4 - 5x3 - x2 + 7x - 6
Can be rewritten successively:
(3x4 + 2x3 - 5x2 - x + 7) · x - 6
((3x3 + 2x2 - 5x - 1) · x + 7) · x - 6
(((3x2 + 2x - 5) · x - 1) · x + 7) · x - 6
((((3x + 2) · x - 5) · x - 1) · x + 7) · x - 6
This an expanded, or unrolled, Horner's Method loop. If the coefficients were expressed as an array:
double polynomial[] = { -6, 7, -1, -5, 2, 3 };
Then, the polynomial could be evaluated with this function:
double horners (double poly[], int terms, double x) {
double result = 0;
while (terms--) {
result = result * x + poly[terms];
}
return result;
}

Just add parenthesis and decrease out powers inside until you get to the last one, like this:
(3x^5)+(2x^4)-(5x^3)-(x^2)+7x-6
((3x^4)+(2x^3)-(5x^2)-x+7)x-6
(((3x^3)+(2x^2)-5x-1)x+7)x-6
((((3x^2)+2x-5)x-1)x+7)x-6
((((3x+2)x-5)x-1)x+7)x-6

Related

base-2: why DECIMAL_DIG - DIG == 2 or 3?

base-2: why DECIMAL_DIG - DIG == 2 or 3?
Examples:
FLT_DECIMAL_DIG - FLT_DIG == 9 - 6 == 3
DBL_DECIMAL_DIG - DBL_DIG == 17 - 15 == 2
LDBL_DECIMAL_DIG - LDBL_DIG == 21 - 18 == 3
FLT16_DECIMAL_DIG - FLT16_DIG == 5 - 3 == 2
Extra: Is it guaranteed that on any given implementation using base-2 this difference will be 2 or 3?
Yes, for base-2 radix, xxx_DECIMAL_DIG - xxx_DIG will be 2 or 3.
Informal proof
For a floating point type:
b is the base or radix or exponent representation (an integer > 1)
p is the precision (the number of base-b digits in the significand) (I assume this is > 1 and rational)
The xxx_DECIMAL_DIG values are defined as:
⎰ p log10 b — if b is a power of 10
⎱ ⌈1 + p log10 b⌉ — otherwise
The xxx_DIG values are defined as:
⎰ p log10 b — if b is a power of 10
⎱ ⌊(p - 1) log10 b⌋ — otherwise
For b = 2, log10 b ≈ 0.301 and is irrational (proof).
∴ p log10 2 is irrational (since p is rational and > 1).
∴ ⌈p log10 2⌉ - ⌊p log10 2⌋ = 1.
∴ ⌈1 + p log10 2⌉ - ⌊p log10 2⌋ = 2. — ①
⌊p log10 2⌋ - ⌊(p - 1) log10 2⌋ ∈ {0, 1}. — ② (since 0 < log10 2 < 1)
∴ ⌈1 + p log10 2⌉ - ⌊(p - 1) log10 2⌋ ∈ {2, 3}. — (from ① and ②) ∎
Given definitions of xxx_DECIMAL_DIG and xxx_DIG we have
2 <= ceil(1+p*log10(2)) - floor((p-1)*log10(2)) <= 3
Simplifying:
2 <= ceil(1+p*log10(2)) - floor((p-1)*log10(2)) <= 3
2 <= 1 + ceil( p*log10(2)) - floor((p-1)*log10(2)) <= 3
let q = p – 1
2 <= 1 + ceil( (1 + q) *log10(2)) - floor((p-1)*log10(2)) <= 3
2 <= 1 + ceil(log10(2) + q*log10(2)) - floor( q *log10(2)) <= 3
let R = trunc(q*log10(2))
let r = trunc(q*log10(2)) – R, r is [0.0 …1.0)
log10(2) = 0.301…
2 <= (1 + R + ceil(0.301… + r)) – (R + floor(r)) <= 3
2 <= 1 + ceil(0.301… + r) – floor(r) <= 3
2 <= 1 + ceil(0.301… + r) – 0 <= 3
When r <= 1.0 - 0.301…
2 <= 1 + 1 <= 3
Otherwise r > 1.0 - 0.301…
2 <= 1 + 2 <= 3
Even though it is shown that the difference is 2 or 3, I suppose the next questions are why are
xxx_DECIMAL_DIG: ceil(1+p*log10(2))
and
xxx_DIG: floor((p-1)*log10(2))?

Exponentiation complexity

Based on this code:
int poow(int x,int y)
{
if(y==0)
return 1;
if(y%2!= 0)
return poow(x,y-1)*x;
return poow(x,y/2)*poow(x,y/2); //this line
}
I tried to see the complexity: We suppose that we have n=2^k
we have T(0)=1
T(n)=2*T(n/2)+C
T(n)=2^i * T(n/2^i)+i*c
for i=k we have T(n)=2^k * T(n/2^k) + k * c
T(n)=2^k * T(1) + k*c
T(n)=2^k * c2 + k * c
I am stuck here ? How can I continue the computation of complexity and what is the difference when changing this line:
return poow(x,y/2)*poow(x,y/2); //this line
with
int p=poow(x,y/2);
return p*p;
in term of complexity !
Start off with a proper recurrence. The complexity is solely based on y, so we can write the recurrence as
T(0) = 1
T(y) = y is even: 2 * T(y / 2)
y is odd: T(y - 1) + 1
Worst-case would be that every division by 2 leaves us with a odd number, which would lead to the complexity of
T(2^n-1) = 1 + 2 * (1 + 2 * (1 + 2 * ( ... * T(1)))) =
= 2 ^ 0 + 2 ^ 1 + 2 ^ 2 + 2 ^ 3 + ... + 2 ^ (n - 1) + 2 ^ (n - 1) =
= 2 ^ n - 1 + 2 ^(n - 1) = 3 * 2 ^ (n - 1) - 1
T(y) = O(y)
Best-case would be a power of 2:
T(2^n) = 2 * 2 * ... * 2 * T(1) = 2 ^ n * (1 + 1) = 2 ^ (n + 1) = 2 * 2 ^ n
T(y) = O(y)
Now what if we optimized the whole function?
T'(0) = 1
T'(y) = y is even: T(y / 2) + 1
y is odd: T(y - 1) + 1
Worst case:
T'(2^n - 1) = T(2^n - 2) + 1 = T(2^(n - 1) - 1) + 1 + 1 = ... =
= T(1) + 1 + 1 + 1 + ... =
= 2 + 1 + 1 + 1 + ... =
= 1 + ln(2^n) / ln(2) * 2 =
= 1 + 2 * n
T'(y) = O(log y)
Best case:
T'(2 ^ n) = T(1) + 1 + 1 + ... =
= 2 + 1 + 1 + ... =
= 2 + ln(2^n) / ln(2)
= n + 2
T'(y) = O(log y)
So the optimized version is definitely faster (linear vs logarithmic complexity).
Sometime code analysis focuses on the theory and not the actual.
Code has a bug.
if(y%2!= 0)
return poow(x,y-1)*x;
// should be
if(y%2!= 0)
return poow(x,y-y%2)*x;
// better alternative, use
unsigned poow(unsigned x, unsigned y)
Without the fix, calling poow(1,-1) causes infinite recursion and likely stack-overflow. So O(∞) regardless in the last line is return poow(x,y/2)*poow(x,y/2); or int p=poow(x,y/2);
return p*p;.

C arithmetic precedence

I stumbled upon a question about arithmetic precedence in a test and I cannot wrap my head at all around its answer.
float x = 5 % 3 * + 2 - 4.5 / 5 * 2 + 2;
My "understanding" right now is that multiplication must take place first before division and modulus, yet when I try using that approach, the answer is 6.55 instead of 4.20. I tried playing around with the expression (adding brackets here and there), and it turns out that 5 % 3 takes place first before everything else. I just don't understand why since, according to the precedence table I was provided, that shouldn't be the case. Could someone clear this up for me?
Please refer to the documentation here.
The precedence of multiplication, division and remainder operators are higher than those of sum and subtraction.
When multiplication, division or remainder operators go one after another, then they are left-associated, meaning they will be executed one by one in the given order.
In your example 5 % 3 will be performed first, then the multiplication (by whatever number there is), then the division 4.5 / 5, then multiplication of the result by 2, and only after all that will the sum and subtraction be performed.
Your C code:
x = 5 % 3 * + 2 - 4.5 / 5 * 2 + 2;
First, unary plus and unary minus has the highest precedence:
x = 5 % 3 * (+ 2) - 4.5 / 5 * 2 + 2;
Second, multiplication, division, and remainder have the same precedence, associated from left to right:
x = ((5 % 3) * (+ 2)) - ((4.5 / 5) * 2) + 2;
Last, addition and subtraction have the same precedence, associated from left to right:
x = ((((5 % 3) * (+ 2)) - ((4.5 / 5) * 2)) + 2);
Now we evaluate the expression:
x = (((2 * (+ 2)) - ((4.5 / 5) * 2)) + 2);
x = (((2 * 2) - ((4.5 / 5) * 2)) + 2);
x = ((4 - ((4.5 / 5) * 2)) + 2);
x = ((4 - (0.9 * 2)) + 2);
x = ((4 - 1.8) + 2);
x = (2.2 + 2);
x = 4.2;
you can refers this link for more detail
http://www.difranco.net/compsci/C_Operator_Precedence_Table.htm

Understanding CEILING macro use cases

I've found the following macro in a utility header in our codebase:
#define CEILING(x,y) (((x) + (y) - 1) / (y))
Which (with help from this answer) I've parsed as:
// Return the smallest multiple N of y such that:
// x <= y * N
But, no matter how much I stare at how this macro is used in our codebase, I can't understand the value of such an operation. None of the usages are commented, which seems to indicate it is something obvious.
Can anyone offer an English explanation of a use-case for this macro? It's probably blindingly obvious, I just can't see it...
Say you want to allocate memory in chunks (think: cache lines, disk sectors); how much memory will it take to hold an integral number of chunks that will contain the X bytes? If the chuck size is Y, then the answer is: CEILING(X,Y)
When you use an integer division in C like this
y = a / b
you get a result of division rounded towards zero, i.e. 5 / 2 == 2, -5 / 2 == -2. Sometimes it's desirable to round it another way so that 5 / 2 == 3, for example, if you want to take minimal integer array size to hold n bytes, you would want n / sizeof(int) rounded up, because you want space to hold that extra bytes.
So this macro does exactly this: CEILING(5,2) == 3, but note that it works for positive y only, so be careful.
Hmm... English example... You can only buy bananas in bunches of 5. You have 47 people who want a banana. How many bunches do you need? Answer = CEILING(47,5) = ((47 + 5) - 1) / 5 = 51 / 5 = 10 (dropping the remainder - integer division).
Let's try some test values
CEILING(6, 3) = (6 + 3 -1) / 3 = 8 / 3 = 2 // integer division
CEILING(7, 3) = (7 + 3 -1) / 3 = 9 / 3 = 3
CEILING(8, 3) = (8 + 3 -1) / 3 = 10 / 3 = 3
CEILING(9, 3) = (9 + 3 -1) / 3 = 11 / 3 = 3
CEILING(10, 3) = (9 + 3 -1) / 3 = 12 / 3 = 4
As you see, the result of the macro is an integer, the smallest possible z which satisfies: z * y >= x.
We can try with symbolics, as well:
CEILING(k*y, y) = (k*y + y -1) / y = ((k+1)*y - 1) / y = k
CEILING(k*y + 1, y) = ((k*y + 1) + y -1) / y = ((k+1)*y) / y = k + 1
CEILING(k*y + 2, y) = ((k*y + 2) + y -1) / y = ((k+1)*y + 1) / y = k + 1
....
CEILING(k*y + y - 1, y) = ((k*y + y - 1) + y -1) / y = ((k+1)*y + y - 2) / y = k + 1
CEILING(k*y + y, y) = ((k*y + y) + y -1) / y = ((k+1)*y + y - 1) / y = k + 1
CEILING(k*y + y + 1, y) = ((k*y + y + 1) + y -1) / y = ((k+2)*y) / y = k + 2
You canuse this to allocate memory with a size multiple of a constant, to determine how many tiles are needed to fill a screen, etc.
Watch out, though. This works only for positive y.
Hope it helps.
CEILING(x,y) gives you, assuming y > 0, the ceiling of x/y (mathematical division). One use case for that would be a prime sieve starting at an offset x, where you'd mark all multiples of the prime y in the sieve range as composites.

Differentiating sums with Maxima

I have the following sum:
sum((R[i]-(a*X[i]+b)*t + 1/2*(c*X[i]+d)^2*t)^2/((c*X[i]+d)^2*t), i, 1, N);
which I want to differenciate wrt. a:
diff(%, a);
but Maxima (wxMaxima to be precise) just prints d/da . Can I
make it actually differentiate the sum (so because N is finite is
should differentiate every element in the sum separately)?
If I set N to some constant, e.g.:
sum((R[i]-(a*X[i]+b)*t + 1/2*(c*X[i]+d)^2*t)^2/((c*X[i]+d)^2*t), i, 1, 100);
then I get explicit sum of 100 elements (takes about 2 pages), and
then differentiation works (but again I get 2 pages instead of a small
sum). Can I get this result displayed as a sum?
Which version of Maxima do you use ?
Here is my session of Maxima with you equation differentiated wrt.a and than substituted to N=100.
~$ maxima
Maxima 5.24.0 http://maxima.sourceforge.net
using Lisp SBCL 1.0.51
Distributed under the GNU Public License. See the file COPYING.
Dedicated to the memory of William Schelter.
The function bug_report() provides bug reporting information.
(%i1) sum((R[i]-(a*X[i]+b)*t + 1/2*(c*X[i]+d)^2*t)^2/((c*X[i]+d)^2*t), i, 1, N);
2
(c X + d) t
N i 2
==== (------------- - (a X + b) t + R )
\ 2 i i
> ------------------------------------
/ 2
==== (c X + d)
i = 1 i
(%o1) ------------------------------------------
t
(%i2) diff(%, a);
2
(c X + d) t
N i
==== X (------------- - (a X + b) t + R )
\ i 2 i i
(%o2) - 2 > --------------------------------------
/ 2
==== (c X + d)
i = 1 i
(%i3) %, N=100;
2
(c X + d) t
100 i
==== X (------------- - (a X + b) t + R )
\ i 2 i i
(%o3) - 2 > --------------------------------------
/ 2
==== (c X + d)
i = 1 i

Resources