Page 1 of 1

Coding help... :(

Posted: February 26th, 2018, 7:10 pm
by Coco
Hello guys. I was wondering if someone can help me out a bit. You're all experienced coders, and some of you have done stuff in C#, if I remember it right.
I'll try to include in short everything I know so far, and explain my code. I'm tinkering in visual studio (windows forms), and I'm trying to make matrix multiplication code. Note that only essential pieces of code are here, the rest works fine:

Code: Select all

public partial class Form1 : Form
{
	int[,] A = new int[2, 3];
        int[,] B = new int[3, 2];
        
        private void button1_Click(object sender, EventArgs e)
        //this button creates elements of both matrices above randomly , and it works fine
        
        private void button2_Click(object sender, EventArgs e)
        {
            int[,] C = new int[A.GetLength(0),B.GetLength(1)];
            int n = 0;
            lblMatc.Text = String.Empty; //this line later should be filled with elements of C array
            
            for (int z = 0; z < A.GetLength(0); z++)
            {
                for (int y = 0; y < B.GetLength(1); y++)
                {                    
                    for (int x = 0; x < A.GetLength(1); x++)
                    {
                        n = n + A[y, x] * B[x, y];
                    }                   
                    C[z, y] = n;
                    n = 0;                    
                }
            }
The result I get fro this code is for example, something like this:
if
A = 3, 2, 1
0, 2, 1

B = 1, 0
0, 1
1, 0
For C, I get:

C = 4, 2
4, 2

So, the first element of C[0,0] matrix is fine, the second also, but the second row of C, always ends out as a copy of the first row. I can't really spot the error.

Re: Coding help... :(

Posted: February 28th, 2018, 12:37 am
by Falcury
I think you used the wrong index here:

Code: Select all

n = n + A[y, x] * B[x, y];
Shouldn't that be A[z, x] instead?
Because with A[y, x] I think you end up computing C's diagonal twice in a row.

Re: Coding help... :(

Posted: February 28th, 2018, 6:26 pm
by Coco
Nah. I figured it out finally :mrgreen:
for (int z = 0; z < aai; z++)
{
for (int y = 0; y < bbj; y++)
{
for (int x = 0; x < aaj; x++)
{
n = n + A[z, x] * B[x, y];
}
C[z, y] = n;
n = 0;
}
}
n = n + A[z, x] * B[x, y]; this was the correct way

Re: Coding help... :(

Posted: February 28th, 2018, 6:51 pm
by Norbert
Coco wrote: February 28th, 2018, 6:26 pm Nah. I figured it out finally :mrgreen:
for (int z = 0; z < aai; z++)
{
for (int y = 0; y < bbj; y++)
{
for (int x = 0; x < aaj; x++)
{
n = n + A[z, x] * B[x, y];
}
C[z, y] = n;
n = 0;
}
}
n = n + A[z, x] * B[x, y]; this was the correct way
Isn't that literally what Falcury suggested you'd do/change?

Re: Coding help... :(

Posted: March 1st, 2018, 1:10 am
by Coco
Isn't that literally what Falcury suggested you'd do/change?
Actually it is :mrgreen:. It's just, that I've spent in front of the screen for far to long to notice it.
Note to myself, avoid naming variables with one letter, it only leads to headache.

Re: Coding help... :(

Posted: March 4th, 2018, 1:44 pm
by David
Coco wrote: March 1st, 2018, 1:10 am Note to myself, avoid naming variables with one letter, it only leads to headache.
It's common practice to name loop variables with one letter. (Especially in mathematical formulas.)
However, for matrix multiplication the usual names are i,j,k.

What better names could you use?
z=row, y=column; but I don't know what would be a good name for x. Maybe "step"?

Re: Coding help... :(

Posted: March 4th, 2018, 10:09 pm
by Coco
It's common practice to name loop variables with one letter.
Sure I know that. But with 3 loops, it just got confusing. It took me a while to figure out where the mistake was, and I would track the result of each loop easier if I named them differently. Row and column rings better in my mind than x and y. "step" would actually be a great name for this one :!:. I didn't even realize what Falcury told me, because it's easier to compare a whole word than a letter (and I was kinda happy for resolving the damned thing myself anyway). But now when I know where the error was and why, I think it'll be easier to spot such mistakes in the future. Onward to matrix determinants now...

Re: Coding help... :(

Posted: February 11th, 2019, 3:17 am
by Coco
Is anyone here familiar with C#? Kinda need some help again... :/

Re: Coding help... :(

Posted: February 17th, 2019, 11:39 am
by David
Coco wrote: February 11th, 2019, 3:17 am Is anyone here familiar with C#? Kinda need some help again... :/
I think I'm quite familiar with it.
What do you need help with?

Re: Coding help... :(

Posted: February 19th, 2019, 12:51 am
by Coco
Well... First of all, this isn't something that would have some use in the real world. It's just itching me whether or not is possible to achieve it, so don't bang your head about it if you have other things to do. Anyway...

Suppose you have a class, called Sapient with just an Age property, keeping it simple...

Code: Select all

public class Sapient
{
	public int Age { get; set; }
}
What I wanna do with it, is to set some restrictions on the Age property during instantiation in the following manner:

Code: Select all

Sapient sapient = new Sapient(options =>
	options.Age.MinValue = 0,
	options.Age.MaxValue = 99
);
Min & MaxValue just tell that if somebody tries to do something like this:

Code: Select all

sapiens.Age = 100;
it won't be accepted. Note that I'm aware that Sapiens class does not implement these things currently, and that there are easier ways to do this... But again, this is a can it be done type of thing. The closest thing I've managed so far, is to make a class that behaves like this:

Code: Select all

Sapient sap = new Sapient(
        age: a => a >=0 && a < 100
);
But I'm not satisfied with that. It should also (if possible) have some other optional field configurations, when I add them. So if I add Name property:

Code: Select all

public class Sapiens
{
	public string Name { get; set; }
	public int Age { get; set; }
}
you could set some name restrictions such as:

Code: Select all

Sapient sapient = new Sapient(options =>
	options.Name.CanContainSymbols = true
);
or some other crap, that will be implemented of course. You can use nested class, delegates, whatever. You can even use some special... say Config class that behaves like:

Code: Select all

var sapient = new Config<Sapient>(options =>
	options.Name.CanContainSymbols = false,
	options.Age.MaxValue = 80
);
If anything is unclear, ask ofcourse.
If this is not achievable, my current working class, the one with age: a => a >=0 && a < 100 is using Func<int, bool> in the constructor. It also uses Func<string, bool> as a second parameter in the constructor. How to set all of this Func params to be optional, without making overloads for each combination...?

Re: Coding help... :(

Posted: March 3rd, 2019, 5:39 pm
by David
First of all, sorry for the late reply.

I don't really understand why do you want to be able to set different restrictions for different instances.
Anyway, here is my opinion...
Coco wrote: February 19th, 2019, 12:51 am What I wanna do with it, is to set some restrictions on the Age property during instantiation in the following manner:

Code: Select all

Sapient sapient = new Sapient(options =>
	options.Age.MinValue = 0,
	options.Age.MaxValue = 99
);
This syntax is not valid.
Something like this would be valid syntactically:

Code: Select all

Sapient sapient = new Sapient {
	Options = new SapientOptions {
		Age = new IntegerOptions {
			MinValue = 0,
			MaxValue = 99
		}
	}
};
However, I'm not sure if this is the right way to go.
Coco wrote: February 19th, 2019, 12:51 am The closest thing I've managed so far, is to make a class that behaves like this:

Code: Select all

Sapient sap = new Sapient(
        age: a => a >=0 && a < 100
);
But I'm not satisfied with that.
This looks like the simplest solution to me.
Coco wrote: February 19th, 2019, 12:51 am You can even use some special... say Config class that behaves like:

Code: Select all

var sapient = new Config<Sapient>(options =>
	options.Name.CanContainSymbols = false,
	options.Age.MaxValue = 80
);
It's not possible in C# to make a Config<T> class that has the same properties as T (just with different types).
The amount and names of properties are fixed even in a generic class.
Coco wrote: February 19th, 2019, 12:51 am If this is not achievable, my current working class, the one with age: a => a >=0 && a < 100 is using Func<int, bool> in the constructor.
It also uses Func<string, bool> as a second parameter in the constructor.
How to set all of this Func params to be optional, without making overloads for each combination...?
You can set default values for parameters since C# version 4.
For Func parameters the only possible default value is null, so you have to use that.
Then in the constructor replace these nulls with checks that always return true.

Something like this:

Code: Select all

public Sapient (Func<int, bool> age_check = null, Func<string, bool> name_check = null)
{
	if (age_check == null) age_check = x => true;
	if (name_check == null) name_check = x => true;
	...
}
And call it like this:

Code: Select all

Sapient sapient = new Sapient {
	age_check: a => a >= 0 && a < 100
};
Alternatively, you could use a parameterless constructor, and make public properties/fields that hold the check conditions:

Code: Select all

class Sapient {
	public Sapient() { ... }
	public Func<int, bool> age_check = x => true;
	public Func<string, bool> name_check = x => true;
}
Then you can initialize the properties with this syntax:

Code: Select all

Sapient sapient = new Sapient {
	age_check = a => a >= 0 && a < 100
};

Re: Coding help... :(

Posted: March 6th, 2019, 7:03 pm
by Coco
David wrote: March 3rd, 2019, 5:39 pm I don't really understand why do you want to be able to set different restrictions for different instances.
Various reasons. Although I'm aware that this is totally unusual practice, or so I'm told.
David wrote: March 3rd, 2019, 5:39 pm For Func parameters the only possible default value is null, so you have to use that.
Bummer...

Thanks for the reply ;)