Assuming I have:
double[] someArray = new [] { 11, 12, 13, 14, 21, 22, 23, 24, 31, 32, 33, 34, 41, 42, 43, 44 };
Is there any out of the box way of creating a 4x4 matrix out of this array without having to split it into 4 arrays myself?
I know it is simple to do this, but I'm exploring how much is ready out of the box.
EDIT
Sorry for not being clear (thought title was):
What I'm wondering is if there is out of the box functionality with the Matrix builder in Math.NET Numerics. Something like:
Matrix<double> someMatrix = DenseMatrix.OfArray(columns: 4, rows: 4, data: someArray);
From looking at the documentation, you could use the constructor directly, or the function OfColumnMajor(int rows, int columns, IEnumerable<double> columnMajor), if your data is in column-major order.
The code would look like this:
//Using the constructor
Matrix<double> someMatrix = new DenseMatrix(4, 4, someArray)
//Using the static function
Matrix<double> someMatrix = DenseMatrix.OfColumnMajor(4, 4, someArray);
If your data is in row-major order, you could split into arrays and use one of the OfRows function, or use the constructor and transpose the matrix, as suggested by Christoph.
Related
Given 2 arrays of double of the same length (The 'Master' array 'A' and secondary 'B'). I want to write a function that 'align' B to A.
I defined alignment such that for each element of A, if B contains that element, then the element should be placed at the same index as A's.
Some specificity on A and B :
A and B are both initially sorted. After the alignment, B DON'T needs to be sorted.
A and B are of the same length.
There is no duplicate neither in A nor in B
A and B are rather small (10 elements max, more usually around 3-5 elements).
A should not change.
I tagged the question C# because it will be my final implementation, but I can adapt the answers
The function will be called often (100k/1M per seconds), I need something efficient ideally.
Ex1 :
A = [10, 11, 12, 13]
B = [10, 12, 14, 16] (initial)
= > [10, 16, 12, 14] B Final ([10, 14, 12, 16] is also valid, only the placement of the elements '10' and '12' are relevant (the other elements are not in A))
Ex2:
A = [10, 11, 12, 13]
B = [08, 09, 10, 11] (initial)
= > [10, 11, 08, 09] B Final
I write two different ways of doing this: one with a dictionary to keep the place of the existing elements, a second one with two pointers that advance in tandem. However, I found the code rather complicated/messy for what it is supposed to do.
Does someone know an existing algorithm for this or a clean and efficient way to do it?
Here's one way. Not optimal, but the code isn't bad.
private static double[] align(double[] A, double[] B)
{
var extras = B.Where(b => !A.Contains(b)).ToArray();
int i = 0;
var result = A.Select(a => B.Contains(a) ? a : extras[i++]).ToArray();
return result;
}
I want to know how to clamp/range/fix (I don't know how to call it) a number.
For exmaple, I want to always fix a number into multiple of 10 so it should works like this:
If you get number: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 must be fixed at 0.
If you get number: 10, 11, 12, 13, 14, 15, 16, 17, 18, 19 must be fixed at 10.
If you get number: 20, 21, 22, 23, 24, 25, 26, 27, 28, 29 must be fixed at 20.
And so on.
I know that it must be easy but I can't find out by myself, thank you in advise.
Divide by the desired precision (e.g. 10) -- integer division always floors values, i.e. returns the smallest integer for the division -- and then multiply again with the same precision.
public static int FloorToPrecision(int value, int precision) {
return (value / precision) * precision;
}
e.g. Console.WriteLine(FloorToPrecision(17, 10)); prints out 10.
I was wondering whether there's an easy way to implement the method I've described in the comments below
// Yields all the longs in the range [first, last] that
// are composed of the prime digits.
// For example, if the range is [4, 50] then the numbers
// yielded are 5, 7, 22, 23, 25, 27, 32, 33, 35, 35,
// although they don't necessarily need to be yieled in order.
static IEnumerable<long> PossiblesInRange(long first, long last)
{
throw new NotImplementedException();
}
The prime digits, in order, are
static long[] PrimeDigits = { 2, 3, 5, 7 };
In my C# application, I have a collection of objects which have an int Order property ranging from 1 to n.
When I do like this:
var listings = session.Query<Listing>().Where(x => !x.IsDeleted && x.CategoryId == category.Id && x.WorkflowStatus == WorkflowStatus.Published).OrderBy(x => x.Order);
I get a collection of listings but not 100% in the correct order. As it stands the order goes:
0, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 20, 21, 22, 23, 24, 25, 26, 28, 29, 3, 30, 31, 32, 33, 4 ....
Any idea why the OrderBy doesn't do exactly as it should?
If you are using an index you need to set the sortoptions for the Order property.
From http://ravendb.net/docs/client-api/querying/static-indexes/customizing-results-order
Numerical values, on the other hand, are stored as text and therefore require the user to specify explicitly what is the number type used so a correct sorting mechanism is enforced. This is quite easily done, by declaring the required sorting setup in SortOptions in the index definition:
Sort(x => x.Order, SortOptions.Int);
The index outlined above will allow sorting by value on the user's age (1, 2, 3, 11, etc). If we wouldn't specify this option, it would have been sorted lexically (1, 11, 2, 3, etc).
The default SortOptions value is String. Appropriate values available for all numeric types (Byte, Double, Float, Int, Long and Short).
I've tried copying arrays in such a way I can crunch data in an array with threads but obviously without splitting the array into smaller chunks (lets say 1 array -> 4 quarters (4 arrays)).
The only method I can find copies from a specified (int)start point and copies all leading data from the start to the end which if I am using multiple threads to crunch the data its nullifies the point of threading.
Here is pseudo code to show what I wish to do.
int array { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
int split1 { 0, 1, 2, 3 }
int split2 { 4, 5, 6, 7 }
int split3 { 8, 9, 10, 11 }
int split4 { 12, 13, 14, 15 }
or lets say the length of the array cant be split up evenly
int array { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 }
int split1 { 0, 1, 2, 3 }
int split2 { 4, 5, 6, 7 }
int split3 { 8, 9, 10, 11 }
int split4 { 12, 13, 14, 15, 16}
The only method I can find copies from a specified (int)start point and copies all leading data from the start to the end which if I am using multiple threads to crunch the data its nullifies the point of threading.
It's a shame you didn't show which method that was. Array.Copy has various overloads for copying part of an array to another array. This one is probably the most helpful:
public static void Copy(
Array sourceArray,
int sourceIndex,
Array destinationArray,
int destinationIndex,
int length
)
Alternatively, look at Buffer.BlockCopy, which has basically the same signature - but the values are all in terms of bytes rather than array indexes. It also only works with arrays of primitives.
Another alternative would be not to create copies of the array at all - if each thread knows which segment of the array it should work with, it can access that directly. You should also look into Parallel.ForEach (and similar methods) as a way of parallelizing operations easily at a higher level.