Is there a c# library that provides array manipulation like numpy [closed] - c#

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
I am starting to use the Numpy and really like it's array handling capabilities. Is there some library that I can use in C# that provides similar capabilities with arrays. The features I would like most are:
Creating one array from another
Easy/trival iteration of arrays of n dimension
Slicing of arrays

NumPY has been ported to .NET via IronPython.

I don't think you need a library. I think LINQ does all you mention quite well.
Creating one array from another
int[,] parts = new int[2,3];
int[] flatArray = parts.ToArray();
// Copying the array with the same dimensions can easily be put into an extension
// method if you need it, nothing to grab a library for ...
Easy iteration
int[,] parts = new int[2,3];
foreach(var item in parts)
Console.WriteLine(item);
Slicing of arrays
int[] arr = new int[] { 2,3,4,5,6 };
int[] slice = arr.Skip(2).Take(2).ToArray();
// Multidimensional slice
int[,] parts = new int[2,3];
int[] slice = arr.Cast<int>().Skip(2).Take(2).ToArray();
The awkward .Cast<int> in the last example is due to the quirk that multidimensional arrays in C# are only IEnumerable and not IEnumerable<T>.

Related

How can I print the greatest number of two number without any conditional statements? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
The greatest of two numbers without any conditional statements. I need to get two numbers from the user and find out the greatest of them but I can't figure it out.
There are many ways to solve this but I would solve this by sorting numbers in a descending way. Try this:
int[] numbers = { 10, 20};
Array.Sort(numbers);
Array.Reverse(numbers);
Console.WriteLine("The highest number is: " + numbers[0]);
Or, just use numbers.Max() instead of Array.Sort(numbers) and Array.Reverse(numbers). There are still more than 20 ways to solve this problem.
You can use Math.Max, but it is a wrapper around a ternary statement that looks like this:
return (val1>=val2)?val1:val2;
There is a different way...
var array = new double[] { userValue1, userValue2 };
var largest = array.Max();
Or you can sort the array in descending order and get the first element... Really though all programming boils down to a conditional somewhere, and .Max() is no different, it just sorts the array and gets the biggest value.

