Let A,B, C be fad. Consider the equation X = AX + BX + C. Must a solution X be fad? - finite-automata

Let A,B, C be fad. Consider the equation X = AX + BX + C. Must a solution X be fad?
Could you help me solve this question?
fad is a regular language

Assume juxtaposition (AX) means concatenation, and + means union. Then, let A = B = {e} and C = {}, the FAD language containing only the empty string and the empty language, respectively. Then let X be any non-FAD language. Clearly, the equation X = AX + BX + C is true since AX = X, BX = X, and X + X + {} = X.
Here are FAs for {e} and {} (proof, if desired, is left as an exercise):
/-\
--->[q0]-s->q1 | s
\-/
/-\
--->q0 | s
\-/
If juxtaposition and union mean something else, the answer may change. For instance, it's possibly that + means concatenation, but then I don't know what to make of juxtaposition (union? intersection?).

Related

How to calculate the number of scatterplot data points in a particular 'region' of the graph

As my questions says I'm trying to find a way to calculate the number of scatterplot data points (pink dots) in a particular 'region' of the graph or either side of the black lines/boundaries. Open to any ideas as I don't even know where to start. Thank you!!
The code:
################################
############ GES ##############
################################
p = fits.open('GES_DR17.fits')
pfeh = p[1].data['Fe_H']
pmgfe = p[1].data['Mg_Fe']
pmnfe = p[1].data['Mn_Fe']
palfe = p[1].data['Al_Fe']
#Calculate [(MgMn]
pmgmn = pmgfe - pmnfe
ax1a.scatter(palfe, pmgmn, c='thistle', marker='.',alpha=0.8,s=500,edgecolors='black',lw=0.3, vmin=-2.5, vmax=0.65)
ax1a.plot([-1,-0.07],[0.25,0.25], c='black')
ax1a.plot([-0.07,1.0],[0.25,0.25], '--', c='black')
x = np.arange(-0.15,0.4,0.01)
ax1a.plot(x,4.25*x+0.8875, 'k', c='black')
Let's call the two axes x and y. Any line in this plot can be written as
a*x + b*y + c = 0
for some value of a,b,c. But if we plug in a points with coordinates (x,y) in to the left hand side of the equation above we get positive value for all points of the one side of the line, and a negative value for the points on the other side of the line. So If you have multiple regions delimited by lines you can just check the signs. With this you can create a boolean mask for each region, and just count the number of Trues by using np.sum.
# assign the coordinates to the variables x and y as numpy arrays
x = ...
y = ...
line1 = a1*x + b1*y + c1
line2 = a2*x + b2*y + c2
mask = (line1 > 0) & (line2 < 0) # just an example, signs might vary
count = np.sum(mask)

How to determine whether base point of secp256k1 (G) lies on curve?

I am doing some research on elliptic curves. If I understood correctly there is a G base point that was set as a large prime point on the curve. As an example, I pick the infamous secp256k1 curve with G.
G = (0x79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798,
0x483ada7726a3c4655da4fbfc0e1108a8fd17b448a68554199c47d08ffb10d4b8)
I wanted to test if a G lies on the curve. So I written a small piece of python code.
# y^2 = x^3 + 7
def curveEq(G):
x, y = G
left = (y * y)
right = (x * x * x) + 7
print("L: " + str(left))
print("R: " + str(right))
but I get left != right. What am I missing?
The point for me was as Mr. Polk said in the comment - the secpk1 curve contains in definition mod p. I totally misunderstood that this curve is defined with mod p (I was thinking that curve is just an equation without the mod and that mod was used only in point doubling and adding). After correcting my code with modulus everything works and I am able to verify the point.
# y^2 = x^3 + 7 mod P for secp256k1
P = 0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f
def curveEqMod(p):
x, y = p
x = int(x)
y = int(y)
left = (y * y) % P
right = ((x * x * x) + 7) % P
print("L: " + str(left))
print("R: " + str(right))
Also as Mr. kelalaka said there is a similar example: Proof that user public key corresponds the curve equation (secp256r1)

Dynamic scope, F#

I'm trying to figure out what this function will evaluate to in F# in a dynamic scope
let y = 3 in
let f g = g 2 + g y in
let y = f (fun x -> x + 4) in
f (fun x -> x + y)
I know that in static scope the answer is 31, but I´m having a problem figuring out what it is in dynamic scope
I assume that this is an academic question, so I will not give a full answer, but try to provide some hints for you to solve this on your own. This is also not really an F# question, because F# uses static scope.
The idea with dynamic scope is essentially that variables will refer to values that were assigned to a given name last during the execution of the program.
In your example, the key thing are the references to the variable y. In Static scope, the reference on line 2 refers to declaration on line 1 and the reference on line 4 refers to definition on line 3:
01: let y = 3 in
02: let f g = g 2 + g y (* ref to line 1 *) in
03: let y = f (fun x -> x + 4) in
04: f (fun x -> x + y (* ref to line 3 *) )
With dynamic scope, the references to y will point to whatever was the last definition of y encountered in the program execution. When running f on line 3, the reference on line 2 will refer to the value defined on line 1. When running the code in line 4, all references to y will refer to value defined on line 3.
To make this easier to explain, I'll rewrite this with two versions of f, f1 and f2 that refer to different variables:
01: let y = 3 in
02a: let f1 g = g 2 + g y (* ref to line 1 *) in
02b: let f2 g = g 2 + g y (* ref to line 3 *) in
03: let y = fa (* last y in scope defined on line 1 *) (fun x -> x + 4) in
04: fb (* last in scope defined on line 3 *) (fun x -> x + y (* ref to line 3 *) )
With this, you should be able to figure out what the result will be.

