More Efficient Way to Avoid Multiple Calculations #2? - arrays

Here is a more difficult version of another post I made earlier today.
More Efficient Way to Avoid Multiple Calculations?
I have a lot of these chains in my sheet. Is there a more efficient way than what I am doing?
Here is an example post of more difficult formula needed.
https://docs.google.com/spreadsheets/d/1qejqo0WzMYa5K7YCnovW-7ki97DCX2h6gGA7LE7Eeh8/edit?usp=sharing
=INDIRECT("Data!E"&(K8-1))
=INDIRECT("Data!E"&(K9-1))
=INDIRECT("Data!E"&(K10-1))
etc.
Can I modify this to work?
=ARRAYFORMULA(ROW(A1:A20)*????)
It there a single formula or more efficient one that may reduce calculation time?

you can do it like this:
=ARRAYFORMULA(IFERROR(VLOOKUP(K8:K-1, {ROW(A:A), Data!E:E}, 2, 0)))

Related

Clingo: Intersecting All Possible Optimal Solutions (ASP)

I want to find atoms, within a pre-defined set of atoms, that are in all possible optimal solutions of an ASP problem.
I'll explain what i want to do. I have an optimization problem where multiple optimal answer sets can exist. An answer set consists of atoms that characterizes a solution, for example node(0), edge(1,2) , as well as some that are not relevant, like color(0).
I want to compute all optimal answer sets, only show atoms of the type node() and edge(), then find the specific relevant atoms (like node(0) and edge(1,0)) that exist in all optimal answer sets.
I know i can limit the output of certain atoms with the #show directive. I can also add the flags --opt-mode=optN to compute all optimal answer and limit the output to only those with --quiet=1.
As per guide, using --enum-mode cautious computes the intersection of all answer sets.
An example:
If i have two optimal answer sets:
Answer set 1: node(1) node(0) edge(1,0) edge(0,1) color(1)
Answer set 2: node(1) node(0) edge(1,0) color(0)
I want to the result node(1) node(0) edge(1,0)
I can run clingo with --opt-mode=optN --quiet=1 then manually search for all node() or edge() atoms that all answer sets have in common.
I can add #show node/1 #show edge/2 to my encoding then run clingo with --opt-mode=optN --quiet=1 --enum-mode cautious.
Are all those two options logically equivalent?
Is there a more efficient way of achieving what i want?
I'm unsure about the interplay between the #show directive and the --opt-mode, --quiet and --enum-mode flags. I have a problem where answer sets can be very large, so i would prefer using option 2 since it would use the least disk space.
Thank you in advance.
You are definitely on the right track. I'm unsure about the combination of options though, especially --quiet as it only prevents showing the non-optimal models, but they will still be counted as models for the --enum-mode=cautious thingy.
You should simply use the clingo python API to achieve your goal. First, compute one optimal solution. Then you do have the bound that you can give as an additional argument to the opt-mode and your approach should work.
https://potassco.org/clingo/python-api/current/clingo/

Preconditioning of a linear system

I have a large sparse linear system generated as a part of PDE solution for flows in the form Ax=b. The condition number of matrix A is very bad - of the order 3000!. But I get expected solutions with direct solvers. So, now I want to precondition the matrix so that I can use iterative solvers and use the sparseness. I have tried Jacobi preconditioner, but it does not work well as the matrix is not diagonally dominant. I need some help in proceeding further:
1) Imagine I get an approximate solution for x (generated by one run of biconjugate gradient solver). Now can I get "inverse of A" (for preconditioning) from this, seems like it must be possible but I am unable to figure out how! i.e knowing x and b can I calculate the A inverse (which may be used as preconditioner!).
2) Any other way of preconditioning which you feel would be worth a try?
3) Any way to circumvent pre-conditioning for iterative schemes for bad condition number systems?
Thanks a lot in advance for any help. Any comments are welcome.

Using transpose versus ctranspose in MATLAB

