1// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
2// Copyright (C) INRIA - Vincent Couvert
3//
4// Copyright (C) 2012 - 2016 - Scilab Enterprises
5//
6// This file is hereby licensed under the terms of the GNU GPL v2.0,
7// pursuant to article 5.3.4 of the CeCILL v.2.1.
8// This file was originally licensed under the terms of the CeCILL v2.1,
9// and continues to be available under such terms.
10// For more information, see the COPYING file which you should have received
11// along with this program.
12
13function M=%hm_i_i(varargin)
14    //insertion of an hypermatrix in a matrix  derived from %hm_i_hm
15    [lhs,rhs]=argn(0)
16    M=varargin(rhs) //Matrix
17    N=varargin(rhs-1)//inserted hypermatrix
18    dims=size(M)'
19
20    v=M(:)
21
22
23    nd=size(dims,"*")
24    if rhs-2>nd then dims(nd+1:rhs-2)=1;end
25
26    //convert N-dimensional indexes to 1-D
27    [Ndims,I]=convertindex(list(dims,double(matrix(N.dims,1,-1))),varargin(1:$-2))
28
29    if or(Ndims>dims) then
30        //extend the destination matrix
31        I1=0
32        for k=size(Ndims,"*"):-1:1
33            ik1=(1:dims(k))'
34            if ik1<>[] then
35                if Ndims(k)>1 then
36                    if size(I1,"*")>1 then
37                        I1=(Ndims(k)*I1).*.ones(ik1)+ones(I1).*.(ik1-1)
38                    else
39                        I1=Ndims(k)*I1+ik1-1
40                    end
41                else
42                    I1=Ndims(k)*I1+ik1-1
43                end
44            end
45        end
46        select type(v)
47        case 1
48            v1=zeros(prod(Ndims),1)
49        case 2 then
50            v1=zeros(prod(Ndims),1)
51        case 4 then
52            v1=(zeros(prod(Ndims),1)==1)
53        case 8 then
54            v1=iconvert(zeros(prod(Ndims),1),inttype(v))
55        case 10 then
56            v1=emptystr(prod(Ndims),1)
57        end
58        v1(I1+1)=v;v=v1
59    end
60
61    v(I)=matrix(N.entries,-1,1)
62
63    while  Ndims($)==1 then Ndims($)=[],end
64    select size(Ndims,"*")
65    case 0
66        M=v
67    case 1
68        M=v
69    case 2
70        M=matrix(v,Ndims(1),Ndims(2))
71    else
72        M=mlist(["hm","dims","entries"],int32(matrix(Ndims,1,-1)),v)
73    end
74endfunction
75