c++ – vector out of range exception in my code

I am writing a C++ code for some Monte Carlo simulations. In the simulations, I am storing matrices and updating them based on some rules. So to do this, I created two classes : a 5D matrix class, and a 2D matrix class. The 5D matrix is a big 5D table which can store data types of the 2D matrix class, which is just supposed to mimic a 2D array. Both these classes are essentially just hidden vector classes, which I have defined in a way to give me the structure of the matrices required. This was the background.

Now, when I go through my Monte Carlo code updating the 5D matrix with different 2D matrices based on my requirements, for some reason, I always get a std::out_of_range: vector exception in the 5D matrix class. Moreover, this happens when I get to an element which is supposed to updated at index (size/4) of the underlying 5Dmatrix vector. When I print the size of the 5DMatrix, it gives me the same big size required though. So I am not sure where I am making a mistake? Could someone help me? My MonteCarlo Code is attached below, and I have tried to make it readable (the loops are over various dimensions of the 5D matrix)

void MonteCarlo(){
for(int i = 0; i<4; i++){
    for(int j = 0; j<8; j++){
        for(int k = 0; k<8; k++){
            for(int l = 0; l<8; l++){
                for(int m = 0; m<8; m++){
                    //Generate a random matrix for the MonetCarlo procedure
                    complex<double>** trialU = GetRandomMatrix();
                    TwoDMatrix< complex<double> > trial(3,3);
                    TwoDMatrix< complex<double> > current = U.get(i,j,k,l,m);
                    complex<double>** currentU = new complex<double>* (3);//Convert from MyMatrixType to C++ Matrix
                    for(int a = 0; a<3; a++){
                        currentU(a) = new complex<double>(3);
                        for(int b = 0; b<3; b++){
                            currentU(a)(b) = current.get(a,b);
                        }
                    }
                    //Calculate action (weights)
                    double act_new = action(currentU, i,j,k,l,m);
                    double act_old = action(trialU,i,j,k,l,m);
                    if(act_new < act_old){
                        U.replace(trial,i,j,k,l,m);
                    }
                    else {
                        double prob = (rand()%100)/100.0;
                        if(prob < exp(beta*(act_old - act_new))){
                            U.replace(trial,i,j,k,l,m);
                        }
                    }
                    for(int c1 =0; c1<3; c1++){
                        delete() trialU(c1);
                        delete() currentU(c1);
                    }
                    delete() trialU;
                    delete() currentU;
                }
            }
        }
    }
}

}

In the following, U.replace(…) is the method which gives me troubles and the error. The 5D class is defined below :

class FiveDMatrix{
int dim1;
int dim2;
int dim3;
int dim4;
int dim5;
int size;
vector<T> FDmat;
public:
FiveDMatrix(int _dim1, int _dim2, int _dim3, int _dim4,int _dim5){
    this->dim1 = _dim1;
    this->dim2 = _dim2;
    this->dim3 = _dim3;
    this->dim4 = _dim4;
    this->dim5 = _dim5;
    size = 0;
    FDmat.reserve(dim1*dim2*dim3*dim4*dim5);
}
void insert(T value){
    size++;
    FDmat.push_back(value);
}

void replace(T value, int pos1, int pos2, int pos3, int pos4, int pos5){
    //Check this weird result : If I include this line, I get an error pretty quickly FDmat(size/4) = value;
    int index = pos5 + dim5*pos4 + dim5*dim4*pos3 + dim5*dim4*dim3*pos2 + dim5*dim4*dim3*dim2*pos1;
    FDmat(index) = value;
}

T get(int pos1, int pos2, int pos3, int pos4, int pos5){
        int index = pos5 + dim5*pos4 + dim5*dim4*pos3 + dim5*dim4*dim3*pos2 + dim5*dim4*dim3*dim2*pos1;
        return FDmat.at(index);
}

int num(){
    return FDmat.size();
}

};

In fact, something which is even more weird is that if I include a line in the replace() method like I noted in the comment, I get the out of bounds error even more quickly. I do not understand why this happens. Is dynamic memory allocation playing a role? Because if I do not run this in a MonteCarlo loop but just separately run the values which seem to give error, I do not get an error. Somehow looping this turns it into an error.

Sorry for the long post, and hopefully my question is understandable. Thanks!

C++: Is a pointer to a vector going to cause memory issues?

I started to write a function which had a pointer to a vector as a parameter so that it could modify that vector to output results (as the actual return value was an error code), when I started to think about the memory behind that.

For example, if I have the following code:

std::vector<int> *vect = new std::vector<int>();

for (uint32_t i = 0; i < 10; i++)
{
    std::cout << "Ptr: " << vect << " Size " << vect->size() <<  " Max Size " << vect->capacity();
    vect->push_back(i);
    std::cout << " elements 0: " << (*vect)(0) << ", " << i << " :" << (*vect)(i) << std::endl; 
}