Is it possible to create a constructor for Arrays c#? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I am new to coding and still at college at the moment. Sorry if this question has been asked before but I could not see it any where.
I was wonder if it is possible to create a constructor for the array Class, so each time I create a new array I can execute code such as count the amount of arrays I have in my application for each instance I make? I understand the Array class is abstract so you cannot make an instance of it.
Are int[] arrays just methods within the abstract Array class?
Any insight as to why it is or isn't possible would be greatly appreciated!
Thanks
If your custom code is just to populate the array, C# provides several built in ways to do this (see All possible C# array initialization syntaxes).
If you want to have code execute every time you create an instance of a particular kind of object, you should make a class that represents that object (you might be able to do an extension method as well, but that would likely get confusing over time). This class might be nothing more than a wrapper around an array:
public class MyArray<T>
{
private T[] _array;
public MyArray()
{
// execute your always must run code here!
}
public ArrVal
{
get { return _array; }
set { _array = value; }
}
}
...
MyArray<int> myArray = new MyArray<int>(); // your custom code gets executed when you new up the object here
However, per best practices, you should avoid having code in a constructor that does too much work (and in my experience, having a constructor that throws exceptions can cause some problems that are hard to debug, although MSDN says it's better to throw the exception than to cover it up). If this code is going to be doing intensive work, it may be better to create a separate method (maybe something called public void Initialize()) so that callers can new up the object more lazily.
You should also avoid trying to have this done for all arrays, because I can guarantee it'll cause problems for you or someone else down the road when they can't figure out why int[] arr = new int[3] is doing extra stuff. You should look to properly use encapsulation here instead (i.e. creating a wrapper/extension/decorator class).
Also, it's entirely possible that one of the existing .NET classes for Collections fulfills your needs... Look into those.
I'm not sure if I understand your question correctly, but if you want to be able to execute code when you create an array you can use LINQ:
int[] myArray = new int[10]
.Select((x, idx) => /*execute your code here*/).ToArray();
So you can create an array of length 10, then you can execute code for each element to determine what you want to populate it.
For example you could fill the array with random numbers using:
Random random = new Random();
int[] myRandomArray = new int[10]
.Select((x, idx) => random.Next()).ToArray();

Adding a new element to the end of a C# array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Im trying to do exactly what the title says
int[] weeks = {};
weeks[weeks.Length]=1;
this doesnt work. There is also no .Add Method.
Any ideas? or is this not possible
Line 1 declares and initializes a new 1-dimensional array of int without specific size.
Line 2 resizes the array (weeks) to its actual size, plus 1.
Line 3 assigns the value 1 to the element at the last position (that we created in Line 2)
Remember: int[5] weeks; -> to access last element you have to use index 4 -> weeks[4] = 1
int[] weeks = {};
Array.Resize(ref weeks,weeks.Length + 1);
weeks[weeks.Length - 1]=1;
Arrays in C# by definition are of a fixed size, and cannot be dynamically expanded.
I would suggest using a List<>, as they can be dynamically expanded, much like vectors in other programming languages.
List<int> weeks = new List<int>();
weeks.add(1);
See more here.
First of all there is no such thing razor array. Razor is a view engine and array is a data structure.
Arrays have fixed length so if you declare an array with the length of 5:
int[] weeks = new int[5];
Trying to add an element to a fifth place will result in IndexOutOfRangeException
If you need some data-structure with variable size you could look at all the objects that implement IList interface for example a List, an ArrayList and others.
IList interface also defines Add method that you requested.

What is the modern analogue for a two-dimensional string array? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 8 years ago.
Improve this question
I'm refactoring some legacy code that uses a 2D string array:
/// <summary>Array of valid server messages</summary>
private static string[,] serverRsp =
{
{"JOIN", "RSP" },
{"SETTING", "RSP" },
. . .
I want to modernize this, but don't know if I should use a Dictionary, a List of list of string, or something else. Is there a standard correlation between the "olden" way and the golden way (legacy vs. refactored)?
IWBN (it would be nice) if there was a chart somewhere that showed the olden vs. the golden for data types and structures, etc.
[,] is not an "old" datastructure, and hopefully will never become.
Keep using it whenever appropriate.
For example:
just in this case have a List<List<T>> is much more confusing then having simple 2 dimensional array.
It's lighter then List<T>in terms of memory consumption (at least from my measurements).
In short: if there is no any real reason, or new requirement to change it, like make it faster O(1) access data structure key-value store (for non index, hence key like, fast access), do not change it. It is clear and it is readable.

C# instantiate array value [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
For some reason this works
public void TestData(ref array[,])
{
array[0, 0] = new DataType();
}
while this doesn't work
public void TestData(ref array[][])
{
array[0][0] = new DataType();
}
I've found a way around needing to use the [][] array, but would be nice to know why it doesn't seem to work the same. Do I need to instantiate at the array[0] level as well?
type[,] is a 2-dimensional array, while type[][] is a 1-dimensional array of 1-dimensional arrays, sometimes called a jagged array, because the arrays are not necessarily the same length.
You can use either, depending on what makes sense. If you use a jagged array, you have to instantiate each of the arrays (the outer one and all of the inner ones). If you use a 2-D array, you just instantiate it once.
"Do I need to instantiate at the array[0] level as well?" yes.
So if you want to use T[][] then you need to do
for (int i = 0; i < array.Length; i++)
array[i] = new T[someSize];
So that is the basic reason why your "jagged" array isn't working. When you declare the other one it instantiates the horizontal arrays as well as the vertical one, in this case you only get the vertical one (column) and have to instantiate the rows yourself. The two aren't actually interchangeable even though working with them is fairly similar.

Categories