Netlogo reduce polynomial example - netlogo

In the Netlogo dictionary for "reduce" they show an example with one "+" operator
reduce [?1 + ?2] [1 2 3 4]
which they expand as equivalent to (((1 + 2) + 3) + 4).
Later they give this example:
;; evaluate the polynomial, with given coefficients, at x
to-report evaluate-polynomial [coefficients x]
report reduce [(x * ?1) + ?2] coefficients
end
;; evaluate 3x^2 + 2x + 1 at x = 4
show evaluate-polynomial [3 2 1] 4
=> 57
What is the equivalent expansion (using parentheses) for that evaluation?

observer> show (4 * ((4 * 3) + 2)) + 1
observer: 57
The key to understand it is to do it step by step. reduce starts by taking the first two elements of the list and plugging them into ?1 and ?2, so
(x * ?1) + ?2
becomes
(x * 3) + 2
That whole expression then becomes ?1, and the last element of the list, 1, becomes ?2. Replacing ?1 and ?2 in the initial expression again, we get:
(x * ((x * 3) + 2)) + 1
All that's left is to replace x with 4:
(4 * ((4 * 3) + 2)) + 1

Related

How do you factor over a Z field?

I have to factorize a polynomial e.g.
over the field of Z5 using Matlab or Mupad.
And i tried everything read a lot of Matlab and Mupad documentation and still can't find it, so i am guessing it is the math i don't know that's going to help me factor it.
Don't kill a mosquito with a cannon!
You only need to find a root between 0, 1, 2, -2, -1.
Also, given that x5 = x, the problem reduces to finding x such that
2x + 2x^4 + x^3 + 2x^2 - 3 = 0
and since x ≠ 0, x^4 = 1 hence
2x + x^3 + 2x^2 - 1 = 0
Well, let's try!
1: 2 + 1 + 2 - 1 -> -1
2: -1 + 3 - 2 - 1 -> -1
-2: 1 - 3 + 3 - 1 -> 0 -> root!
Then the polynomial is divisible by (x - 3), and you can repeat the procedure with the quotient until there are no roots left.
Addendum
After dividing by (x - 3) we get
x4 + x2 + 1
which we can expressed as
(x2 + 1)2 - x2
or
((x2 + 1) - x)((x2 + 1) + x)
To find the factors of degree 2 programmatically, just try with x2 + ax + b for a and b between 0 and 4.
I found a mupad command to do what i needed.
Still thanks for exaplaining the math behind it.

Recursive procedure for sum of squares of first n odd numbers?

I'm trying to implement recursive procedure for sum of squares of first n odd numbers on Racket
(starting with 1)
e.g., (sum-alt-squares-recursive 0) is 0
(sum-alt-squares-recursive 1) is 1 (1^2)
(sum-alt-squares-recursive 2) is 10 (3^2 + 1^2)
(sum-alt-squares-recursive 3) is 35 (5^2 + 3^2 + 1^2)
This is a recursive procedure that uses a linear iterative process
(define (sum-alt-squares-recursive x (y 1) (z 0))
(if (zero? x)
z ; if x is zero, return accumulator z
(sum-alt-squares-recursive (- x 1) ; x goes down by 1 each iteration
(+ y 2) ; y starts at 1 and goes up by 2 each iteration
(+ z (* y y)) ; z, goes up by y^2 each time
)))
(sum-alt-squares-recursive 1) ; => 1
(sum-alt-squares-recursive 2) ; => 10
(sum-alt-squares-recursive 3) ; => 35

Set range of values for turtles

I need to set a range of values for a turtle, the range must go from >= 3 to <= 5 .
I wrote this code
if ((a) + (b) + (c) >= 3 <= 5) [set pcolor gray]
But I did not get what I expected
let value a + b + c
if 3 <= value and value <= 5 [ set pcolor gray ]
Edit:
So I put in the let value a + b + c to simplify the code, but that might be confusing. Here's the version that most closely matches what you have in your question:
if ((3 <= a + b + c) and (a + b + c <= 5)) [ set pcolor gray ]
That said, I recommend using a local variable as I did above so you don't have to write out a + b + c multiple times.

Understanding recursive function with implicit return statement