And I run it, I get the following output:

Ptr: 0x557c393f9e70 Size 0 Max Size 0 elements 0: 0, 0 :0
Ptr: 0x557c393f9e70 Size 1 Max Size 1 elements 0: 0, 1 :1
Ptr: 0x557c393f9e70 Size 2 Max Size 2 elements 0: 0, 2 :2
Ptr: 0x557c393f9e70 Size 3 Max Size 4 elements 0: 0, 3 :3
Ptr: 0x557c393f9e70 Size 4 Max Size 4 elements 0: 0, 4 :4
Ptr: 0x557c393f9e70 Size 5 Max Size 8 elements 0: 0, 5 :5
Ptr: 0x557c393f9e70 Size 6 Max Size 8 elements 0: 0, 6 :6
Ptr: 0x557c393f9e70 Size 7 Max Size 8 elements 0: 0, 7 :7
Ptr: 0x557c393f9e70 Size 8 Max Size 8 elements 0: 0, 8 :8
Ptr: 0x557c393f9e70 Size 9 Max Size 16 elements 0: 0, 9 :9

It seems as though this could cause major memory issues – because if the vector needs to expand, it could be writing into space which is already being utilized, because it looks like that pointer does not change. Even running this over a much larger loop, this pointer looks like it is constant.

I’m still a (relatively) new programmer, and am not sure that I have the grasp on memory allocation that I would like to. Is my understanding correct – will this cause buffer errors and overwrite adjacent memory? Or is there some protection in std::vector that I am not considering?

Vector Notation of $nablacdotleft(left(vec{v}cdotnablaright)vec{v}right)$ where $0=nablacdotvec{v}$

I want a simplified way of writing the above expression in a clear way using vector notation. It is given that $vec{v}$ has a divergence of 0. I’ve found that
$$nablacdotleft(left(vec{v}cdotnablaright)vec{v}right)=left(vec{v}cdotnablaright)left(nablacdotvec{v}right)+sum_{i=1}^{3}sum_{j=1}^{3}left(frac{partial v_j}{partial x_i}right)left(frac{partial v_i}{partial x_j}right)=sum_{i=1}^{3}sum_{j=1}^{3}left(frac{partial v_j}{partial x_i}right)left(frac{partial v_i}{partial x_j}right)=Trace{left(left(nablavec{v}right)^2right)}$$
To clarify,
$$left(vec{A}cdotnablaright)vec{B}=A_xleft(frac{partialvec{B}}{partial x}right)+A_yleft(frac{partialvec{B}}{partial y}right)+A_zleft(frac{partialvec{B}}{partial z}right)$$

nt.number theory – Classification of vector spaces with a quadratic form and an order n automorphism

Introductory general nonsense (for motivation: feel free to skip): Let $G$ be a finite group and $k$ be a field of characteristic $0$. Consider the set $mathcal{S}$ of isomorphism classes of finite dimensional vector spaces $V$ over $k$ endowed with (a) a nondegenerate quadratic form $qcolon Vto k$, and (b) a linear action $G to mathit{GL}(V)$, which are compatible in the sense that $G$ preserves $q$ (viꝫ. $q(gcdot v)) = q(v)$ for $gin G$). Note that we can take direct sums and tensor products of such data, giving $mathcal{S}$ a semiring (“ring without subtraction”) structure; we can also form the Grothendieck group $mathcal{R}$ of $mathcal{S}$, which is a ring.

Classifying (a) alone and (b) alone is well studied: (a) gives the Grothendieck-Witt ring of $k$, and (b) gives the representation ring of $G$ over $k$. I’m curious about what can be said about both data simultaneously (and compatibly). We have obvious ring homomorphisms from $mathcal{R}$ to the Grothendieck-Witt ring of $k$ and to the representation ring of $G$ over $k$, but I think $mathcal{R}$ (generally) isn’t a fiber product of them, and I suppose there isn’t much we can say at this level of generality (though I’d be happy to be wrong!).

I might still point out that if $V = V_1 oplus V_2$ is a decomposition of $V$ as representations of $G$ and there is no irreducible factor common in $V_1$ and the dual $V_2^vee$ of $V_2$, then necessarily $V_1$ and $V_2$ are orthogonal for $q$ (proof: apply Schur’s lemma to the $G$-invariant linear map $V_1 to V_2^vee$ obtained from $q$). So we are reduced to classifying elements of $mathcal{R}$ (or $mathcal{S}$) whose underlying representation is of the form $U^r$ for $U$ an irreducible self-dual representation of $G$, or of the form $(Uoplus U^vee)^r$ for an irreducible non-self-dual representation $U$.

