I am implementing a compiler framework that allows for custom casts.
A concept in this situation is a set of types. For example, the Integer concept is the set of all signed and unsigned whole numbers, where as the Boolean concept consists of a single boolean type. It is known whether or not any given type is a member of a concept.
A custom cast is defined by an input concept and an output concept, meaning that any type that fits the input concept will be transformed to be a member of the output concept. Ex: I can define a cast that converts any Integer to a Boolean. I have it set up this way, as I don't have to define casts for every single member of Integer and rather just one.
In addition to these rules, casts can be chained. For example, int& can be converted to bool by first converting it to int and then to bool. However, the shortest "distance" is preferred. Meaning that if a cast is able to convert int& to bool directly, than it will be chosen (if multiple paths have this shortest distance than an error will be thrown, so this case shouldn't be considered).
The problem is that given a list of all possible casts and input type T and destination concept C, how do I find which casts to apply to T to get to C?
Set theory approach:
Given an item T and list of transformations that allows a member of a given set to become a member of another one, how do I find the path of minimum transformations to get to set C?
My Ideas:
My first one was to create a graph with each unique concept as vertices and casts as edges and apply a path finding algorthm. The only problem is that I don't know how to define the start and goal nodes.
My second idea is to just brute force a path by going through each possible cast for each intermediate concept. While this would work, would there be a potential approach with dynamic programming?
Related
I noticed that one can constraint parameters in LMFIT using min, max, and /or use an expression. I was wondering if there is a way I could use an expression to constrain a parameter to follow a normal distribution defined by a mean and standard deviation. For example, one of my parameters lies between -3000 and 5000, if I specify these as minimum and maximum value, the optimizer considers them as equally likely (uniform) but instead I want it to consider values far from the mean less likely (i.e normal). Thank you.
Specifying min and max values does not actually assert equal probability for all values between these bounds. It does assert zero probability outside the bounds.
A non-linear least-squares fit as done with lmfit seeks to find the highest probability value for all parameters, it does not treat all values as equally probable. You supply starting values for each parameter, and the method uses computed gradients (could be analytic, but typically numeric) to find the direction for optimizing each parameter value.
But if I understand your goal, you don't really want "hard wall constraints", but want to penalize the fit if a parameter is too far from the expected value. Lmfit does not have a built-in way to easily enable this, but such penalties can be added in the objective function. One approach is to add a "penalty" value as an added element in the array to be minimized. That is, you can extend the residual. Since "least-squares" a Gaussian distribution for the residual in the first place, you can simply append (np.concatenate) a term of::
(current_parameter_value - expected_value)/sigma_expected_value
to the residual. In some sense, this is similar to a regularization and is sometimes called a restraint to allow but penalize values for a parameter that are far from the expected value.
Hope that makes sense!
Is there any functionality in Mathematica that let's the user work directly with matrix objects (potentially of non-defined size), eg. solving a matrix equality for the matrix object without necessarily specifying all elements of the matrix. What I want to do is to manipulate matrix equations while using the matrix objects as elemental, so eg solving a matrix equation for a matrix object (and not for all its elements explicitly.
As a simple example of what I mean: say I'd want to check if two matrix inequalities are equivalent eg. and let's say there is a function matrix[A] that declares that A may be of dimension >2.
matrix[A]
matrix[B]
matrix[C]
In principle that would have to be like an element function, like:
element[A, Reals[dim=n]]
Then there should be a function MatrixSolve[] such that
In: Assuming[A is square and det[A]==0, MatrixSolve[A*B==C,B]]
Out:B->A^(-1)*C.
Or for example:
Q:=A*B *so Q must also be a matrix*
In: Assuming[again the necessary stuff like A square..., Q*A^(-1)===B]]
Out: True
I have not found any such functionalities in the documentation (or in the search function on SE) and was wondering if there is a way to implement this and, if not, why such features would not exist.
I am working on a project that traverses a decision tree. Every node in the decision tree has a formula which can contain multiple variables and be fairly complex. The application asks a user to input the value for the variables, one by one.
Two requirements of the application are:
The application must ask the user to answer variables in the order that they appear in the expression.
The application must skip any variables that are not needed to determine the answer.
If statements are in the format:
if(expression;pass;fail)
For example, consider the following expression:
if((a=1&b=1)|(c=1&d=1&e=1)|f=1;1;2)
If we already know that a=1 and b=1, then we know the answer will be 1, regardless of the value of c, d, e, and f. So there is no need to ask the user to input the value of those variables.
These expressions can be fairly complex, containing multiple comparison operators and embedded ifs. For example:
if(a>1;if(b<5;1;if(c=2;2;0));if(d!=2;if(e=1;1;if(f=2;2;0));0))
I’m having a terrible time coming up with an algorithm to do this efficiently. Is there an existing algorithm for deciding which variables don't matter in a given expression? Or perhaps just a new way of thinking about the problem that might help me out here?
How about constructing a syntax tree?
As you can see, literal numbers are regarded as resolved. Every time the user gives a variable a value, the corresponding leaves node resolves to that value. Then you traverse up the tree resolving values til no more nodes can be resolved. If you reach the root node, then you can determine the answer.
EDIT: A if(e1;e2;e3) expression can be represented as a ternary operator node (has three child nodes). If e1 has resolved to true and e2 to some value v, this expression resolves to v. Same goes for the case e1=false.
General Description of Branch and Bound
From Wikipedia's Branch and Bound:
This step is called pruning, and is usually implemented by maintaining
a global variable m (shared among all nodes of the tree) that
records the minimum upper bound seen among all subregions examined so
far. Any node whose lower bound is greater than m can be discarded.
Practical Example: Traveling Salesman Problem
A simple solution to the Traveling Salesman Problem is to keep a variable, e.g. best, that represents the shortest Hamiltonian Circuit found so far (upper bound).
Every time we consider a new step in a potential new circuit, we compute the cost of the path at the current point, e.g. cost, which is a lower bound on the cost of this path, and compare it to the best variable. If at any point cost >= best, we need not consider this path; we may prune all potential paths that begin with this subpath.
This is not difficult to implement in a procedural language such as C where we can create a variable that is in the scope of the function. For example,
int best = -1; // no best path yet
node* solveTSP() {
// best can be consulted and has a consistent
// value across all recursive calls to solveTSP()
...
}
My Actual Question
It is not clear to me how such an algorithm would be implemented purely functionally. Is there a way to simulate the global variable m mentioned in wikipedia's definition?
I know that it is simple to have a global variable in Lisp, but is this possible in a more purely-functional language such as Haskell?
You simply pass the current best value as an additional argument to recursive calls and also return the updated best value as the a part of their results. So if you have a function of type a -> b, it becomes something like a -> Int -> (b, Int). Instead of reading the global variable, you read the additional argument, and instead of writing it, you return a modified value when the function finishes.
This can be nicely expressed using the state monad. Type Int -> (b, Int) is isomorphic to State Int b, so our function of type a -> b becomes a -> State Int b.
Computation with mutable state is no more powerful than computation without it. One can be reduced to the other.
In particular, a mutable cell in a functional viewpoint becomes a sequence of successive values, usually in some arrangement where new copies shadow older ones (for example, in tail recursion.)
While reading "Essentials of Programming Languages" I came across top down and bottom up definitions for list of integers.While I understand what these definitions say. But I am not able to understand fine details of top down vs. bottom up approach. How do I look at a definition and say weather it is top down or bottom up?
top-down
A Scheme list is a list of integers
if and only if either
it is the empty list, or
it is a pair whose car is an integer and whose cdr is a list of integers.
bottom-up
The set List-of-Int is the smallest
set of Scheme lists satisfying the following two properties:
() ∈ List-of-Int, and
if n ∈ Int and l ∈ List-of-Int, then (n . l) ∈ List-of-Int.
These two concepts are related to the notion of induction and recursion. Both of these concepts are ways of describing infinitely large families of objects, though they differ in their approach.
When you're defining something bottom-up, you are defining it inductively. The idea is that you start out with a set of fixed elements and a way of combining those elements into new elements. In the bottom-up definition above, initially the only element in the set of all list of integers is the empty list. You also have a rule which allows you to take a list in the set of lists of integers and grow it into something one step larger by prepending an integer.
When you're defining something top-down, you are defining it recursively. The idea is that you're beginning with some very large family of objects - in this case, every possible list - and then describing just those lists that are composed solely of integers. Usually elements defined coinductively are defined by taking existing objects and ruling out objects that don't match. For example, in the example of lists of integers, you define whether something is a list of integers by taking any list that you feel like and then verifying that if you keep breaking it down and down and down you eventually bottom out at some objects that you know are lists of integers (in this case, just the empty list).
The two forms are actually equivalent to one another, but they serve different purposes. Induction tries to build up the entire set of valid objects, then defines all objects matching the description. Recursion doesn't initially define anything, but then checks whether any object you have matches some criteria by piecing it apart and verifying it. Due to the magical way in which the two are mathematically defined, any inductive definition can be turned into a recursive definition and vice-versa (assuming that all objects you're talking about are finite).
EDIT: If you're really up for a fun ride, you might want to check out the related concepts of coinduction and corecursion. These are a mathematical dual to induction and recursion and provide an entirely different way of thinking about how to define a data structure. In particular, they allow for infinitely large data structures, which can't normally be defined inductively. Interestingly, there's a connection between coinduction, corecursion, induction, and recursion in terms of fixed points. You can think of the inductive definition of a data structure as the smallest set meeting some property, while the coinductive definition is the largest set with that property. It's really cool!