Below recursive method sums the integer values between a range
def sumInts(a: Int, b: Int): Int = {
if(a > b) 0
else {
println(a +"," + b)
a + sumInts(a + 1 , b)
}
}
So sumInts(2 , 5) returns 14
I'm confused about how the recursive call to sumInts sums the integer range. Can explain textually how this method works ?
How does sumInts return the incremented value ?? Perhaps I am missing something fundamental to recursion here
It calculates the sum of values in the range [a, b] by first calculating the sum of the range [a+1, b] (by recursively calling sumInts(a + 1 , b)) then adding a to it.
[Update] In Scala, the return statement is optional; functions return the value of the last expression evaluated. Thus the above function body is equivalent to
if(a > b) return 0
else {
println(a +"," + b)
return a + sumInts(a + 1 , b)
}
[/Update]
Which for the range [2, 5] it would do the following (I removed the println call for the sake of simplicity, and added brackets to mark recursive calls):
if(2 > 5) 0 else 2 + sumInts(2 + 1, 5) which, the condition being false, evaluates to
2 + sumInts(3, 5)
2 + (if(3 > 5) 0 else 3 + sumInts(3 + 1, 5)) which evaluates to
2 + (3 + sumInts(4, 5))
2 + (3 + (if(4 > 5) 0 else 4 + sumInts(4 + 1, 5))) which evaluates to
2 + (3 + (4 + sumInts(5, 5)))
2 + (3 + (4 + (if(5 > 5) 0 else 5 + sumInts(5 + 1, 5)))) which evaluates to
2 + (3 + (4 + (5 + sumInts(6, 5))))
2 + (3 + (4 + (5 + (if(6 > 5) 0 else 6 + sumInts(6 + 1, 5))))) which, the condition being true, evaluates to
2 + (3 + (4 + (5 + (0))))

What Is The Purpose of Negative Modulus Operator Results?

I was previously under the (naive) assumption that the modulus operator returned the remainder of division. I was apparently wrong, as -2 % 5 returns 3. I would have thought that 5 divides -2 zero times with -2 as the remainder.
Now I understand the mechanics of how this operation is performed, but my question is why? Could someone give me a link to something that explains why modulus and remainder are not synonymous, or an example of a situation where it would be useful?
The result is entirely correct. Modular arithmetic defines the following (I'll use "congruent" since I can't type the equal sign with three lines)
a congruent b mod c iff a-b is a multiple of c, i.e. x * c = (a-b) for some integer x.
E.g.
0 congruent 0 mod 5 (0 * 5 = 0-0)
1 congruent 1 mod 5 (0 * 5 = 1-1)
2 congruent 2 mod 5 (0 * 5 = 2-2)
3 congruent 3 mod 5 (0 * 5 = 3-3)
4 congruent 4 mod 5 (0 * 5 = 4-4)
5 congruent 0 mod 5 (1 * 5 = 5-0)
6 congruent 1 mod 5 (1 * 5 = 6-1)
...
The same can be extended to negative integers:
-1 congruent 4 mod 5 (-1 * 5 = -1-4)
-2 congruent 3 mod 5 (-1 * 5 = -2-3)
-3 congruent 2 mod 5 (-1 * 5 = -3-2)
-4 congruent 1 mod 5 (-1 * 5 = -4-1)
-5 congruent 5 mod 5 (-1 * 5 = -5-0)
-6 congruent 4 mod 5 (-2 * 5 = -6-4)
-7 congruent 3 mod 5 (-2 * 5 = -7-3)
...
As you can see, a lot of integers are congruent 3 mod 5:
..., -12, -7, -2, 3, 8, 13, ...
In mathematics, the set of these numbers is called the equivalence class induced by the equivalence relation "congruence". Our understanding of the remainder and the definition of the "mod" function are based on this equivalence class. The "remainder" or the result of a mod computation is a representative element of the equivalence class. By declaration we have chosen the smallest non-negative element (so -2 is not a valid candidate).
So when you read -2 mod 5 = x this translates to "Find the smallest non-negative x so that there exists an integer y with y * 5 = -2 - x", in concordance with the definition of congruence. The solution is y=1 and x = 3 as you can see by simply trying out other values for y.
a = n (mod m) is defined as a = n + m*t and it applies to negative numbers equally well. (Another to look at it is that a = n (mod m) means (a - n) is a multiple of m)
-2 = 3 (mod 5) because -2 = 3 - 5 (i.e. t = -1)
The convention is that the result of taking a modulo m is a number between 0 and m - 1 (inclusive)
The fundamental guarantee that you get is that
(a % b) + b * (a / b) == a
For signed values, there is no reason either sign should be the preferred outcome of a modulo or divide operation. Some languages fix one form, others leave it up to the implementation, so that the implementation can use whichever way the hardware happens to provide. The hardware instruction, in turn, may have been chosen to operate efficiently the hardware's representation of signed integers.
Generally, be very careful when using signed integers together with division, remainder and bit shift operations.
I guess it depends on whether you want your result rounded down or rounded towards 0:
2 / 5 = 0.4 = 5*0 + 2 works in both cases, whereas
-2 / 5 = -0.4 = 5*0 + -2 if you're rounding towards 0 (truncation),
-2 / 5 = -0.4 = 5*-1 + 3 if you're rounding down (floor).
Note that the result is always positive (for a positive divisor) in the second case and it would be useful, for example, when calculating an array index:
hashmapBuckets[getIntHash(obj) % hashmapBuckets.size].add(obj)
or normalizing an angle:
angle = angle % 360; //0-359
It is in fact the other case I'm having trouble finding practical examples for :)
--
Oh, and the Wikipedia page on the modulo operation has some nice graphs. Note that the remainder always has the same sign as the divisor for a floored division.
Think of modulo as an operator that wraps a line of length y (in terms of y % x) around a circle of x pegs. The remaining length of the line that doesn't fully wrap around x is the resultant.