Anyway, let me concentrate on the important special case where $k=mathbb{Q}$ and $G=mathbb{Z}/nmathbb{Z}$. The irreducible representations of $mathbb{Z}/nmathbb{Z}$ over $mathbb{Q}$ are of the form $U_d$ (self-dual) for $d$ dividing $n$ where $U_d$ splits over $mathbb{C}$ as sum of one-dimensional representations on which a chosen generator acts through each of the primitive $d$-th roots of unity. So I ask:

Actual question: Given $d,n,rgeq 1$ be integers such that $d$ divides $n$, let $U_d$ be the irreductible representation of $mathbb{Z}/nmathbb{Z}$ over $mathbb{Q}$ such that the generators act with characteristic polynomial given by the $d$-th cyclotomic polynomial. Can we classify quadratic forms on $(U_d)^r$ which are invariant under the action of $mathbb{Z}/nmathbb{Z}$ (i.e., describe the corresponding elements of the (known) Grothendieck-Witt ring of $mathbb{Q}$)? Or equivalently, in the other direction, given a quadratic form $(V,q)$ over $mathbb{Q}$ (through its image in the G-W ring), can classify $mathbb{Z}/nmathbb{Z}$-actions (over $V$, linear, preserving $q$) which make $V$ isomorphic to $(U_d)^r$?

I don’t even know the answer when $q$ is the standard Euclidean form (viꝫ. $mathbb{Q}^m$ with the quadratic form $x_1^2 + cdots + x_m^2$): for which $d,n,r$ is there a $mathbb{Z}/nmathbb{Z}$-invariant quadratic form on $(U_d)^r$ that is isomorphic to this?

Note: there is a $mathbb{Z}/nmathbb{Z}$-invariant standard Euclidean structure on $mathbb{Q}^n = bigoplus_{d|n} U_d$ with cyclic permutation of the coordinates, which induces a quadratic form on each of the $U_d$ so that this direct sum is orthogonal (the class of this form in the G-W ring can be computed by the Möbius inversion formula; because there is a scaling involved, it depends on $n$, not just $d$). It might be tempting to think that all $mathbb{Z}/nmathbb{Z}$-invariant quadratic forms on $U_d$ are obtained in this way: if my Witt ring calculations are correct, this is not the case: $U_{30}$ does not get a standard Euclidean structure that way; but there is a standard Euclidean structure on $U_{30}$, namely, take the Coxeter element of the Weyl group of $E_8$ as acting on $mathbb{Q}^8$ with its standard Euclidean structure, which is then $U_{30}$ as a representation of $mathbb{Z}/30mathbb{Z}$.

linear algebra – can you describe the span of a two dimensional vector space, as the solutions of a degree 1 polynomial in the affine field $K^n$?

I will just like to prove this statement to help me understand affinge geometry better.

can you describe the span of a two dimensional vector space, as the solutions of a degree 1 polynomial in the affine field $K^n$?

E.g
For $mathbb{R}^3$, any two dimension subspace is a hypersurface, hence can be described as,
$ax+by+cz+d=0$,
I was wondering if this could be generalised, and if you can provdie a proof or literature that could help.

vector spaces – understand of span, linear independence and basis by using dimension

Before start explaining what makes me confused, I’m sorry about my poor English. I’m not good at English. lol

If V is Finite-dimensional vector space, let {v1,v2, ⋯,vn} is abritary basis of V.
(a)The set of vectors that have more than n is linearly dependent set.
(b)The set of vectors that have less than n can’t span V

I wanna know that (b) means if we have more than n vectors when the basis of the V is n, we can just span V.

I’m confused because I’m not sure about the concept of span and linear combination exactly.

If S={v1,v2,⋯vr}, S’={w1,w2,⋯wk} is a vector set that is included in vector space V, if and only if span{v1,v2,⋯vr}=span{w1,w2,⋯wk} is that each vector of S is linear combination of w1,w2,⋯wk and also each vector of S’ is linear combination of v1,v2,⋯vr.

I wanna know this sentence is right.
If there are ***n**-times basis* *(I mean the number of the basis vector is n)* and n≦r, n≦k then S,S' can span vector space V and also S,S' is linearly dependent set.

How to add a vector to a multiple dimension list of vectors

This problem is related to Add a vector to a list of vectors.
Given:

v1 = {a, b};
v2 = {{{d, e}, {{g, h}, {r, s}}}, {j, k}};

I want to add v1 to all subvectors of v2. I can cheat and do it by

t1 = Outer[Plus, {v1}, v2, 1][[1]]
Map[List, t1, {3}]

to get the solution

{{{{a + d}, {a + e}}, {{{b + g, b + h}}, {{b + r, b + s}}}}, {{a + j}, {b + k}}}

But I am looking for a more general solution which works on more complex dimensions for v2.