Iterating over multidimensional Numpy array

What is the fastest way to iterate over all elements in a 3D NumPy array? If array.shape = (r,c,z), there must be something faster than this:
x = np.asarray(range(12)).reshape((1,4,3))
#function that sums nearest neighbor values
x = np.asarray(range(12)).reshape((1, 4,3))
#e is my element location, d is the distance
def nn(arr, e, d=1):
d = e[0]
r = e[1]
c = e[2]
return sum(arr[d,r-1,c-1:c+2]) + sum(arr[d,r+1, c-1:c+2]) + sum(arr[d,r,c-1]) + sum(arr[d,r,c+1])
Instead of creating a nested for loop like the one below to create my values of e to run the function nn for each pixel :
for dim in range(z):
for row in range(r):
for col in range(c):
e = (dim, row, col)
I'd like to vectorize my nn function in a way that extracts location information for each element (e = (0,1,1) for example) and iterates over ALL elements in my matrix without having to manually input each locational value of e OR creating a messy nested for loop. I'm not sure how to apply np.vectorize to this problem. Thanks!
It is easy to vectorize over the d dimension:
def nn(arr, e):
r,c = e # (e[0],e[1])
return np.sum(arr[:,r-1,c-1:c+2],axis=2) + np.sum(arr[:,r+1,c-1:c+2],axis=2) +
np.sum(arr[:,r,c-1],axis=?) + np.sum(arr[:,r,c+1],axis=?)
now just iterate over the row and col dimensions, returning a vector, that is assigned to the appropriate slot in x.
for row in <correct range>:
for col in <correct range>:
x[:,row,col] = nn(data, (row,col))
The next step is to make
rows = [:,None]
cols =
arr[:,rows-1,cols+2] + arr[:,rows,cols+2] etc.
This kind of problem has come up many times, with various descriptions - convolution, smoothing, filtering etc.
We could do some searches to find the best, or it you prefer, we could guide you through the steps.
Converting a nested loop calculation to Numpy for speedup
is a question similar to yours. There's only 2 levels of looping, and sum expression is different, but I think it has the same issues:
for h in xrange(1, height-1):
for w in xrange(1, width-1):
new_gr[h][w] = gr[h][w] + gr[h][w-1] + gr[h-1][w] +
t * gr[h+1][w-1]-2 * (gr[h][w-1] + t * gr[h-1][w])
Here's what I ended up doing. Since I'm returning the xv vector and slipping it in to the larger 3D array lag, this should speed up the process, right? data is my input dataset.
def nn3d(arr, e):
r,c = e
n = np.copy(arr[:,r-1:r+2,c-1:c+2])
n[:,1,1] = 0
n3d = np.ma.masked_where(n == nodata, n)
xv = np.zeros(arr.shape[0])
for d in range(arr.shape[0]):
if np.ma.count(n3d[d,:,:]) < 2:
element = nodata
else:
element = np.sum(n3d[d,:,:])/(np.ma.count(n3d[d,:,:])-1)
xv[d] = element
return xv
lag = np.zeros(shape = data.shape)
for r in range(1,data.shape[1]-1): #boundary effects
for c in range(1,data.shape[2]-1):
lag[:,r,c] = nn3d(data,(r,c))
What you are looking for is probably array.nditer:
a = np.arange(6).reshape(2,3)
for x in np.nditer(a):
print(x, end=' ')
which prints
0 1 2 3 4 5

Explain np.polyfit and np.polyval for a scatter plot

I have to make a scatter plot and liner fit to my data. prediction_08.Dem_Adv and prediction_08.Dem_Win are two column of datas. I know that np.polyfit returns coefficients. But what is np.polyval doing here? I saw the documentation, but the explanation is confusing. can some one explain to me clearly
plt.plot(prediction_08.Dem_Adv, prediction_08.Dem_Win, 'o')
plt.xlabel("2008 Gallup Democrat Advantage")
plt.ylabel("2008 Election Democrat Win")
fit = np.polyfit(prediction_08.Dem_Adv, prediction_08.Dem_Win, 1)
x = np.linspace(-40, 80, 10)
y = np.polyval(fit, x)
plt.plot(x, y)
print fit
np.polyval is applying the polynomial function which you got using polyfit. If you get y = mx+ c relationship. The np.polyval function will multiply your x values with fit[0] and add fit[1]
Polyval according to Docs:
N = len(p)
y = p[0]*x**(N-1) + p[1]*x**(N-2) + ... + p[N-2]*x + p[N-1]
If the relationship is y = ax**2 + bx + c,
fit = np.polyfit(x,y,2)
a = fit[0]
b = fit[1]
c = fit[2]
If you do not want to use the polyval function:
y = a*(x**2) + b*(x) + c
This will create the same output as polyval.