array – I want to Prepend the Row Number of the Matrix to all Elements within that row

Platform: Mathematica

Hello all!

I have a matrix of nested lists that looks like this:

matrix =
{{{1, 2}, {3, 4}, {5, 6}, {7, 8}, {9, 0}},
{{2, 3}, {4, 5}, {6, 7}, {8, 9}, {0, 1}},
{{1, 3}, {2, 4}, {3, 5}, {4, 6}, {5, 7}}}

and I want to Prepend the row number ‘x’ to each of the elements within each row like this:

{{{x, 1, 2}, {x, 3, 4}, {x, 5, 6}, {x, 7, 8}, {x, 9, 0}},
{{x, 2, 3}, {x, 4, 5}, {x, 6, 7}, {x, 8, 9}, {x, 0, 1}},
{{x, 1, 3}, {x, 2, 4}, {x, 3, 5}, {x, 4, 6}, {x, 5, 7}}}

so that the final product looks like this:

{{{1, 1, 2}, {1, 3, 4}, {1, 5, 6}, {1, 7, 8}, {1, 9, 0}},
{{2, 2, 3}, {2, 4, 5}, {2, 6, 7}, {2, 8, 9}, {2, 0, 1}},
{{3, 1, 3}, {3, 2, 4}, {3, 3, 5}, {3, 4, 6}, {3, 5, 7}}}

