Given 2 coordinates and an angle, find C coordinate - math

The following is an equilateral triangle:
Bisecting ∠ACB splits the triangle into two congruent triangles (and the bisector meets the line AB at its midpoint and forms a right angle!)
if A is (1,0) and B is (5,0) and C is (c,y) and D is (c,0).
C has an angle of tan(theta/2). In this case 60 degrees. How would I derive a formula in order to get the following output 3.4641016151377553.
Another example:
A (-2,0), B(6,0), theta is: 120 degrees
The output is: 2.309401076758504
Output must be in radians (pi/180)
Output is the C coordinate
enter image description here

Assuming that the Points A=(a,0) and B=(b,0) are on the x-axis and that ABC is an isosceles triangle with the angle theta at C:
Then D has the coordinates ((a+b)/2,0). And in the right angled triangle ADC we have tan(theta/2) = (b-a)/2 / h. Hence we get the y-coordinate of C as h = (b-a)/2/tan(theta/2).
Here is the corresponding racket code:
#lang racket
(require math)
(define (half v) (/ v 2))
(define (deg2rad angle_deg) (* angle_deg (/ pi 180)))
(define (cy a b theta) (/ (half (- b a)) (tan (deg2rad (half theta)))))
(cy 1 5 60)
(cy -2 6 120)
giving the output:
3.464101615137755
2.309401076758504

Related

Negative histogram y-values in Racket

