Find solution of under-determined system that is close to a starting point - linear-algebra

Can I provide a starting point y to one of the eigen's solver for Ax = b?
I want to get an exact solution x that is close to the starting point y.
When would solving Ax = b with Newton's method (without line search) not converge?
Thanks.

Solve Ax = b for a solution that minimize L2 norm of x - y.
Given a y, that means minimizing L2 norm of x - y, subjected to the constraint Ax = b.
By Lagrange multiplier I get the following block matrix equation.
The vector u are one half of the lagrange multipliers.
/ A 0 \ / x \ / b \
| | | | = | |
\ I A^T / \ u / \ y /
Probably not the fastest way.

Related

Calculating haystack size of a string

I want to create a poor-mans version of a password complexity checker. I determine the rough character set the password is using and its length. The search space would then be: charset ^ length. In order to compare this against a single value I want the smallest x that when used as the exponent of 2 is larger than the search space. In more mathy language I want this:
given a and b find the smallest x where a^b < 2^x;
My math sucks. Is there a quick and easy way to calculate this?
Maybe my math doesn't suck quite that much.
2^x == a^b
= define a = 2^c, c = 2loga
2^x == 2^c^b
=
2^x == 2^c*b
=
x == c*b
=
x == 2loga * b
You can solve the equation by using logarithms. Taking the logarithms on both sides yields
x > b * log(a) / log(2)
If you want to find the smallest integer such that the equation holds we can round up the right hand side. In python this can be implemented as
import math
def find_x(a, b):
return math.ceil(b * math.log(a) / math.log(2))

Convert Linear scale to Logarithmic

I have a linear scale that ranges form 0.1 to 10 with increments of change at 0.1:
|----------[]----------|
0.1 5.0 10
However, the output really needs to be:
|----------[]----------|
0.1 1.0 10 (logarithmic scale)
I'm trying to figure out the formula needed to convert the 5 (for example) to 1.0.
Consequently, if the dial was shifted halfway between 1.0 and 10 (real value on linear scale being 7.5), what would the resulting logarithmic value be? Been thinking about this for hours, but I have not worked with this type of math in quite a few years, so I am really lost. I understand the basic concept of log10X = 10y, but that's pretty much it.
The psuedo-value of 5.0 would become 10 (or 101) while the psuedo-value of 10 would be 1010. So how to figure the pseudo-value and resulting logarithmic value of, let's say, the 7.5?
Let me know if addition information is needed.
Thanks for any help provided; this has beaten me.
Notation
As is the convention both in mathematics and programming, the "log" function is taken to be base-e. The "exp" function is the exponential function. Remember that these functions are inverses we take the functions as:
exp : ℝ → ℝ+, and
log : ℝ+ → ℝ.
Solution
You're just solving a simple equation here:
y = a exp bx
Solve for a and b passing through the points x=0.1, y=0.1 and x=10, y=10.
Observe that the ratio y1/y2 is given by:
y1/y2 = (a exp bx1) / (a exp bx2) = exp b(x1-x2)
Which allows you to solve for b
b = log (y1/y2) / (x1-x2)
The rest is easy.
b = log (10 / 0.1) / (10 - 0.1) = 20/99 log 10 ≈ 0.46516870565536284
a = y1 / exp bx1 ≈ 0.09545484566618341
More About Notation
In your career you will find people who use the convention that the log function uses base e, base 10, and even base 2. This does not mean that anybody is right or wrong. It is simply a notational convention and everybody is free to use the notational convention that they prefer.
The convention in both mathematics and computer programming is to use base e logarithm, and using base e simplifies notation in this case, which is why I chose it. It is not the same as the convention used by calculators such as the one provided by Google and your TI-84, but then again, calculators are for engineers, and engineers use different notation than mathematicians and programmers.
The following programming languages include a base-e log function in the standard library.
C log() (and C++, by inclusion)
Java Math.log()
JavaScript Math.log()
Python math.log() (including Numpy)
Fortran log()
C#, Math.Log()
R
Maxima (strictly speaking a CAS, not a language)
Scheme's log
Lisp's log
In fact, I cannot think of a single programming language where log() is anything other than the base-e logarithm. I'm sure such a programming language exists.
I realize this answer is six years too late, but it might help someone else.
Given a linear scale whose values range from x0 to x1, and a logarithmic scale whose values range from y0 to y1, the mapping between x and y (in either direction) is given by the relationship shown in equation 1:
x - x0 log(y) - log(y0)
------- = ----------------- (1)
x1 - x0 log(y1) - log(y0)
where,
x0 < x1
{ x | x0 <= x <= x1 }
y0 < y1
{ y | y0 <= y <= y1 }
y1/y0 != 1 ; i.e., log(y1) - log(y0) != 0
y0, y1, y != 0
EXAMPLE 1
The values on the linear x-axis range from 10 to 12, and the values on the logarithmic y-axis range from 300 to 3000. Given y=1000, what is x?
Rearranging equation 1 to solve for 'x' yields,
log(y) - log(y0)
x = (x1 - x0) * ----------------- + x0
log(y1) - log(y0)
log(1000) - log(300)
= (12 - 10) * -------------------- + 10
log(3000) - log(300)
≈ 11
EXAMPLE 2
Given the values in your question, the values on the linear x-axis range from 0.1 to 10, and the values on the logarithmic y-axis range from 0.1 to 10, and the log base is 10. Given x=7.5, what is y?
Rearranging equation 1 to solve for 'y' yields,
x - x0
log(y) = ------- * (log(y1) - log(y0)) + log(y0)
x1 - x0
/ x - x0 \
y = 10^| ------- * (log(y1) - log(y0)) + log(y0) |
\ x1 - x0 /
/ 7.5 - 0.1 \
= 10^| --------- * (log(10) - log(0.1)) + log(0.1) |
\ 10 - 0.1 /
/ 7.5 - 0.1 \
= 10^| --------- * (1 - (-1)) + (-1) |
\ 10 - 0.1 /
≈ 3.13
:: EDIT (11 Oct 2020) ::
For what it's worth, the number base 'n' can be any real-valued positive number. The examples above use logarithm base 10, but the logarithm base could be 2, 13, e, pi, etc. Here's a spreadsheet I created that performs the calculations for any real-valued positive number base. The "solution" cells are colored yellow and have thick borders. In these figures, I picked at random the logarithm base n=13—i.e., z = log13(y).
Figure 1. Spreadsheet values.
Figure 2. Spreadsheet formulas.
Figure 3. Mapping of X and Y values.

