示例#1
0
        public void SelectOnMultipleIndexes()
        {
            // more complex example (isn't it another test?)
            var array = new MultiDimensionalArray(3, 3);

            // 1 9 2     1   2
            // 9 9 9 ==>
            // 3 9 4     3   4
            array[0, 0] = 1;
            array[0, 1] = 9;
            array[0, 2] = 2;
            array[1, 0] = 9;
            array[1, 1] = 9;
            array[1, 2] = 9;
            array[2, 0] = 3;
            array[2, 1] = 9;
            array[2, 2] = 4;

            IMultiDimensionalArrayView view = array.Select(0, new[] { 0, 2 }).Select(1, new[] { 0, 2 });

            Assert.IsTrue(new[] { 2, 2 }.SequenceEqual(view.Shape));
            Assert.AreEqual(4, view.Count);
            Assert.AreEqual(1, view[0, 0]);
            Assert.AreEqual(2, view[0, 1]);
            Assert.AreEqual(3, view[1, 0]);
            Assert.AreEqual(4, view[1, 1]);
        }
示例#2
0
        public void SelectUsingIndexes()
        {
            IMultiDimensionalArray array = new MultiDimensionalArray(3);

            array[0] = 1;
            array[1] = 2;
            array[2] = 3;

            IMultiDimensionalArrayView view = array.Select(0, new[] { 0, 2 });

            // Select first and last element
            Assert.AreEqual(2, view.Count);
            Assert.AreEqual(1, view[0]);
            Assert.AreEqual(3, view[1]);

            Assert.AreEqual(3, view.MaxValue);
            Assert.AreEqual(1, view.MinValue);
        }
示例#3
0
        public void Bind3DArrayUsing2DView()
        {
            IMultiDimensionalArray array = new MultiDimensionalArray(3, 3, 3);

            array[0, 0, 0] = 1;
            array[1, 1, 1] = 2;

            IMultiDimensionalArrayView view = array.Select(0, 0, 0);

            view.Reduce[0] = true; // reduce 1st dimension

            DataGridView gridView = new DataGridView();

            MultiDimensionalArrayBindingList multiDimensionalArrayBindingList = new MultiDimensionalArrayBindingList(view);

            gridView.DataSource = multiDimensionalArrayBindingList;

            Form form = new Form();

            gridView.Dock = DockStyle.Fill;
            form.Controls.Add(gridView);

            WindowsFormsTestHelper.ShowModal(form);
        }
        /// <summary>
        /// Prepares the Multidimensional array of function. Expands dimensions where needed.
        /// TODO: this and above methods seem to be redundant
        ///
        /// TODO: shouldn't it happen in Function?
        /// </summary>
        /// <param name="function"></param>
        /// <param name="filters"></param>
        /// <returns></returns>
        private IMultiDimensionalArray CreateComponentArrayFromFilters(IFunction function,
                                                                       IList <IVariableFilter> filters)
        {
            if (filters.Count == 0)
            {
                return(FunctionValues[Functions.IndexOf(function)]);
            }

            var variable = (IVariable)function;

            // HACK: will create generic view. based on maxInt
            IMultiDimensionalArrayView view = FunctionValues[Functions.IndexOf(variable)].Select(0, int.MinValue,
                                                                                                 int.MaxValue);

            // check if we have aggregation filters, if yes - return a new array, otherwise return a view based on existing array
            // probably it should be always a view based on existing array, even when aggregation is used
            if (filters.Any(f => f is VariableAggregationFilter))
            {
                var filterVariableIndex = new Dictionary <VariableAggregationFilter, int>();
                foreach (VariableAggregationFilter filter in filters.OfType <VariableAggregationFilter>())
                {
                    filterVariableIndex[filter] = variable.Arguments.IndexOf(filter.Variable);
                }

                // calculate shape of the resulting array
                var shape = (int[])view.Shape.Clone();
                foreach (VariableAggregationFilter filter in filters.OfType <VariableAggregationFilter>())
                {
                    shape[filterVariableIndex[filter]] = filter.Count;
                }

                // now fill in values into the resulting array
                IMultiDimensionalArray result = variable.CreateStorageArray();
                result.Resize(shape);

                int totalLength = MultiDimensionalArrayHelper.GetTotalLength(shape);
                for (int i = 0; i < totalLength; i++)
                {
                    int[] targetIndex = MultiDimensionalArrayHelper.GetIndex(i, result.Stride);

                    // calculate index in the source array
                    var sourceIndex = (int[])targetIndex.Clone();
                    foreach (VariableAggregationFilter filter in filters.OfType <VariableAggregationFilter>())
                    {
                        int filterArgumentIndex = filterVariableIndex[filter];
                        sourceIndex[filterArgumentIndex] = GetSourceIndex(filter, targetIndex[filterArgumentIndex]);
                    }

                    result[targetIndex] = view[sourceIndex]; // copy value (only required)
                }

                view = result.Select(0, int.MinValue, int.MaxValue);
            }

            //use variable filters that are relevant to make a selection
            IEnumerable <IVariableFilter> relevantArgumentFilters =
                filters.OfType <IVariableFilter>().Where(
                    f =>
                    (f is IVariableValueFilter || f is VariableIndexRangesFilter || f is VariableIndexRangeFilter) &&
                    (f.Variable == function || function.Arguments.Contains(f.Variable)));

            foreach (IVariableFilter variableFilter in relevantArgumentFilters)
            {
                //determine indexes for this variable..get in a base class?
                int[] indexes = GetArgumentIndexes(variableFilter);

                if (function.IsIndependent && variableFilter.Variable == function)
                {
                    view.SelectedIndexes[0] = indexes; // indepedent variable always dim 0
                }
                else if (function.Arguments.Contains(variableFilter.Variable))
                {
                    int dimensionIndex = function.Arguments.IndexOf(variableFilter.Variable);
                    view.SelectedIndexes[dimensionIndex] = indexes;
                }
            }

            //reduce the view for reduce filters.
            foreach (VariableReduceFilter filter in filters.OfType <VariableReduceFilter>())
            {
                int index = function.Arguments.IndexOf(filter.Variable);
                if (index != -1)
                {
                    view.Reduce[index] = true;
                }
            }

            return(view);
        }