I am trying to plot a set of values that includes both positive and negative numbers in Racket. I have tried using the discrete-histogram function from plot:
(plot
(discrete-histogram '(A B C) '(1.2 0.2 -0.4)
#:y-min -0.5))
The corresponding output doesn't plot anything for the negative value:
discrete-histogram has one required argument cat-vals, so you should use '((A 1.2) (B 0.2) (C -0.4)) or (if you already have list of X values and list of Y values) (map list '(A B C) '(1.2 0.2 -0.4)):
#lang racket
(require plot)
(plot (discrete-histogram
(map list '(A B C) '(1.2 0.2 -0.4))
#:y-min -0.5))
In both cases, I see this result:

Show that term `cons` works by showing all beta reductions

I'm new to functional programming.
So the terms cons appends an element to the front of the list. Where
cons ≜ λx:λl:λc:λn: c x (l c n)
How should I go about proving that cons works correctly using beta reduction for a sample function call? For example reducing cons 3 [2,1] to [3,2,1]?
Is there a formula like for the arithmetic operations in lambda calculus? I'm a bit confused on how to approach this compared to an arithmetic operation (i.e. addition or multiplication).
Thanks.
cons ≜ λx:λl:λc:λn: c x (l c n) means that
cons x l c n =
c x (l c n)
(in functional / applicative / combinatory notation). So
cons 3 [2,1] c n =
= c 3 ([2,1] c n)
and what is [2,1] if not just shortcut notation for cons 2 [1] so that we continue
= c 3 (cons 2 [1] c n)
= c 3 (c 2 ([1] c n))
= c 3 (c 2 (cons 1 [] c n))
= c 3 (c 2 (c 1 ([] c n)))
So there's no reduction from cons 3 [2,1] to [3,2,1]; [3,2,1] is cons 3 [2,1]. And [2,1] is cons 2 [1], and [1] is cons 1 [].
The list cons x xs, when supplied with c and n arguments, will turn into c x (xs c n), and so will xs, in its turn; so any list's elements are used in the chain of applications of c on top one another.
And what should [] c n turn into? It has nothing in it to put through the c applications -- those are to be applied to a list's elements, and [] has none. So the only reasonable thing to do (and I'm sure you're already given that definition) is to turn [] c n into just n:
cons 3 [2,1] c n =
= c 3 (c 2 (c 1 ([] c n)))
= c 3 (c 2 (c 1 n ))
whatever c and n are.
And that's that.

Is in Racket any function to draw a plot like "stem" plot in python?

"A stem plot plots vertical lines at each x location from the baseline to y, and places a marker there."
Like this:
The graph you use is generated by the following code in Python:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1, 2 * np.pi, 41)
y = np.exp(np.sin(x))
plt.stem(x, y, use_line_collection=True)
plt.show()
Trying to build a similar plot using Racket:
#lang racket
(require plot)
(define my-linspace
(λ (start stop [num 50])
(range start stop (/ stop num))))
(define x (my-linspace 0 (* 2 pi) 41))
(define y (map exp (map sin x)))
(define my-blue '(32 119 180))
(plot
#:x-min -0.3
#:x-max 6.5
#:y-min -0.2
#:y-max 2.9
#:x-label #f
#:y-label #f
(let ([data (map list x y)])
(list
(points data #:color my-blue #:sym 'fullcircle5)
; for each data point, draw a vertical line
; at its "x" ranging from height 0 to its "y"
(list (map (λ (d) (vrule (first d) 0 (second d) #:color my-blue #:width 2)) data))
(list (hrule 0 (first (first data)) (first (last data)) #:color "red" #:width 2)))))

Using Racket to Plot Points

After spending some time looking at the documentation, searching the web, and experimenting at the prompt, I haven't succeeded at plotting points using Racket. Could someone post an example of how I'd go about plotting (0 1 2 3 4 5) for my x coordinates and (0 1 4 9 16 25) for the y coordinates. I think 1 good example will clear the problem up.
Based on the first example of the doc, and given that the function you want to plot already exists in Racket, it's as simple as:
(require plot)
(plot (function sqr 0 5 #:label "y = x ^ 2"))
If you just want to see the individual points, this is also taken from the docs:
(require plot)
(define xs '(0 1 2 3 4 5))
(define ys '(0 1 4 9 16 25))
(plot (points (map vector xs ys) #:color 'red))
which is equivalent to
(require plot)
(plot (points '(#(0 0) #(1 1) #(2 4) #(3 9) #(4 16) #(5 25)) #:color 'red))

Find x in a^x = a (mod n)

I want to calculate am mod n, where n is a prime number, and m is very large. Rather doing this with binary power calculation, I'd like to find such x that ax = a (mod n) and then calculate a(m mod x) mod n.
Obviously such x exists for any a, because powers mod n loop at some point, but I didn't find out how to calculate it with modular arithmetics. I wonder if I missed something or maybe there exists some numerical method for that?
Your modulus is prime, that makes it easy to get a start, as by Fermat's (inappropriately dubbed "little") theorem, then
a^n ≡ a (mod n)
for all a. Equivalent is the formulation
a^(n-1) ≡ 1 (mod n), if n doesn't divide a.
Then you have
a^m ≡ 0 (mod n) if a ≡ 0 (mod n) and m > 0
and
a^m ≡ a^(m % (n-1)) (mod n) otherwise
(note that your suggested a^(m % x) is in general not correct, if m = q*x + r, you'd have
a^m ≡ (a^x)^q * a^r ≡ a^q * a^r ≡ a^(q+r) (mod n)
and you'd need to repeat that reduction for q+r until you obtain an exponent smaller than x).
If you are really interested in the smallest x > 1 such that a^x ≡ a (mod n), again the case of a ≡ 0 (mod n) is trivial [x = 2], and for the other cases, let y = min { k > 0 : a^k ≡ 1 (mod n) }, then the desired x = y+1, and, since the units in the ring Z/(n) form a (cyclic) group of order n-1, we know that y is a divisor of n-1.
If you have the factorisation of n-1, the divisors and hence candidates for y are easily found and checked, so it isn't too much work to find y then - but it usually is still far more work than computing a^r (mod n) for one single 0 <= r < n-1.
Finding the factorisation of n-1 can be trivial (e.g. for Fermat primes), but it can also be very hard. In addition to the fact that finding the exact period of a modulo n is usually far more work than just computing a^r (mod n) for some 0 <= r < n-1, that makes it very doubtful whether it's worth even attempting to reduce the exponent further.
Generally, when the modulus is not a prime, the case when gcd(a,n) = 1 is analogous, with n-1 replaced by λ(n) [where λ is the Carmichael function, which yields the smallest exponent of the group of units of Z/(n); for primes n, we have λ(n) = n-1].

Resources