How can I avoid a DOMAIN ERROR using matrix inversion ⌹ in Dyalog - matrix

I have been porting old APL*PLUS PC functions that run on DOS to Dyalog and have had great success. But one function runs fine on APL*PLUS PC but gave me a DOMAIN ERROR on Dyalog. I traced the problem to a matrix invertion using ⌹.
Here is an example of what I am seeing:
F←6.91E9 5.655E9
I←¯0.0144722 ¯0.01897385
Z←I⌹2 2⍴F[1],(÷-F[1]),F[2],÷-F[2]
DOMAIN ERROR
Z←I⌹2 2⍴F[1],(÷-F[1]),F[2],÷-F[2]
∧
This error was also observed on GNU_64 APL.
I created my own DIVIDE function to work with 2x2 arrays:
Z←I2 DIVIDE M22;A;B;C;D
A←M22[1;1] ⋄ B←M22[1;2] ⋄ C←M22[2;1] ⋄ D←M22[2;2]
Z←÷(A×D)-B×C
Z←Z×(2 2)⍴D,(-B),(-C),A
Z←Z+.×I2
Z←I DIVIDE 2 2⍴F[1],(÷-F[1]),F[2],÷-F[2]
Z
4.625648461E¯13 122089494.5
Check answer:
(2 2⍴F[1],(÷-F[1]),F[2],÷-F[2])+.×Z
¯0.0144722 ¯0.01897385
DIVIDE gives the correct answer

Related

Stumpy: Example of finding top-k motifs not working

I am trying to recreate the example shown in https://github.com/TDAmeritrade/stumpy/discussions/438.
Stumpy 1.11.1 seems to find just 1 motif; the size of the returned inde in the call to stumpy.motifs() is (1, 10).
Can someone please help?
Running the code that produces two motifs (for the person entering / leaving the room) does not result in 2 motifs: only the first motif is returned.

Parameters for dlib::find_min_bobyqa

I'm working on the C++ version of Matt Zucker's Page dewarping. So far everything works fine, but I have a problem with optimization. In line 748 of Github repo Matt uses optimize function from Scipy. My C++ equivalent is find_min_bobyqa from dlib.net. The code is:
auto f = [&](const column_vector& ppts) { return objective( dstpoints, ppts, keypoint_index); };
dlib::find_min_bobyqa(f,
params,
2 * params.nr() + 1, // npt - number of interpolation points: x.size() + 2 <= npt && npt <= (x.size()+1)*(x.size()+2)/2
dlib::uniform_matrix<double>(params.nr(), 1, -2), // lower bound constraint
dlib::uniform_matrix<double>(params.nr(), 1, 2), // upper bound constraint
1, // initial trust region radius
1e-5, // stopping trust region radius
4000 // max number of objective function evaluations
);
In my concrete example params is a dlib::column_vector with double values and length = 189. Every element of params is less than 2.0 and greater than -2.0. Function objective() returns double value and "alone" it works properly because I get the same value as in the Python version. But after running fin_min_bobyqa function I usually get the message:
Terminate called after throwing an instance of 'dlib:bobyqa_failure', return from BOBYQA because the objective function has been called max_f_evals times.
I set max_f_evals to quite big value to see if it optimizes at all, but it doesn't. I did some tweaking with parameters but without good results. How should I set the parameters of find_min_bobyqa to get the right solution?
I am very interested in this issue as well. Zucker's work, with very minor tweaks, is ideal for straightening sheet music images, and I was looking for ways to implement it in a mobile platform when I came across your question.
My research so far suggests that BOBYQA is not the equivalent of Powell's method in scipy. BOBYQA is constrained, and the one in scipy is not.
See these links for more information, and a possible way to compile the right supporting library - I would try UOBYQA or NEWUOA.
https://github.com/jacobwilliams/PowellOpt
https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html#rdd2e1855725e-3
(See the Notes section)
EDIT: see C version here:
https://github.com/emmt/Algorithms/tree/master/newuoa
I wanted to post this as a comment, but I don't have enough points for that.
I am very interested in your progress. If you're willing, please keep me posted.
I finally solved this problem. I used PRAXIS library, because it doesn't need derivative information and is fast.
I modified the code a little to my needs and now it is faster around few seconds than original version written in Python.

AMPL Syntax Error - Greater than or equal to issue

