I am trying to solve B-Spline equations using De-Boors method. All in all, the method will generate equations that will then have to be solved. What is the best way to represent an equation in code as a variable? I was thinking, just pushing each variable, coefficients, and operators in an array. I would also create a function to interpret these "equation" arrays for solving once variables are available for plug in. Is there a better way to represent equations that are not known until later in execution? Sort of like a variable but for an equation. Example, equation1 = [7,"xxx","+","(",2,"y","-","x",")",3,"yy"] representing 7x^3 + (2y-x)3y^2
Related
My problem is described in this picture(It's like a Pyramid structure):
The objective function is below:
In this problem, D is known, A is the object that I want to get. It is a layered structure, each block in the upper layer is divided into four sub-blocks in the layer below. And the value of the upper layer node is equal to the sum of the four child nodes of the lower layer. In above example, I used only 2 layers.
What I want to do is simulate the distribution of D with A, so in the objective function is the ratio of two adjacent squares in each row in A compared to the value in D. I do this comparison on each layer and sum them. Then it is all of my objective function. But in the finest layer, the value in A has a constrain A<=1, the value in A can be a number between 0 and 1. I have tried to solve it using Quadratic programming in python library CVXPY. However, it seems the speed is slow.
So I want to solve it in another way, because this is a convex optimization problem, which can guarantee the global optimal solution. What I think is whether it is possible to use the method of derivation. There are two unknown variables in each item, that is, the two items with A in the formula. Partial derivatives are obtained for them, and the restriction of A<=1 is added, then solve using gradient descent method. Is this mathematically feasible, because I don't know much about optimization, and if it is possible, how should I do it? If not possible, what other methods can I use?
I have some observational data and I want to fit some model parameters by using lmfit.Minimizer() to minimize an error function which, for reasons I won't get into here, must return a float instead of an array of residuals. This means that I cannot use the Leastsq method to minimize the function. In practice, methods nelder, BFGS and powell converge fine, but these methods do not provide the covariance of the best-fit parameters (MinimizerResult.covar).
I would like to know if thee is a simple way to compute this covariance when using any of the non-Leastsq methods.
It is true that the leastsq method is the only method that can calculate error bars and that this requires a residual array (with more elements than variables!).
It turns out that some work has been done in lmfit toward the goal of being able to compute uncertainties for scalar minimizers, but it is not complete. See https://github.com/lmfit/lmfit-py/issues/169 and https://github.com/lmfit/lmfit-py/pull/481. If you're interested in helping, that would be great!
But, yes, you could compute the covariance by hand. For each variable, you would need to make a small perturbation to its value (ideally around 1-sigma, but since that is what you're trying to calculate, you probably don't know it) and then fix that value and optimize all the other values. In this way you can compute the Jacobian matrix (derivative of the residual array with respect to the variables).
From the Jacobian matrix, the covariance matrix is (assuming there are no singularities):
covar = numpy.inv(numpy.dot(numpy.transpose(jacobian), jacobian))
I'm working on a project which needs to evaluate the performance of CNN/RNN after adding noise to all the variables. For example, if we have an simple MLP, I want to add a random gaussian noise to all the weight parameters, which is not difficult. However, it doesn't seem easy to manipulate the variables for RNN. For example, the variables inside the tf.contrib.rnn.BasicLSTMCell are encapsulated and not accessble for users.
I found a possible way to do this by using the tf.train.saver() function. I can print all the variables including the encapsulated variables. However, how to modify the value of all the variables is still not clear.
Is there an easy way to do this?
You can use tf.trainable_variables (doc) or tf.global_variables (doc) to get those variables, and add noisy to them.
I'm using
R=QQ['x'];
to declare x as variable such that I can calculate with polynomials in x. What I need is another variable, for example t, to represent an integer which can also be used as exponent. For example I want to consider the polynomial (t+1)x^t. Is that somehow possible in SageMath?
EDIT: I want to explain a little bit the reason why I'm looking for such a feature. I've got a couple of really complex rational functions in a few variables and want SageMath to help me to show that they are identical. They are written down in a different way and if you would do it with pen and paper you would need hours and have a huge change of making mistakes. Actually it are not only a few rational functions but infinitely many. But using variables in the coefficients you can cover infinitely many with only one rational function. A simple example is the polynomial 1+x+x^2+...+x^t. For every non-negative integer you get a different polynomial. But you can write (x^(t+1)-1)/(x-1) as rational function instead. With taking t as a variable you cover infinitely many cases with just one rational function.
Is there a way to do such stuff in SageMath?
To create the polynomial x^t as an element of a polynomial ring, Sage needs to know what integer t is equal to. If polynomials of undetermined degree were introduced, most of the Sage methods for polynomials would not work for them: no way to get the list of coefficients with coefficients(), or to find the factors, or the GCD of two polynomials, etc.
However, you can manipulate and simplify polynomials and rational functions just like any other symbolic expressions. For example:
x,t,k = var('x,t,k')
sum(x^k, k, 1, t)
returns (x^(t + 1) - x)/(x - 1).
The relevant articles are Symbolic Computation and Symbolic Expressions.
Basically, I have a set of up to 100 co-ordinates, along with the desired tangents to the curve at the first and last point.
I have looked into various methods of curve-fitting, by which I mean an algorithm with takes the inputted data points and tangents, and outputs the equation of the cure, such as the gaussian method and interpolation, but I really struggled understanding them.
I am not asking for code (If you choose to give it, thats acceptable though :) ), I am simply looking for help into this algorithm. It will eventually be converted to Objective-C for an iPhone app, if that changes anything..
EDIT:
I know the order of all of the points. They are not too close together, so passing through all points is necessary - aka interpolation (unless anyone can suggest something else). And as far as I know, an algebraic curve is what I'm looking for. This is all being done on a 2D plane by the way
I'd recommend to consider cubic splines. There is some explanation and code to calculate them in plain C in Numerical Recipes book (chapter 3.3)
Most interpolation methods originally work with functions: given a set of x and y values, they compute a function which computes a y value for every x value, meeting the specified constraints. As a function can only ever compute a single y value for every x value, such an curve cannot loop back on itself.
To turn this into a real 2D setup, you want two functions which compute x resp. y values based on some parameter that is conventionally called t. So the first step is computing t values for your input data. You can usually get a good approximation by summing over euclidean distances: think about a polyline connecting all your points with straight segments. Then the parameter would be the distance along this line for every input pair.
So now you have two interpolation problem: one to compute x from t and the other y from t. You can formulate this as a spline interpolation, e.g. using cubic splines. That gives you a large system of linear equations which you can solve iteratively up to the desired precision.
The result of a spline interpolation will be a piecewise description of a suitable curve. If you wanted a single equation, then a lagrange interpolation would fit that bill, but the result might have odd twists and turns for many sets of input data.