I have played around with something like MapIndexed[Prepend[#,x]&,matrix,{3}] which successfully gets me to the intermediate matrix as described above where “x” is prepended, but I can’t figure out how to make “x” conditionally equal the index of the row.

Your help is very much appreciated! Thanks so much in advance!!!

Best regards,
Taylor

plugin development – HTMLCollection not counting right in editor? / for loop not working on elements in DOM

I’m migrating my front-end code for a plugin to React and ESNext, and am still new to all of this, but for some reason, this really simple for loop isn’t executing on the admin edit page, but is fine when viewing the post. The only thing I can see is that there seems to be a weird issue with how HTMLCollections are counted, on the back end. I have this code:

/*
 * external dependencies
 */
import * as React from "react";
import { render } from "react-dom";

export default class App extends React.Component {
        render() {
                return (
                        <div className={"myapp-container"}>
                           // this is a test
                        </div>
                );
        }
}

document.addEventListener("DOMContentLoaded", (event) => {
        let els = document.getElementsByClassName("myapp_wrapper_class");
        console.log("got elements ", els); // this fires fine, everywhere

        for (let el of els) {
                console.log("working with ", el); // this fires on the front-end, but not in the editor
                render(<App />, document.getElementById(el.id));
        }
});

When I look at the console, on the front end, it logs

got elements 
HTMLCollection (1) // note this "1"
0 <div id="myApp1" style="width: 100%; height: 300px;" class="myapp myapp_wrapper_class" data-cwragc-src="https://localhost:8888/wp-content/uploads/2021/01/nakaza.csv" data-cwragc-type="line"></div>

but in the editor, it logs

got elements 
HTMLCollection (0) // note this "0"
0 <div id="myApp1" style="width: 100%; height: 300px;" class="myapp myapp_wrapper_class" data-cwragc-src="https://localhost:8888/wp-content/uploads/2021/01/nakaza.csv" data-cwragc-type="line"></div>

I’m unsure whether that zero is the problem, but like I said, it’s the only thing I can see that’s weird. There are no error messages in the console. I am sure that the for loop isn’t being executed on the back end, even though the element is there (as you can see in the console log), and exists in the rendered DOM.

The elements are created with a server-side render. In the block’s edit function, it looks thusly:

export function MyAppEdit( props ) {
//...
        const MyAppRender = () => {
                return(
                    <Disabled>
                        <ServerSideRender
                            block={ props.name }
                            attributes={{ ...attributes }} />
                    </Disabled>
                );
        }
//...
        return (
                <>
                        { controls }
                        <div { ...blockProps }>
                                { cwragcLocalFile && <MyAppRender /> }
                                { myAppPlaceholder }
                        </div>
                </>
        );
}
export default withNotices( MyAppEdit );

Grasping at straws, I also tried removing the <Disabled> wrapper on the editor side, but no change in effect.

Basically, I have no idea what’s going on here, but I’d like my stuff to render on the back end 🙂

Is it possibly a Babel bug? Is there some difference/timing/race issue with when DOMContentLoaded fires on the back end? Is there some better/more naturally React way for me to add a component to a div when I don’t know how many of them there will be (other than getElementsByClassName after DOMContentLoaded?

For completeness’ sake, my webpack is this:

const defaultConfig = require( '@wordpress/scripts/config/webpack.config' );
const path = require('path');

module.exports = {
    ...defaultConfig,
    entry: {
        myAppAdmin:
            path.resolve( process.cwd(), 'admin/src', 'index.js' ),
        myAppPublic:
            path.resolve( process.cwd(), 'public/src', 'index.js' ),
    },
    output: {
        filename: '(name).js',
        path: path.resolve( process.cwd(), 'assets/js' ),
    },
}

Maybe I’m missing some other require? But, like I said, it’s not generating any errors. admin/src/index.js is the block code; public/src/index.js is front-end code I’m trying to migrate, that adds React stuff after the server-side render.

functional analysis – A subset of the vector space of all real sequences with finitely-many non-zero elements. Boundedness, closedness, compactness and completeness.

Let $X$ be the space of all real sequences with finitely many nonzero elements, equipped with the supremum norm. That is, for each $x= (x_k)_{k in mathbb N}$ we have:
$$ |x | = sup { |x_k| kin mathbb N }.$$
Consider the subset $V$ of $X$ defined by:
$$ V:= { x= (x_k)_{k in mathbb N} in X | sum_{k=1}^infty |x_k| leq 1 }.$$

I wish to answer the following practice exam questions:

  1. Is $V$ bounded in $X$?
  2. Is $V$ closed in $X$?
  3. Is $V$ compact in $X$?
  4. Is $V$ complete in $X$?

  1. Consider $x=(x_n)_{n in mathbb N}in V$, we then have that for each $nin mathbb N$ we may write:
    $$ |x_n| leq sum_{k=1}^infty|x_k| leq 1 $$
    Each term is nonnegative and therefore the entire sum is certainly more than a single term. We thus find that $1$ is an upper bound for each element of the sequence hence certainly it is larger than or equal to the least upper bound:
    $$ | x| leq 1. $$
    We conclude that $V$ is bounded.
  2. I think this is not true as there are sequences in $V$ that leave the space (and $X$). Consider $x^{(n)}$ given by:
    $$x^1= (frac{1}{2}, 0, 0, dots) $$
    $$x^2=(frac{1}{2}, frac{1}{4}, 0, dots) $$
    $$x^3=(frac{1}{2}, frac{1}{4}, frac{1}{8},0, dots) $$
    Which might be described as $(x^n_k)_{k in mathbb N}$ with $x^n_k=frac{1}{2^{k}}$ for $kleq n$ and $0$ otherwise ($0 not in mathbb N$). Each of the elements of this sequence lives in $X$ and since the geometric series converges to $1$ also in $V$. So $$ sum_{k=1}^infty |x^k_n| leq 1 .$$ However, we run into a problem since its limit is $x=(x_k)_{kin mathbb N}$ with $x_n=frac{1}{2^n}$, which does not have finitely many nonzero elements hence it is not in $X$ and then certainly not in $V$.
  3. Compact normed vector spaces are closed, since the space is not closed, it cannot be compact.
  4. The space cannot be complete as the example in (2) is a Cauchy-sequence, but the sequence does not converge to a limit in $V$. In a complete space every Cauchy sequence is convergent (with limit in the space).

Did I do this okay?

javascript – Having trouble swapping elements

I’m trying to visualize a simple Hanoi solver that uses an algorithm, and I’m swapping elements on array to visualize it.

Here’s my code:

        let A = ();
        let B = ();
        let C = ();
        let moveCount = 0;
        let once = true;
        hanoi(3, A, C, B);
// 3, how many disk
// A C B are rods
        alert(A + '.n' + B + '.n' + C + '.')
        
        
        function hanoi(n, start, target, other)
        {
            if (once)
            {
                once = false;
                for (var i = 1; i <= n; i--)
                {
                    start.push(i);
                }
            }
            
            
            if (n <= 1)
            {
                let temp = start(n-1);
                start(n-1) = target(target.length);
                start = start.filter(Boolean)
                target(target.length) = temp;
                target = target.filter(Boolean);
                //console.log('Moving disk '+n+' from rod '+start+' to '+target+' rod');
                moveCount++;
            }
            else
            {
                hanoi(n-1, start, other, target);
                
                let temp = start(n-1);
                start(n-1) = target(target.length);
                start = start.filter(Boolean)
                target(target.length) = temp;
                target = target.filter(Boolean);
                //console.log('Moving disk '+n+' from rod '+start+' to rod '+target);
                moveCount++;
                
                hanoi(n-1, other, target, start);
            }
        }

The C value should be expected (3, 2, 1) but only shows 2. I also checked A and B and 1 is nowhere to be found

How do I make this properly so the output would be (3, 2, 1)

Imagine 3 is the largest disk and so on…

nt.number theory – Products of short elements in a field

Consider a field $F$ of characteristic zero. Let $L=F[alpha]$ be an extension of degree $d.$ We call an element
$$
x=a_0 + a_1 alpha +ldots+ a_{d-1}alpha^{d-1}in L
$$

short if $a_{d-1}=0.$
Under which conditions on $alpha$ every element in $L^times$ is a product of short elements?

It is easy to see that every element of $L$ is a quotient of two short elements for $dgeq 3$.

extension field – Why does the set of algebraic elements of $K/F$ contain $F$?

Let $K/F$ be a field extension and $tilde{F}$ the set of elements of $K$ that are algebraic over $F$, i.e.

$$tilde{F}={alpha in K mid alpha text{algebraic over} F }$$
in my lecture notes for university there is a corollary which states that $tilde{F}$ is a subfield of $K$ which contains $F$. I’m interested in the last part, i.e. why does $tilde{F}$ contain $F$? In other words, why is every $x in F$ algebraic over $F$?

arrays – non-zero elements C++

I would be glad, if you help me with this problem:

I have two-dimenssion array with m rows and n columns. I have to write a program with C++, which creates a new array A, whose value is equal to average of the non-zero elements in the i-th row in G.

#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

const int m = 5; 
const int n = 5;
int G(m)(n); 
int i, j;
double sum = 0, A;

void randNum() 
{
srand(89); 
for (i = 0; i < m; i++) 
{
    cout << endl << endl;
    for (j = 0; j < n; j++) 
    {
        G(i)(j) = rand() % 10;
        cout << setw(6) << G(i)(j);
    }
}
cout << endl << endl;
}

void randAvg() 
{
for (int i = 0; i < m;i++)
{
    float sum = 0.f;
    for (int j = 0; j < n; j++) {
        sum += G(i)(j);

    }
    float A = sum / m;
    cout << "Average of row " << i << ": " << A << endl;
}

float sum = 0.f;
for (int i = 0; i < m; i++)
{
    for (int j = 0; j < n; j++) {
        sum += G(i)(j);
    }
}
float A = sum / (m * n);
cout << "Overall average: " << A << endl;
}


int main() 
{   
randNum();
randAvg();
}

This is the code that I wrote, but these “non-zero elements” are still there.