plotting matrix equation in R

I'm new to R and I need to plot the quadratic matrix equation:
x^T A x + b^T x + c = 0
in R^2, with A being a 2x2, b a 2x1, and c a constant. The equation is for a boundary that defines classes of points. I need to plot that boundary for x0 = -6...6, x1 = -4...6. My first thought was generate a bunch of points and see where they are zero, but it depends on the increment between the numbers (most likely I'm not going guess what points are zero).
Is there a better way than just generating a bunch of points and seeing where it is zero or multiplying it out? Any help would be much appreciated,
Thank you.
Assuming you have a symmetric matrix A,
eg
# A = | a b/2 |
# | b/2 c |
and your equation represents a conic section, you can use the conics package
What you need is a vector of coefficients c(a,b,c,d,e,f) representing
a.x^2 + b*x*y + c*y^2 + d*x + e*y + f
In your case, say you have
A <- matrix(c(2,1,1,2))
B <- c(-20,-28)
C <- 10
# create the vector
v <- append(c(diag(A),B,C),A[lower.tri(A)]*2), 1)
conicPlot(v)
You could easily wrap the multiplication out into a simple function
# note this does no checking for symmetry or validity of arguments
expand.conic <- function(A, B, C){
append(c(diag(A),B,C),A[lower.tri(A)]*2), 1)
}

Implementing additional constraints in R's nnls

I am using the R interface to the Lawson-Hanson NNLS implementation of an algorithm for non-negative linear least squares that solves ||A x - b||^2 with the constraint that all elements of vector x ≥ 0. This works fine but I would like to add further constrains. Of interest to me are:
Also minimize "energy" of x:
||A x - b||^2 + m*||x||^2
Minimize "energy in the x derivative"
||A x - b||^2 + m ||H x||^2, where H is the sum of identity and a matrix with -1 on the first off-diagonal
Most generally, minimize ||A x - b||^2 + m ||H x - f||^2.
Is there are a way to coax nnls to do this by some clever way of restating the problems 1.-3. Above? The reason I have hope for such a thing is that there is a little-throw away comment in a paper by Whitall et al (sorry for the paywall) that claims that "fortunately, NNLS can be adopted from the original form above to accommodate something in problem 3".
I take it m is a scalar, right? Consider the simple case m=1; you can generalize for other values of m by letting H* = sqrt(m) H and f* = sqrt(m) f and using the solution method given here.
So now you're trying to minimise ||A x - b||^2 + ||H x - f||^2.
Let A* = [A' | H']' and let b* = [b' | f']' (i.e. stack up A on top of H and b on top of f) and solve the original problem of
non-negative linear least squares on ||A* x - b*||^2 with the constraint that all elements of vector x ≥ 0 .

Fast, inaccurate sin function without lookup

For an ocean shader, I need a fast function that computes a very approximate value for sin(x). The only requirements are that it is periodic, and roughly resembles a sine wave.
The taylor series of sin is too slow, since I'd need to compute up to the 9th power of x just to get a full period.
Any suggestions?
EDIT: Sorry I didn't mention, I can't use a lookup table since this is on the vertex shader. A lookup table would involve a texture sample, which on the vertex shader is slower than the built in sin function.
It doesn't have to be in any way accurate, it just has to look nice.
Use a Chebyshev approximation for as many terms as you need. This is particularly easy if your input angles are constrained to be well behaved (-π .. +π or 0 .. 2π) so you do not have to reduce the argument to a sensible value first. You might use 2 or 3 terms instead of 9.
You can make a look-up table with sin values for some values and use linear interpolation between that values.
A rational algebraic function approximation to sin(x), valid from zero to π/2 is:
f = (C1 * x) / (C2 * x^2 + 1.)
with the constants:
c1 = 1.043406062
c2 = .2508691922
These constants were found by least-squares curve fitting. (Using subroutine DHFTI, by Lawson & Hanson).
If the input is outside [0, 2π], you'll need to take x mod 2 π.
To handle negative numbers, you'll need to write something like:
t = MOD(t, twopi)
IF (t < 0.) t = t + twopi
Then, to extend the range to 0 to 2π, reduce the input with something like:
IF (t < pi) THEN
IF (t < pi/2) THEN
x = t
ELSE
x = pi - t
END IF
ELSE
IF (t < 1.5 * pi) THEN
x = t - pi
ELSE
x = twopi - t
END IF
END IF
Then calculate:
f = (C1 * x) / (C2 * x*x + 1.0)
IF (t > pi) f = -f
The results should be within about 5% of the real sine.
Well, you don't say how accurate you need it to be. The sine can be approximated by straight lines of slopes 2/pi and -2/pi on intervals [0, pi/2], [pi/2, 3*pi/2], [3*pi/2, 2*pi]. This approximation can be had for the cost of a multiplication and an addition after reducing the angle mod 2*pi.
Using a lookup table is probably the best way to control the tradeoff between speed and accuracy.

Resources