0

I have a generic type called Vector<T>, I created it as so, cause the T might be float or Complex :

public class Vector<T>
    {
        #region Properties
        public ulong Length
        {
            get
            {
                return _length;
            }
        }
        public VectorType VectorType
        {
            get
            {
                return _vectorType;
            }
        }

        #endregion

        #region Indexers
        public T this[ulong index]
        {
            get
            {
                return _data[index];
            }
            set
            {
                _data[index] = value;
            }
        }

        #endregion

        #region Constructors

        public Vector(VectorType vectorType, T[] data)
        {
            if (!((data is float[]) || (data is Complex[])))
            {
                throw new InvalidDataException("Data must be array of float or array of Complex");
            }
            _data = new T[_length = (ulong)data.Length];
            for (ulong i = 0; i < _length; i++)
            {
                _data[i] = data[i];
            }
            _vectorType = vectorType;
        }


        public Vector(VectorType vectorType, Vector<T> vector)
        {
            _data = new T[_length = vector.Length];
            for (ulong i = 0; i < _length; i++)
            {
                _data[i] = vector[i];
            }
            _vectorType = vectorType;
        }

        #endregion

        #region Methods

            //Unity Matrix, this vector has 1/N everywhere
            public static Vector<float> e(VectorType vectorType, ulong length)
            {
                var data = new float[length];
                for (ulong i = 0; i < length; i++)
                {
                    data[i] = (float)1 / length;
                }
                var vectorE = new Vector<float>(vectorType, data); 
                return vectorE;
            }

            public float Sum()
            {
                float sum = 0;
                if (_data is float[])
                {
                    sum = (_data as float[]).Sum();
                }
                else
                {
                    if (_data is Complex[])
                    {
                        for (ulong i = 0; i < _length; i++)
                        {
                            sum += (float)
                                Math.Sqrt(Math.Pow((_data[i] as Complex?).Value.Real, 2) +
                                          Math.Pow((_data[i] as Complex?).Value.Imaginary, 2));
                        }
                    }
                }
                return sum;
            }

            public bool CheckIfSochasitc()
            {
                return Math.Abs(Sum() - 1) < float.Epsilon;
            }

            public void Normalize()
            {
                var sum = Sum();

                if (_data is float[])
                {
                    for (ulong i = 0; i < _length; i++)
                    {
                        float x = ((float) _data[i])/sum;
                        _data[i] =  (T)x;
                    }
                }

            }

        #endregion

        #region Operators
        //I omitted the code inhere to avoid overload
        #endregion

        #region Fields

            private  ulong _length;
            private readonly VectorType _vectorType;
            private T[] _data;

        #endregion
    }

    public enum VectorType
    {
        Row,Column        
    }

My problem is that I have a generic array (if I can call it so) :

private T[] _data;

And I have the Normalize() method:

public void Normalize()
          {
             var sum = Sum();
             if (_data is float[])
             {
                for (ulong i = 0; i < _length; i++)
                {
                    //Here is the problem
                    _data[i] =  ((_data[i] as float?) / sum);
                }
             }
          }

This doesn't work saying can't cast float to T tried to search but couldn't find helpful aide, any clarification I'd be thankful.

Update :

The Sum() method always returns a float

2
  • Generally it's an indication that you're probably not doing things well if this is a requirement. Commented May 10, 2014 at 19:33
  • Yeah sorry @dasblinkenlight yeah my french doesn't wanna leave me in peace, I update. Commented May 10, 2014 at 19:43

2 Answers 2

4

It's not clear why you're converting to float? at all (or why you're using ulong as the index variable type...) but you just need to cast the result back to T - otherwise you can't assign it back into an array of type T[]. Additionally, you need to cast to object (in order to convert back to T:

float x = ((float) (object) data[i]) / sum;
data[i] = (T) (object) x;

You can use float? for the first line, with as, to avoid boxing - but then you need to get the non-nullable value:

float x = (data[i] as float?).Value / sum;

Both are pretty ugly :(

As noted in comments though, this sort of thing is usually an indication of the design not really being properly generic at all. We don't know what type Sum() returns, but you should consider just how "general" your type is to start with.

Sign up to request clarification or add additional context in comments.

9 Comments

On the other hand, having code in a generic type that deals with specific types seems like incorrect design to me.
@JonSkeet I used the float? because of the as operator, Sum() returns a float always, I tried your coded : float x = ((float) data[i]) / sum; but the compiler still telling me Can't convert T to float
@AymenDaoudi: I don't follow your reasoning at all - and what's the compile-time type of Sum()? If it's float, then the code I've given should be fine. It would really help if you'd provide a short but complete program demonstrating the problem.
@AymenDaoudi: Actually, I see the problem now - editing.
@Aymen: I can't right now, but your program still isn't complete... It's not full enough for me to run it and reproduce the error.
|
-2

May be you can try this

if (typeof(_data) == float[])
             {
                for (ulong i = 0; i < _length; i++)
                {
                    _data[i] =  ((_data[i] as float?) / sum);
                }
             }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.