I am modeling a sort of schedule optimization problem in AMPL and am using gurobi for the option solver.
In this problem, I have declared a set of schedules from 1 through 1000, and called this set "Schedules".
Each schedule sas a sort of layer (a parameter called "layer" has been created) with a value ranging from 1 to 4. This is a sort of preference or hirearchy of the days off that this particular schedule has.
I want several constraints that determine how many schedules of each preference are available. For instance, I want at least 170 of the schedules to have a preference layer of 1. I wrote the following line to do so:
subject to Preference1: sum {j in Schedules: layer[j]=1} >= L1Demand;
Where L1Demand is set to 170. However, when I go to include the model file in the ampl window, I get the following error:
syntax error
context:
subject to Preference1: sum {j in Schedules: layer[j]=1} >>> >= <<< L1Demand;
I don't understand why this is throwing out a syntax error. I may be missing something very basic or obvious, but can anyone tell me as to why this would be happening? Thank you very much.
You should specify an argument for sum, for example:
sum {j in Schedules: layer[j]=1} x[j]
where x is some variable indexed over Schedules.

Error when subtracting two negative numbers in ksh: “assignment requires lvalue”

I am trying to debug someone else's script:
The code line is:
y=$((${oldvalue[$x]}-${newvalue[$x]}))
y gets calculated fine as long as both sides are positive numbers. However, I have a production situation where they are both negative and the error I get is:
DEBUG Old value = -4144290000
DEBUG New value = -4009685000
script.sh: line 123: -4144290000--4009685000: assignment requires lvalue
I never would use ksh myself for even the simplest of calculations but I am in a position of production support and have to deal with a big ball of mud, I would use at least Perl/Python. Can anybody tell why this problem is happening and how to fix it?
Thanks
It needed spaces on both sides of the minus
y=$((${oldvalue[$x]} - ${newvalue[$x]}))

Express a number using words in Mathematica

I heard that we can use the English words to express the number in Mathematica. Like using One hundred to express 100. Which function can do it?
A solution basically equivalent to dreeves's solution (but not available at the time of his answer) would be to call WolframAlpha[] directly from Mathematica (this requires an internet connection). For example,
WolframAlpha["6 million 2 hundred and 12 thousand and fifty two",
{{"Input", 1}, "Plaintext"}]
returns the string
"6212052"
So we can construct the following function that returns the actual number
textToNumber[num_String] :=
Module[{in = WolframAlpha[num, {{"Input", 1}, "Plaintext"}]},
If[StringMatchQ[in, NumberString], ToExpression[in], $Failed]]
It also works with decimals and negative numbers, e.g., textToNumber["minus one point one"].
Note that we could ask for things other than "Plaintext" output. The easiest way to find out what's available is to enter some number, eg,WolframAlpha["twelve"], and explore the options available when you press the ⨁ signs on the right of each "pod". It is also worth exploring the documentation, where you find useful output "formats" such as "MathematicaParse" and "PodIDs".
We can also go in the other direction:
numberToText[num_Integer] := WolframAlpha[ToString[num],
{{"NumberName", 1}, "Plaintext"}]
I couldn't find the right incantations to get the spoken phrase form for non-integers. If someone knows the right spell, or if W|A gains this ability, please feel free to update this answer. It's a shame that SpokenString does not have an option for reading numbers as their spoken phrases.
I see that Wolfram Alpha can do that, so here's a kludgy little function that sends the English string to Wolfram Alpha and parses the result:
w2n[s_String] := ToExpression[StringCases[
Import["http://www.wolframalpha.com/input/?i=" <> StringReplace[s, " "->"+"],
"String"],
RegularExpression["Hold\\[([^\\]]*)\\]"] -> "$1"][[1]]]
Example:
w2n["two million six hundred sixty-six"]
> 2000666
Does Wolfram Alpha provide an actual API? That would be really great!
PS: They have one now but it's expensive: http://products.wolframalpha.com/api/
PPS: I notice that the wolframalpha results page changed a bit and my scraping no longer works. Some variant on that regular expression should work though.
This is the code:
IntegerName[78372112345]
This is the output:
78 billion 372 million 112 thousand 345
not available back in '09...
SemanticInterpretation["one hundred fifty thousand three hunded and six"]
or
Interpreter["SemanticNumber"]["one hundred fifty thousand three hunded and six"]
150306
( notice my spelling error didn't phase it..)
The two functions are not the same by the way,
SemanticInterpretation["six and forty two thousandths"]//N (* 6.042 *)
Interpreter["SemanticNumber"]["six and forty two thousandths"] (*fails*)

Resources