Efficiently finding the unique non-zero columns after a variable column in a data table

Suppose I have the following data:

        tempmat=matrix(c(1,1,0,4,1,0,0,4,0,1,0,4, 0,1,1,4, 0,1,0,5),5,4,byrow=T)         tempmat=rbind(rep(0,4),tempmat)         tempmat=data.table(tempmat)         names(tempmat)=paste0('prod1vint',1:4)         tempmat[,firstnonzero:=c(NA,1,1,2,2,2)] 

So the data table looks like this:

> tempmat    prod1vint1 prod1vint2 prod1vint3 prod1vint4 firstnonzero 1:          0          0          0          0           NA 2:          1          1          0          4            1 3:          1          0          0          4            1 4:          0          1          0          4            2 5:          0          1          1          4            2 6:          0          1          0          5            2 

I want to find the number of nonzero elements to the RIGHT of the column indicated by “firstnonzero”.

To make this clear, the desired output then is:

> tempmat    prod1vint1 prod1vint2 prod1vint3 prod1vint4 firstnonzero numbernonzero 1:          0          0          0          0           NA            NA 2:          1          1          0          4            1             2 3:          1          0          0          4            1             1 4:          0          1          0          4            2             1 5:          0          1          1          4            2             2 6:          0          1          0          5            2             1 

This is because, for example, on row 2, there is a nonzero element in prod1vint2 and prod1vint4, so the number of nonzero elements to the right of the first nonzero element is 2, and so forth.

This needs to be efficient and scale well, so it cannot be an apply or looping style solution.

Is there a way to inherit a method that points to a class’ own private variable?

I am trying to have a child class inherit a method from some parent class that points to parent’s own private variable. I want the inherited method of the child to use the child’s own private variable. Is this possible, without redefining the whole method in the child class?

The code works fine if using public or protected (single underscore) variables. But our instructor insists on using private (double underscore) variables. We are only a couple of days into object-oriented programming so I do not really know what is the term I am looking for (sorry for that!).

I suspect I should only be using protected ones, but I want to comply with the instructions as much as possible.

class ClassA:     def __init__(self): self.__var = 10      def method1(self):         self.__var += 1     #method1 is intended to work on ClassA's __var         return self.__var   #The method will also return the processed value of ClassA's __var  class ClassB(ClassA):     def __init__(self): self.__var = 20      #ClassA's method1 is inherited by ClassB.      #But I want this method1 to use ClassB's __var   ObjectA = ClassA() ObjectB = ClassB()  print(ObjectA.method1()) #Will output 11 print(ObjectB.method1()) #Will raise an AttributeError, still looks for __var of ClassA 

Above is just a demonstration of what I intend to achieve.

When using public or protected variables, I will have an output of 11 and 21.
When using private variables however, I will have an 11 and an error.

I want ClassB‘s method1 to use ClassB‘s __var (_ClassB__var).

I could get ClassB to have a variable named _ClassA__var and set it to 20, and it does work, but that is kind of messy IMO.

Variable and slow internet speed on my Mac compared to other devices connected on the same network

Background.: I have a 40 Mbps Ethernet connection hooked directly to my iMac running on Mojave.

I use the Mac as a router to share the internet connection from Ethernet to other devices using WiFi.

Problem.: Now the problem is, the ‘other’ devices are easily hitting 15-20 Mbps download speeds, on the contrary my Mac being directly connected to the Ethernet can only reach download speeds up to 4 Mbps!

I don’t know what’s the reason behind this.

How can I find the expected value of this variable?

The initial cost of a machine is $ 3. The life-time, T, of that machine has an exponential distribution with an expected value equal to 3 years. The maker wants to offer a warranty that pays $ 3 if the machine gets broken during the first year, $ 2 if the machine gets broken during the second year, and $ 1 if the machine gets broken during the third year.

I know that T ~ Exp(1/3) and that the warranty payment function looks like this:

X = 3 (for T <= 1), 2 (for 1 < T <= 2), 3 (for 2 < T <= 3)

To find E[X] I tired the following: 3*P(T <= 1) + 2*P(1 < T <= 2) + 3*P(2 < T <= 3), which gives me approximately 1.402. However, I don’t know if that is the right way to do it.

Pass variable from custom module to twig template

How can I pass a variable from my custom module to a twig field template?

my field template is the “field–node–field-webform–show-visit.html.twig”

and my module is named as “custommodule”

I tried this function according to documentation but apparently I didnt understand smth correctly:

  function logintosubmit_preprocess_field_node_field_webform_parliament_visit(array &$  variables) {     $  VARIABLE_NAME = 'my_variable';     $  variables['varname'] = $  VARIABLE_NAME ;   } 

How do I get a URL variable in a template?

I am trying to include a certain .html file if the URL has ?work=1.

So far I have this, which I got from Check URL for string in a Twig file.

 {% if 'work' in url %}     {{ include('work/index.html') }}  {% endif %} 

They say I need a function in my .theme file to actually get the URL variable.
Is that right? Is there any way to get it with just Twig?

how to assign file name with full path to a variable in bash script

suppose i opened my file manager nemo and select a file named “abc” so in the location bar it shows media/myuser/folder1/subfoler/ and the selected file name is abc How can assign the selected (highlighted) file name with full path in a variable in my bash script (please note this file is not in home directory) anybody can help me please

Ayuda con mostrar contenido en variable data:blog.etc en blogger

Estoy intentando crear una plantilla de blogger, quiero poder mostrar el texto de una id que tengo dentro de una página estatica.

<!-- Como lo tengo --> <div class="post-title"><data:post.title/></div>  <!-- lo que quiero hacer, es un ejemplo, ya que no sé como llamarla --> <div class="post-title"><data:post.#content/></div>  <!-- el div con la id que se encuentra dentro de la página estatica --> <div id="content"></div> 

What is the distribution of the modulo of a uniformly-distributed random variable

This feels like something that’s easy to answer, but maybe not. (For the record, this isn’t homework from school, it’s to settle an argument I’m having with a colleague.)

I have a random variable $ X$ that’s a value in $ K = [0, 2^{64})$ . Given that $ X$ is uniformly distributed over $ K$ (that is, the probability density function of $ X$ is $ f(x) = \frac{1}{2^{64}}$ ), what is the distribution of $ Y = X \bmod k$ ?

My gut says it’s uniformly distributed over $ M = [0, k)$ (i.e., the pdf of $ Y$ is $ f(y)=\frac{1}{k}$ ) but I can’t quite sort the math to get me there.