When transposing vectors/matrices in MATLAB, I've seen and used just the ' (apostropohe) operator for a long time.
For example:
>> v = [ 1 2 3 ]'
v =
1
2
3
However this is the conjugate transpose as I've recently found out, or ctranspose.
This seems to only matter when there are complex numbers involved, where if you want to transpose a matrix without getting the conjugate, you need to use the .' opertator.
Is it good practice to also use the .' for real matrices and vectors then? What should we be teaching MATLAB beginners?
Interesting question!
I would definitely say it's good practice to use .' when you just want to transpose, even if the numbers are real and thus ' would have the same effect. The mains reasons for this are:
Conceptual clarity: if you need to transpose, just transpose. Don't throw in an unnecessary conjugation. It's bad practice. You'll get used to writing ' to transpose and will fail to notice the difference. One day you will write ' when .' should be used. As probable illustrations of this, see this question or this one.
Future-proofness. If one day in the future you apply your function to complex inputs the behaviour will suddenly change, and you will have a hard time finding the cause. Believe me, I know what I say1.
Of course, if you are using real inputs but a conjugation would make sense for complex inputs, do use '. For example, if you are defining a dot product for real vectors, it may be appropriate to use ', because should you want to use complex inputs in the future, the conjugate transpose would make more sense.
1 In my early Matlab days, it took me quite a while to trace back a certain problem in my code, which turned out to be caused by using ' when I should have used .'. What really got me upset is, it was my professor who had actually said that ' meant transpose! He forgot to mention the conjugate, and hence my error. Lessons I learned: ' is not .'; and professors can tell you things that are plain wrong :-)
My very biased view: Most cases I use ' are purely "formal", aka not related to mathematical calculations. Most likely I want to rotate a vector like the index sequence 1:10 by 90 degrees.
I seldomly use ' to matrices since it's ambiguous - the first question you've to answer is why you want to make a transpose?
If the matrix is originally defined in a wrong direction, I would rather define the matrix in the correct one it should be, but not turning it afterwards.
To transpose a matrix for a mathematical calculation, I explicitly use transpose and ctranspose. Because by doing so the code is easier to read (don't have to focus on those tiny dots) and to debug (don't have to care about missing dots). Do the following jobs such as dot product as usual.
This is actually a subject of debate among many MATLAB programmers. Some say that if you know what you're doing, then you can go ahead and use ' if you know that your data is purely real, and to use .' if your data is complex. However, some people (such as Luis Mendo) advocate the fact that you should definitely use .' all the time so that you don't get confused.
This allows for the proper handling of input into functions in case the data that are expected for your inputs into these functions do turn out to be complex. There is a time when complex transposition is required, such as compute the magnitude squared of a complex vector. In fact, Loren Shure in one of her MATLAB digests (I can't remember which one...) stated that this is one of the reasons why the complex transpose was introduced.
My suggestion is that you should use .' always if your goal is to transpose data. If you want to do some complex arithmetic, then use ' and .' depending on what operation / computation you're doing. Obviously, Luis Mendo's good practices have rubbed off on me.
There are two cases to distinguish here:
Taking transpose for non-mathematical reasons, like you have a function that is treating the data as arrays, rather than mathematical vectors, and you need your error correcting input to get it in the format that you expect.
Taking transpose as a mathematical operation.
In the latter case, the situation has to dictate which is correct, and probably only one of the two choice is correct in that situation. Most often that will be to take the conjugate transpose, which corresponds to ', but there are cases where you must take the straight transpose and then, of course, you need to use .'.
In the former case, I suggest not using either transpose operator. Instead you should either use reshape or just insist that the input be make correctly and throw an error if it is not. This clearly distinguishes these "computer science" instance from true mathematical instances.

Most efficient way to numerically order an array from least to greatest?

I'm working with processing, and I was wondering what the best (most efficient) way of ordering an array was. I basically want to be able to write a function that could take an array with, say, the ints 3,2,7,29,5,1 and order it like: 1,2,3,5,7,29.
I could figure out some inefficient way of doing, but i'm working with 100,000+ numbers and I don't know how to do this efficiently.
Sorry if this is a stupid question!
import java.util.Arrays;
int[] arr = new int[]{33,6,2,74,2,73,836,23};
void setup(){
Arrays.sort(arr);
println(arr);
}
Well... the most efficient way to sort an array just doesn't exist! There are 1000+ methods to sort an array, and the most efficient way depends on the distribution of data inside that array, and the programming language you are using.
My guess is to go with "quicksort".
Sorting algorithms in Wikipedia has a description of several ones. The performance of each algorithm is measured by its computational complexity.
You can search for implementations of these algorithms in several programming languages, depending on your input data or other considerations to your case of use.
Use the sort() function, which is part of the API for exactly this reason: http://processing.org/reference/sort_.html

Solving the problem of finding parts which work well with each other

I have a database of items. They are for cars and similar parts (eg cam/pistons) work better than others in different combinations (eg one product will work well with another, while another combination of 2 parts may not).
There are so many possible permutations, what solutions apply to this problem?
So far, I feel that these are possible approaches (Where I have question marks, something tells me these are solutions but I am not 100% confident they are).
Neural networks (?)
Collection-based approach (selection of parts in a collection for cam, and likewise for pistons in another collection, all work well with each other)
Business rules engine (?)
What are good ways to tackle this sort of problem?
Thanks
The answer largely depends on how do you calculate 'works better'?
1) Independent values
Assuming that 'works better' function f of x combination of items x=(a,b,c,d,...) and(!) that there are no regularities that can be used to decide if f(x') is bigger or smaller then f(x) knowing only x, f(x) and x' (which could allow to find the xmax faster) you will have to calculate f for all combinations at least once.
Once you calculate it for all combinations you can sort. If you will need to look up data in a partitioned way, using SQL/RDBMS might be a good approach (for example, finding top 5 best solutions but without such and such part).
For extra points after calculating all of the results and storing them you could analyze them statistically and try to establish patterns
2) Dependent values
If you can establish some regularities (and maybe you can) regarding the values the search for the max value can be simplified and speeded up.
For example if you know that function that you try to maximize is linear combination of all the parameters then you could look into linear programming
If it is not...

Resources