Alright, I'm trying to understand follow sets and I think I got it except for one thing:
X -> a X
X -> b X
X -> epsilon
Following the rules of this page, FOLLOW(X) should contains $, the end of file character (rule 1). Then, following rule 3, FOLLOW(X) contains everything of FOLLOW(X) which makes my brain melt.
To me, intuitively, FOLLOW(X) should be {a,b,$}, but trying this example in kfg Edit gives me only {$}. Why?
FOLLOW(X) trivially is a subset of itself, so rule 3 does not contribute anything when applied to right-recursive productions.
While this is easily approached descriptively, your difficulty may originate from thinking about algorithms to compute. For computing FOLLOW sets, you could iteratively fill them according to the rules up to saturation. Then you don't need do anything for the trivial case either.
There is however no rule that gets a or b into FOLLOW(X), and I can't see any reason to expect them in FOLLOW(X). The grammar is simple enough to imagine the complete set of syntax trees that it can generate:
X
/|
X / X
/| / /|
X / X / / X
/| / /| / / /|
X / X / / X / / / X
/| / /| / / /| / / / /|
X / X / / X / / / X / / / / X
/| / /| / / /| / / / /| / / / / /|
X / X / / X / / / X / / / / X / / / / / X
| / | / / | / / / | / / / / | / / / / / |
ε α ε α α ε α α α ε α α α α ε α α α α α ε ...
( for α ∊ {a, b} )
They only ever allow X to the very right, so there is no way to have X followed by a or b.
Related
I have a nonconvex optimization problem for which I am calculating a lower bound using the McCormick envelope. Each bilinear term is replaced with an auxiliary variable which has the following constraints defined:
w_{ij} >= x_i^L * x_j + x_i * x_j^L - x_i^L * x_j^L
w_{ij} >= x_i^U * x_j + x_i * x_j^U - x_i^U * x_j^U
w_{ij} <= x_i^U * x_j + x_i * x_j^L - x_i^U * x_j^L
w_{ij} <= x_i^L * x_j + x_i * x_j^U - x_i^L * x_j^U
where
x_U <= x <= x_L
I am given a function taking in several arguments:
def convex_bounds(n,m,c,H,Q,A,b,lb,ub):
# n is the number of optimization variables
# m is the number of eq constraints
# H = positive, semidefinite matrix from objetcive function (n x n)
# Q is (mxn) x n
# A is m x n
# b is RHS of non linear eq constraints (m x 1)
# c,lb,ub are vectors size (n x 1)
......................................
# Create matrix B & b_ineq for inequality constraints
# where B*x <= b_ineq
B = np.eye(3)
b_ineq = np.array((10,10,10))
## these values would work in a scenario with no bilinear terms
My problem is that I don't know how to specify the inequality constraints matrix B and vector b_ineq. For this particular exercise my variables are x1, x2 and x3 with bounds 0 (x_L) and 10 (x_U). My bilinear terms are x_12 and x_23 (which will lead to auxiliary variables w_12 and w_23). How can I specify the known bounds (0 and 10) for x1,x2 and x3 and the calculated ones (as in the theory pasted above) in B and b_ineq?
I don't actually know how to proceed with this.
Assume that I have 2 productions X and Y.
X-> AL|BL|X,
Y-> CK|DK|X.
I guess is not possible to reduce unit productions in X, but is this the case for Y?
You can remove the unit production X → X as it has no effect - expanding X → X in any derivation can be removed and replaced by using one of the other X productions.
Having done that, you can then remove all unit productions for Y by replacing Y → X with Y → ω for each remaining production X → ω.
I was creating a basic number square grid using iteration (only using FOR loops). Unfortunately, I faced some issues with the program not presenting the number square in the preferred format I was trying to present it in - 1 to 10 above and on the side of the number square.
Without using arrays and only FOR loops, could someone help me find a method to present the number square in my preferred format:
Console.WriteLine("[] 1 2 3 4 5 6 7 8 9 10")
Console.WriteLine("1 / / / / / / / / / /")
Console.WriteLine("2 / / / / / / / / / /")
Console.WriteLine("3 / / / / / / / / / /")
Console.WriteLine("4 / / / / / / / / / /")
Console.WriteLine("5 / / / / / / / / / /")
Console.WriteLine("6 / / / / / / / / / /")
Console.WriteLine("7 / / / / / / / / / /")
Console.WriteLine("8 / / / / / / / / / /")
Console.WriteLine("9 / / / / / / / / / /")
Console.WriteLine("5 / / / / / / / / / /")
Sub Main()
Dim output As Integer
For count1 = 0 To 10
Console.Write(Format(output, " 000") & " ")
Next
Console.WriteLine()
Console.WriteLine()
For count3 = 1 To 10
For count4 = 0 To 10
output = count3 * count4
Console.Write(Format(output, " 000") & " ")
Next
Console.WriteLine()
Next
Console.ReadKey()
End Sub
The Problem
My co-worker and I have been trying to get some Scheme code working that will take input from a TSV file and assign those values to variables already defined in the Ansys software.
The Files
The TSV file is organized as follows:
1 0.7 0.8 0.7 1.0 0.5 [...]
2 0.9 0.9 0.3 0.5 0.5 [...]
3 0.5 0.8 0.3 0.3 0.7 [...]
4 0.6 0.2 1.0 0.5 0.5 [...]
5 0.9 0.0 0.0 0.8 0.3 [...]
6 0.5 0.6 0.3 0.4 1.0 [...]
[ ... ]
1998 0.5 0.0 0.2 0.9 0.0 [...]
1999 0.8 0.2 0.2 0.7 0.2 [...]
2000 0.7 0.2 0.8 0.4 0.5 [...]
The first column is the flow-time at which that particular set of inputs needs to be assigned to the variables in the software; the other 96 columns represent the 96 variables that need to be set.
The Scheme code that I have so far is this:
;Set variables
(define (setFunc)
(lambda(row-position outflow-number flow-wt-1)
(let outflow-string ((string-append("def bc outflow " outflow-number " ~a")))
(if (< row-position 180)
(ti-menu-load-string (format #f (outflow-string) flow-wt-1))
(ti-menu-load-string (format #f (outflow-string) 0.0))
)
)
)
)
;Read a file to a list of chars
(define (file->char_list)
(call-with-input-file "doorstates.txt"
(lambda(input-port)
(let loop ((x (read-char input-port)))
(if (not (eof-object? x))
(#t (begin (cons x (loop (read-char input-port)))))
)
)
)
)
)
;Main
(begin
(let inFile ((apply string (file->char_list)))
(let inFileList ((string-split (inFile #\newline)))
(let index ((0))
; Row position = flow-time - 1
(define rowPosition ((- flow-time 1)))
(let loop ((index))
(cond ((>= index (length inFileList))
'()
)
((= (- index (* (/ index 97) 97)) 0)
(+ index 1)
(loop (index))
)
((= (/ index 97) rowPosition)
; Column position = index - (rowPosition * rowLength)
(define colPosition ((- index (* rowPosition 97))))
(setFunc (rowPosition colPosition list-ref inFileList index))
(+ index 1)
(loop (index))
)
(else
(+ index 1)
(loop (index))
)
)
)
)
)
)
)
When the above code is run with the Ansys software it produces the following error:
Error: eval: unbound variable
Error Object: string
All the sources I've read have said that the most likely cause of an unbound variable is that extra parentheses were put around parameters that aren't functions (like literals); but everything I've seen has also had double parentheses around let arguments.
The Point
I had never worked with Scheme or any other LISP variation before being put on this project so if there are any errors or common practices I'm missing please let me know. Also if the above Scheme code is total crap and not doing what I need it to do please let me know and suggest next steps!
You are misinterpreting the error message. Your begin is missing a closing parenthesis. Thus, the end of file is encountered within the "list" starting with (begin .... Furthermore, you are using let incorrectly. You need to enclose the code that uses the variables, e.g.:
(let ((a 1))
(+ a 7))
I suggest using an editor that understand s-expressions.
I'm going through Terry Tao's real analysis textbook, which builds up fundamental mathematics from the natural numbers up. By formalizing as many of the proofs as possible, I hope to familiarize myself with both Idris and dependent types.
I have defined the following datatype:
data GE: Nat -> Nat -> Type where
Ge : (n: Nat) -> (m: Nat) -> GE n (n + m)
to represent the proposition that one natural number is greater than or equal to another.
I'm currently struggling to prove reflexivity of this relation, i.e. to construct the proof with signature
geRefl : GE n n
My first attempt was to simply try geRefl {n} = Ge n Z, but this has type Ge n (add n Z). To get this to unify with the desired signature, we obviously have to perform some kind of rewrite, presumably involving the lemma
plusZeroRightNeutral : (left : Nat) -> left + fromInteger 0 = left
My best attempt is the following:
geRefl : GE n n
geRefl {n} = x
where x : GE n (n + Z)
x = rewrite plusZeroRightNeutral n in Ge n Z
but this does not typecheck.
Can you please give a correct proof of this theorem, and explain the reasoning behind it?
The first problem is superficial: you are trying to apply the rewriting at the wrong place. If you have x : GE n (n + Z), then you'll have to rewrite its type if you want to use it as the definition of geRefl : GE n n, so you'd have to write
geRefl : GE n n
geRefl {n} = rewrite plusZeroRightNeutral n in x
But that still won't work. The real problem is you only want to rewrite a part of the type GE n n: if you just rewrite it using n + 0 = n, you'd get GE (n + 0) (n + 0), which you still can't prove using Ge n 0 : GE n (n + 0).
What you need to do is use the fact that if a = b, then x : GE n a can be turned into x' : GE n b. This is exactly what the replace function in the standard library provides:
replace : (x = y) -> P x -> P y
Using this, by setting P = GE n, and using Ge n 0 : GE n (n + 0), we can write geRefl as
geRefl {n} = replace {P = GE n} (plusZeroRightNeutral n) (Ge n 0)
(note that Idris is perfectly able to infer the implicit parameter P, so it works without that, but I think in this case it makes it clearer what is happening)