Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Squeeze #25

Closed
wilzbach opened this issue Mar 5, 2016 · 11 comments
Closed

Squeeze #25

wilzbach opened this issue Mar 5, 2016 · 11 comments

Comments

@wilzbach
Copy link
Member

wilzbach commented Mar 5, 2016

Yet another nice method that NumPy has.

Remove single-dimensional entries from the shape of an array.

>>> x = np.array([[[0], [1], [2]]])
>>> x.shape
(1, 3, 1)
>>> np.squeeze(x).shape
(3,)
>>> np.squeeze(x, axis=(2,)).shape
(1, 3)

https://docs.scipy.org/doc/numpy-1.10.1/reference/generated/numpy.squeeze.html#numpy.squeeze

@9il
Copy link
Member

9il commented Mar 5, 2016

np.squeeze(x, axis=(2,)).shape is equal to x.transposed!2[0]. Would it be OK?

@9il
Copy link
Member

9il commented Mar 5, 2016

You can create PR for squeeze if you want)

@wilzbach
Copy link
Member Author

wilzbach commented Mar 5, 2016

The problem with squeeze is that you don't know how many dimensions it will return on compile-time, so we need to create a Slice with dynamic dimensions ...
@9il any idea how to do this?

auto squeeze(size_t N, Range)(auto ref Slice!(N, Range) slice)
{
    size_t nonSqueezeDims = 0;
    foreach (i; Iota!(0, N))
        if (slice._lengths[i] > 1)
            nonSqueezeDims += 1;

    Slice!(1, Range) ret; // how to make a slice of a dynamic dimension?
    size_t dims = 0;
    foreach (i; Iota!(0, N))
    {
        if (slice._lengths[i] > 1)
        {
            ret._lengths[dims] = slice._lengths[i];
            ret._strides[dims] = slice._strides[i];
            dims++;
        }
    }
    ret._ptr = slice._ptr;
    return ret;
}
unittest
{
    import mir.ndslice.slice;
    import std.range: iota;
    auto s = iota(3).sliced(1, 3, 1);
    s.squeeze.writeln;
    // [0, 1, 2]
}

@9il
Copy link
Member

9il commented Mar 5, 2016

@9il any idea how to do this?

No idea is good. This is major difference between numpy and ndslice

@wilzbach
Copy link
Member Author

wilzbach commented Mar 5, 2016

No idea is good. This is major difference between numpy and ndslice

Hmm while compile-time dimensions are really awesome, this seems to be a major limitation that we should think about if we ever want to challenge NumPy (or similar libs).

Pinging reviewers and participants of the merge into phobos for their opinions and ideas:

@John-Colvin @burner @TurkeyMan @nordlow @SFrijters @JackStouffer @klickverbot

@9il
Copy link
Member

9il commented Mar 5, 2016

@greenify This is not very good idea to ping all at once =)

Could you provide a real world example where this feature is required?

@John-Colvin
Copy link
Contributor

@absolutelyEveryoneEver I'll look at this tomorrow

@wilzbach
Copy link
Member Author

wilzbach commented Mar 5, 2016

This is not very good idea to ping all at once

I am sorry - acted to rash. I will remember it. Sorry for spamming you @ pinged people.

Could you provide a real world example where this feature is required?

How you would implement loadtxt or loadmat if you don't know the dimensions before compile-time?

@9il
Copy link
Member

9il commented Mar 5, 2016

How you would implement loadtxt or loadmat if you don't know the dimensions before compile-time?

You always can use 2D matrix

@9il
Copy link
Member

9il commented Mar 5, 2016

Or you can use unions

@wilzbach
Copy link
Member Author

wilzbach commented Mar 6, 2016

Could you provide a real world example where this feature is required?

I have thought long about this and no I can't.
The best example that I could find is here:

http://northstar-www.dartmouth.edu/doc/idl/html_6.2/REFORM.html

You always can use 2D matrix

Well let's assume you want to load a file from MatLab/R which is unknown to one, e.g.

VarSlice s = loadmat("foo.m"); // loads a 4-dimensional matrix, dimension come from file
s.sum;

Of course one could write a wrapper around such format to determine the dimensions, but ultimately we need to its dimensions and I guess that any application that wants to do more based on dynamic dimensions is rare and well better off to switch to a dynamic language.

Maybe it was just too many years of programming in dynamically typed languages (Python, JavaScript, NodeJs, ...) that I saw this to be problematic.

==> a big sorry to all the people that I spammed, but at least know you know that development for mir is happening here ;-)

Just for complete here's a list of squeeze implementations in dynamic languages that I found during a short search:

Octave:https://www.gnu.org/software/octave/doc/interpreter/Object-Sizes.html
Matlab: https://se.mathworks.com/help/matlab/ref/squeeze.html#bum54rg
R: https://stat.ethz.ch/R-manual/R-devel/library/base/html/drop.html
JS: http://mathjs.org/docs/datatypes/matrices.html
FreeMat: http://freemat.sourceforge.net/help/array_squeeze.html

@wilzbach wilzbach closed this as completed Mar 6, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants