Spring security introspection not working

Question: I'm trying to implement opaque token validation for my resource server, which runs on Spring Boot 2.4.5 with the dependencies spring-boot-starter-oauth2-resource-server and oauth2-oidc-sdk, and I am struggling to make the call to the introspection endpoint work.
The authorization server is WSO2 Identity Server.
Here's my code:
@Configuration
@EnableWebSecurity
public class OAuth2ResourceServerConfiguration extends WebSecurityConfigurerAdapter {

    @Value("${spring.security.oauth2.resourceserver.opaquetoken.introspection-uri}")
    String introspectionUri;

    @Value("${spring.security.oauth2.resourceserver.opaquetoken.client-id}")
    String clientId;

    @Value("${spring.security.oauth2.resourceserver.opaquetoken.client-secret}")
    String clientSecret;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http
            .authorizeRequests(authorize -> authorize
                .anyRequest().authenticated()
            )
            .oauth2ResourceServer(oauth2 -> oauth2
                    .opaqueToken(opaque -> opaque
                        .introspectionUri(this.introspectionUri)
                        .introspectionClientCredentials(this.clientId, this.clientSecret)
                    )
            );
    }

}

In the application.properties I've got:
spring.security.oauth2.resourceserver.opaquetoken.introspection-uri=https://localhost:9443/oauth2/introspect
spring.security.oauth2.resourceserver.opaquetoken.client-id=admin
spring.security.oauth2.resourceserver.opaquetoken.client-secret=admin

The backend replies to all requests with 401, and it doesn't seem to even contact the authorization server. I read the documentation and watched plenty of examples, but can't figure out what's wrong. Could you help me please?
I also would like to know if the use of the introspection endpoint is correct and if it's the only way a resource server has to validate a non-JWT token.
Edit: @jzheaux: I was using DEBUG logging, now with TRACE I get this exception:
org.springframework.security.authentication.AuthenticationServiceException: I/O error on POST request for "https://localhost:9443/oauth2/introspect": PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
    at org.springframework.security.oauth2.server.resource.authentication.OpaqueTokenAuthenticationProvider.getOAuth2AuthenticatedPrincipal(OpaqueTokenAuthenticationProvider.java:110)
    at org.springframework.security.oauth2.server.resource.authentication.OpaqueTokenAuthenticationProvider.authenticate(OpaqueTokenAuthenticationProvider.java:94)
    at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:182)
    at org.springframework.security.oauth2.server.resource.web.BearerTokenAuthenticationFilter.doFilterInternal(BearerTokenAuthenticationFilter.java:122)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:103)
    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:89)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:117)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.header.HeaderWriterFilter.doHeadersAfter(HeaderWriterFilter.java:90)
    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:75)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:110)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:80)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:55)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:336)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:211)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:183)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:358)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:271)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:100)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:93)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:201)
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:119)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
    at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
    at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
    at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
    at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
    at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    at java.base/java.lang.Thread.run(Thread.java:834)

It seems like a certification problem, but with Postman WSO2's introspection endpoint works flawlessly.
These are the response headers as shown by Postman (body is empty):
Comment: Please include the full 401 response, including headers. Please use TRACE logging for org.springframework.security and paste the logs emitted for the failing request.
Comment: seems like related to ssl/cert, does it work if you try to run `http://localhost:9443/oauth2/introspect` as http only instead? like `http://localhost:8080/oauth2/introspect`
Answer: The problem is with the WSO2 Identity Server's certificate. You need to import the it into java's cacerts file (inside lib/security).
Comment: Thank you, I tried importing into the cacerts file a certificate called "wso2carbon", which was in the wso2carbon keystore, but it didn't work. Then I finally found out that I had to create a public certificate from the whole keystore, as explained here: https://nadeesha678.wordpress.com/2015/09/21/adding-wso2-public-certificate-to-java-certificate-store/. Now it works.


Conditional print node in tensorflow

Question: I am looking for a way to have a conditional print node in a tensorflow, using sample line of code below, in which every 10 loop count, it should print something in the console. But it does not work for me. Can anyone advise?
Thanks,
Hamidreza,
epsilon = tf.cond(tf.constant(counter % 10 == 0, dtype=tf.bool), true_fn=lambda:tf.Print(epsilon, [counter, epsilon], 'batch: ', summarize=10), false_fn=lambda:epsilon)

Answer: I encountered a similar issue and solved it with the following ugly solution.
I am using tf.print instead of 'tf.Print' since it is decrypted:
Since true_func and false_func should return the same type and shape, so I return a meaningless const.
def true_func():
    printfunc = tf.print(TENSOR_TO_PRINT,summarize=-1)
    with tf.control_dependencies([printfunc]):
        return tf.constant(1)

def false_func():
    return tf.constant(1)

shouldprint = tf.cond(YOUR_CONDITION ,true_fn = true_func ,false_fn=false_func)

Then you can run the shouldprint op or add it as a dependency of other ops.
Comment: I can't recall the details of this solution, but the "shouldprint" variable should be a dependency of some other ops, or it won't be executed.

Conditional print node in tensorflow


Finding $x\in k^3$ such that no component of $Ax$ is zero

Question:
Let $k$ be a field and let $n\geq 1$. Let $A\in \operatorname{Mat}(n\times 3,k)$ be a matrix such that no row of $A$ is zero. I would like to find $x,y,z\in k$ such that no component of $A(x,y,z)^{t}$ is zero.

I think proceeding by induction should work. For $n=1$ this is not a problem. But how do I go from $n-1$ to $n$? Suppose we are given $x,y,z\in k$ such that $xa_i+yb_i+zc_i=0$ for $i=1,\ldots,n-1$, where $(a_i,b_i,c_i)$ is the $i$th row of $A$. I have to adjust $x,y,z$ in such a way that we also have $xa_n+yb_n+zc_n=0$.
$\textbf{Edit}$ As Robert posted below, this is false in general. What about if $k$ is algebraically closed?
Answer: Counterexample: over the field $\mathbb Z/2 \mathbb Z$ let $$A = \pmatrix{1 & 0 & 1\cr  0 & 1 & 1 \cr 1 & 1 & 0}$$
EDIT: If $k$ is algebraically closed, in particular it has infinite degree over the field $k_A$ generated by the entries of $A$. Letting $x$, $y$, $z$ be members of $k$
linearly independent over $k_A$, each entry of $A \pmatrix{x\cr y\cr z}$ is
a linear combination of $x, y, z$ over $k_A$ with coefficients not all $0$,
and therefore is not $0$.
Comment: Thanks, I am mainly interested in $k$ being algebraically closed, should have said that. Have you got any idea on how to proceed in this case?


Sshfs is not working

Question: When I run
sshpass -p 'mypass' sshfs 'root'@'188.8.131.52':/ '/dir' -o StrictHostKeyChecking=no,debug

It successfully mounts but it runs on foreground.
When I run without 'debug' parameter, it doesn't mount at all.
Server is ubuntu 8.04
Any ideas why?
UPDATE: When I run the command as ROOT it does mount. It doesn't work with other users.
here is the output of an unsuccessful mount
$ sshpass -p 'pass' sshfs  'root'@'126.96.36.199':/ '/s6' -o StrictHostKeyChecking=no,sshfs_debug,loglevel=debug
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 184.108.40.206 [18.104.22.168] port 22.
debug1: Connection established.
debug1: identity file /var/www/vhosts/devrim.kodingen.com/.ssh/id_rsa type -1
debug1: identity file /var/www/vhosts/devrim.kodingen.com/.ssh/id_dsa type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.1p1 Debian-5
debug1: match: OpenSSH_5.1p1 Debian-5 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_4.7p1 Debian-8ubuntu1.2
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-cbc hmac-md5 none
debug1: kex: client->server aes128-cbc hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
Warning: Permanently added '126.96.36.199' (RSA) to the list of known hosts.
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Trying private key: /var/www/vhosts/devrim.kodingen.com/.ssh/id_rsa
debug1: Trying private key: /var/www/vhosts/devrim.kodingen.com/.ssh/id_dsa
debug1: Next authentication method: password
debug1: Authentication succeeded (password).
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LANG = en_GB.UTF-8
debug1: Sending subsystem: sftp
Server version: 3
debug1: channel 0: free: client-session, nchannels 1
debug1: fd 0 clearing O_NONBLOCK
debug1: Killed by signal 1.

Answer: I've found the same problem: when combining sshfs with sshpass, it only works if sshfs is running in foreground (either with -f or -d). If it's executed in background, it executes normally with exit value 0, but it won't mount anything.
The workaround I've found is executing sshfs in foreground mode, but sending it to background with the shell:
sshpass -f passwordfile sshfs -f origin destination &

This way it will work. Then, when you don't need the mount anymore and unmount it with fusermount -u, the sshfs process will end normally.
Comment: Works, thanks! I just substituted the background task's "&" with running the process `sshfs -f origin destination` in the foreground inside a [screen](https://www.gnu.org/software/screen/manual/screen.html) session, so I can input the ssh password manually before sending the execution/session in the background.
Answer: You need access to the fuse device and that seems to be require root access.
Comment: Or being in the `fuse` group, unless your distro messed up that.
Comment: my user is in fuse group - uid=10816(devrim) gid=2523(psacln) groups=2523(psacln),107(fuse) - still no luck :S
Answer: we couldn't find a solution to this. we are running it on foreground.
Answer: My problem was similar and it had to do with the fuse configuration file in /etc/fuse.conf.
I had to uncomment user_allow_other and the mount it without sudo:
sshfs -o allow_other email@example.com:/foo/bar /home/uesr/foor/bar/baz

Sshfs is not working


como arrumar o resultado da tabuada para ficar no meio da pagina

Question: como faço para o resultado da tabuada ficar no meio da pagina e não no canto, alguém pode me ajudar?

Tabuada MARVEL

    <button class="botao__mostrar" onclick="mostrarTabuada()">Mostrar Tabuada</button>
    <button class="botao__limpar" onclick="limparTabuada()">Limpar Tabuada</button>

Comment: No CSS faça `.tabuada{display: grid; place-items: center;}` Referência: https://developer.mozilla.org/pt-BR/docs/Web/CSS/CSS_grid_layout/Basic_concepts_of_grid_layout
Comment: Obrigadaa ,deu certo <3
Answer: No arquivo css você pode tentar:
.tabuada{
   width: 100%;
   display:grid;
   place-items: center;
}

em casos de centralização e posicionamento, o mais facil e simples é usar ou grid ou flex, segue abaixo dois links de tutorias respectivamente
https://css-tricks.com/snippets/css/complete-guide-grid/
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
Comment: Obrigadaa ,deu certo <3


Elliptic Points of Modular Group in Upper Half Plane

Question: This is a very small question.
Let $\mathbb{\Gamma} = \mathrm{SL_2}(\mathbb{Z})$ be the modular group, $\mathcal{F} = \{z \in \mathbb{C} ;\; \lvert z \rvert \geq 1,\; \lvert \Re (z) \rvert  \leq 1/2\}$ its fundamental domain.
I (probably) don't understand the following argument made by Toshitsune Miyake in "Modular Forms", in the proof of Theorem 4.1.3, page 98.

Since $\mathbb{\Gamma}$ contains $\tau = \left[\begin{smallmatrix}1 & 1 \\ 0 & 1\end{smallmatrix}\right]$, and $\omega = \left[\begin{smallmatrix}0 & -1 \\ 1 & 0\end{smallmatrix}\right]$, the boundary points of $\mathcal{F}$, other than $i$, [$\zeta_6 = e^{2\pi i/6}$, $\zeta_6^2$], are [...] ordinary points.

This is what I thought: Let $p$ be an elliptic point on the boundary of $\mathcal{F}$, stabilized by $\gamma \in \mathbb{\Gamma}$.
Suppose $p$ isn't $\zeta_6$ nor $\zeta_6^2$. Since $\gamma \mathcal{F} \cap \mathcal{F} \neq \emptyset$, $\gamma$ must now be either $\tau$, $\tau^{-1}$ or $\omega$, but it can't be $\tau$ nor its inverse, for it has to be elliptic.
So the only other possible elliptic points have to be stabilized by $\omega$, which leaves one with $i$.
But now I don't know how to prove the step $\gamma \mathcal{F} \cap \mathcal{F} \neq \emptyset\; \Rightarrow\; \gamma = \tau, \tau^{-1}, \omega$, which only is visually clear to me. I feel that the original, intended argument is easier than this and I'm being blind.
Comment: @JyrkiLahtonen ok, took into consideration
Answer: A bit more checking is needed here than the argument you sketch: there are more elements than just $\tau, \tau^{-1}$ and $\omega$ such that $\gamma D \cap D \ne \varnothing$ -- in fact the set $\{ \gamma \in \overline{\Gamma} : \gamma D \cap D \ne \varnothing\}$ has 10 elements if I remember correctly. Here $\overline{\Gamma} = \Gamma / \{\pm 1\} = PSL_2(\mathbb{Z})$. However, you can check that none of these ten elements (except the identity) fix any point other than $i, e^{\pi i/3}, e^{2\pi i / 3}$. This argument is worked out very carefully in the last chapter of Serre's book "A Course in Arithmetic".
As for what Miyake had in mind at that point in his book, I'm not sure!
Comment: Ok, thanks for that. Though I think, if $\gamma$ doesn't stabilize as $\zeta_6$ or $\zeta_6^2$ as assumed, the only possibilities are indeed $\tau$, $\tau^{-1}$ and $\omega$ as suggested by [this picture](https://en.wikipedia.org/wiki/File:ModularGroup-FundamentalDomain-01.png).
Answer: Although this is an old question, I fell into the same problem, and will explain what I think was Miyake's original line of thought with this.
Looking back at the proof of Theorem 1.9.1, there was no point in considering $z$ a vertex. Rather, we can pick it on any point of the border of $F$ where we can think of it as being a vertex, after cutting the line containing it at it.
But then, we would have, by the very same proof (Unless he used the fact that it's a vertex and I didn't see it.), that $\omega_1+\ldots+\omega_v=\frac{2\pi}{e}$.
Now, since $\tau$ and $\omega$ are in $\Gamma$, then for any point $z$ on $\partial F$ except $\zeta,\zeta',i$, there is a different point on $\partial F$($\tau z$ if it is on the straight lines, $\omega z$ if it is on the circular arc) equivalent to it. But then, for all these points, we have $\omega_v=\pi$, and thus, $e=\frac{2\pi}{\omega_1+\ldots+\omega_v}\leq\frac{2\pi}{\pi+\pi}=1$, and therefore, these points are not elliptic.
The difference is that $i$ is taken by $S$ to itself, and that the interior angles at $\zeta$ and $\zeta'$ are less than $\pi$, which is what makes these points special.
The more reason to think my argument is correct is because he uses the formula for $i$, which is not a vertex as he defined it before, but I think it doesn't matter as he doesn't use that back in the proof.
Comment: Unfortunately, I don't have Miyake's book at hand anymore to check what you claim.


Statistical model & hypothesis test

Question: I found the following question in one of the older exam papers,

Protein expression of a certain marker is measured in 2 patients
groups of equal size (n=20) and at 2 different time visits. Assuming
an approximately Gaussian distribution of the marker expression and
homoscedasticity of noise, what would be an appropriate approach to
test the significance of the marker expression difference between both
patient groups while correcting for the effect of the visit? Note that
one ONE p-value should be computed here, not two.

So once I read this I thought that the model might be an unpaired 2 sample t-distribution model where the difference can be taken between the 2 visits for each group and then welch test can be carried out between the groups to find the significance.
But the question goes on to mention,

Hint: First define a statistical model of the form LHS~RHS, where LHS
denotes the expression level of the protein marker and RHS further
variables that should model this expression. In a second step identify
an appropriate statistical hypothesis test for assessing the
significance of the patient group.

Which then made me think it points towards linear regression model where maybe the significance could be calculated by anova.
But I asked my professor about the same question, and he told me:

For this particular question one has to consider two different
statistical effects:

a time effect
a patient group effect

Hence, a statistical model (more specifically a linear model) should look like:
expression ~ group + time
One can then test the hypothesis H0: beta_group = 0 (i.e. no group
effect) via a one sample t-test.

But I do not understand how a one sample t-test can be carried out here, if anyone can help me understand this, it would be highly appreciated.
Comment: Remember the t-stats on regression coefficients?
Comment: @Dave Yes, I do, you try to check if the predictor variable have a significant influence on the fitted value, but I am sorry, I do not think that I ever understood why it is calculated using a one sample either, I thought it should be paired t-test (yes, I know that you calculate one sample in paired t test as well, but you should 1st calculate the difference) as the 2 samples there are not independent
Comment: It's one sample because you're testing if $\beta=0$ versus not, just like when you test $\mu=0$ versus not.
Comment: @Dave Oh ok got it, but how does the same apply here, aren't the 2 groups independent samples?
Comment: What are your two groups? I count four groups: before 1, after 1, before 2, and after 2.
Answer: The question authors were clearly trying to get you to compare pre-post differences across groups. In that framework where mu was the mean of the group differences, the hypothesis would be that mu[A] = mu[B] and you would do a t-test on the cross-group differences, probably using a pooled estimate of the standard deviations. The regression would be LHS = paired, pre-post differences and the RHS would be intercept (beta[0]) plus group membership (beta[1]).  Depending on the coding used (which might vary across analysis platforms) the intercept would be  the first group trend (or the shared trend) and the beta[1] would be the difference from the first (or from the shared).
Your professor on the other hand was steering you in the directions of a repeated measures AnOVa. In this simple situation the two approaches should yield the same answer. The repeated measures AnOVa would yield an estimate of the overall change in values, and if an interactions were included in the  model would let you test for differences across groups in the amount of change. The test of no difference in change of mean values between groups would not be equivalent to a one-sample t-test, but rather equivalent to a two-sample t-test.

Statistical model & hypothesis test


Android Volley Error

Question: I get Android volley error suddenly when i run my program.here i paste my AppController class for handling the volley objects.
AppController.java
public class AppController extends Application {
public static final String TAG = AppController.class.getSimpleName();

private RequestQueue mRequestQueue;

private static AppController mInstance;

@Override
public void onCreate() {
    super.onCreate();
    mInstance = this;
}

public static synchronized AppController getInstance() {
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        mRequestQueue = Volley.newRequestQueue(getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req, String tag) {
    req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
    getRequestQueue().add(req);
}

public <T> void addToRequestQueue(Request<T> req) {
    req.setTag(TAG);
    getRequestQueue().add(req);
}

public void cancelPendingRequests(Object tag) {
    if (mRequestQueue != null) {
        mRequestQueue.cancelAll(tag);
    }
}
}

and i call this class in some another class of my program like

AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

it will executed without any errors in so many days but now it shows error
like
Error:
java.lang.NoSuchMethodError: No virtual method setTag(Ljava/lang/Object;)Lcom/android/volley/Request; in class Lcom/android/volley/Request or its super classes (declaration of 'com.android.volley.Request'; appears in /data/app/com.example.rninja4.rookie-1/split_lib_dependencies_apk.apk:classes6.dex)
                                                                          at com.example.packagename.App.AppController.addToRequestQueue(AppController.java:39)

Comment: attach  your gradle app file and manifiest file with question
Answer: You have not declared application class in manifest like below.
<application
    android:name="AppController"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

Comment: did you try with full qualified name of AppController class.
Comment: yes . i tried still its not working and the app is running without this error in so many days ,i get this error suddenly.
Comment: Here Volley gradle suggested. please change volley gradle and try .https://stackoverflow.com/questions/44370743/java-lang-nosuchmethoderror-no-virtual-method-settagljava-lang-object
Comment: Next i want to know that if there is any sudden changes in library how can i solve that.

Android Volley Error


C# CSV parsing escaping double quotes

Question: I am trying to parse a number of CSV files that has double quotes and commas within the fields. I have no control over the format of the CSVs and instead of using "" to escape the quotes it is using \". The files are also extremely large so reading and using regex isn't the best option for me.
I would prefer to use an existing library and notewrite an entirely new parser. Currently I am using CSVHelper
This is an example of the CSV data:
"id","name","notes"
"40","Continue","If the message \"Continue\" does not appear restart, and notify your instructor."
"41","Restart","If the message \"Restart\" does not appear after 10 seconds, restart manually."
The problem is the double quotes aren't being escaped properly and the , is being read as a delimiter and separating the notes field into 2 separate fields.
This is my current code that is doesn't work.
DataTable csvData = new DataTable();
string csvFilePath = @"C:\Users\" + csvFileName + ".csv";

try
{
    FileInfo file = new FileInfo(csvFilePath);
    using (TextReader reader = file.OpenText())
    using (CsvReader csv = new CsvReader(reader))
    {
        csv.Configuration.Delimiter = ",";
        csv.Configuration.HasHeaderRecord = true;
        csv.Configuration.IgnoreQuotes = false;
        csv.Configuration.TrimFields = true;
        csv.Configuration.WillThrowOnMissingField = false;
        string[] colFields = null;
        while(csv.Read())
        {
            if (colFields == null)
            {
                colFields = csv.FieldHeaders;
                foreach (string column in colFields)
                {
                    DataColumn datacolumn = new DataColumn(column);
                    datacolumn.AllowDBNull = true;
                    csvData.Columns.Add(datacolumn);
                }
            }
            string[] fieldData = csv.CurrentRecord;

            for (int i = 0; i < fieldData.Length; i++)
            {
                if (fieldData[i] == "")
                {
                    fieldData[i] = null;
                }
            }
            csvData.Rows.Add(fieldData);
        }
    }
}

Is there an existing library that lets you specify how to escape quotes or should I just write my own parser?
Comment: http://www.codeproject.com/Articles/9258/A-Fast-CSV-Reader
Comment: I should have specified I am using .Net 4.5, and that targets .Net 2.0
Comment: so download source and re-compile!!
Comment: @DourHighArch: that one is already closed.
Comment: Out of curiosity... you said huge files... how big are they exactly, because if they're big as in 100mb+ big, then you're in for a whole host of fun....
Comment: There are 8 files that range from 2kb to 300mb
Comment: It might interest you and others having this problem, that CsvHelper offers a workaround via `CsvMode` like this: `csvConfig.mode = CsvMode.Escape` [source](https://github.com/JoshClose/CsvHelper/issues/835#issuecomment-763000666). I would like to make this an answer but it seems like I can't because of the "duplicate answer" flag
Answer: You can get quite far when using a very simple linq statement to split and trim and finally Replace for unescaping quotes in the content:
DataTable csvData = new DataTable();
string csvFilePath = @"C:\Users\" + csvFileName + ".csv";
try
{
    string[] seps = { "\",", ",\"" };
    char[] quotes = { '\"', ' ' };
    string[] colFields = null;
    foreach (var line in File.ReadLines(csvFilePath))
    {
        var fields = line
            .Split(seps, StringSplitOptions.None)
            .Select(s => s.Trim(quotes).Replace("\\\"", "\""))
            .ToArray();

        if (colFields == null)
        {
            colFields = fields;
            foreach (string column in colFields)
            {
                DataColumn datacolumn = new DataColumn(column);
                datacolumn.AllowDBNull = true;
                csvData.Columns.Add(datacolumn);
            }
        }
        else
        {
            for (int i = 0; i < fields.Length; i++)
            {
                if (fields[i] == "")
                {
                    fields[i] = null;
                }
            }
            csvData.Rows.Add(fields);
        }
    }
}

When used in a very simple console app, and OPs original input in the "test.txt" file:
public static void CsvUnescapeSplit()
{
    string[] seps = { "\",", ",\"" };
    char[] quotes = { '\"', ' ' };
    foreach (var line in File.ReadLines(@"c:\temp\test.txt"))
    {
        var fields = line
            .Split(seps, StringSplitOptions.None)
            .Select(s => s.Trim(quotes).Replace("\\\"", "\""))
            .ToArray();
        foreach (var field in fields)
            Console.Write("{0} | ", field);
        Console.WriteLine();
    }
}

This produces the following (correct) output:
id | name | notes |
40 | Continue | If the message "Continue" does not appear restart, and notify your instructor. |
41 | Help | If the message "Restart" does not appear after 10 seconds, manually restart. |

Caveat: If your field separators have spaces, like these:
"40" , "Continue" , "If the message \"Continue\" does not appear restart, and notify your instructor."

Or your content strings contain commas directly after a quote, like here (after "Restart"):
"41","Help","If the message \"Restart\", does not appear after 10 seconds, manually restart."

It will fail.
Comment: What is colFields? What is csvData?
Comment: @aquinas `colFields` was forgotten in the copy & paste edit from OPs code. Added that back in now, `csvData` appears to be a variable used but not declared in OPs code snippet.
Comment: The code fails when you try to run it with the OPs original data set.
Comment: It works when I use string[] seps = { "\",\"" }; and .Select(s => s.Trim(quotes).Replace("\"\"", "'")) Thank you!

C# CSV parsing escaping double quotes


Rounding output of sigmoid for binary linear classifier

Question: I am working on a linear classifier with expected output to be 1 for class A belonging and 0 for class B belonging.
The output, in some occasions is

nearly 0 (0.000198752053929624), or
nearly 1 (0.999740100963010).

I've decided to round the numbers, and accuracy is 100%.
My question is: is this an acceptable procedure or there's some underlying problem that results in these outputs instead of clear 0s and 1s?
This problem happens for learning rates smaller than 0.1 on a gradient descent.
Comment: Closely related: https://stats.stackexchange.com/questions/312780/why-is-accuracy-not-the-best-measure-for-assessing-classification-models
Comment: @Sycorax's link is quite important.
Answer: When you expect $0$ or $1$ as result, then there are two approaches: 1. rounding ($> 0.5$ is class 1, $\leq 0.5$ is class 0), 2. looking at the receiver operating characteristic curve (ROC) or the precision-recall (PR) curve to find a threshold. The second approach is better because it is possible to find the best threshold for the dataset at hand.
When you want the output probabilities to indicate confidence, then you should look at metrics such as expected calibration error or calibration curves. Then ideally low probabilities would mean high confidence for class 0, and high probabilities would mean high confidence for class 1.
As was noted in the comments, accuracy is not the best metric for assessing how good a classifier is. I would compute precision to be sure that the results are good. Your classifier is maybe overfitting.
You should also keep in mind that the output of a neural network (with sigmoid activation) is a probability between 0 and 1 and not a binary value 0/1.
Comment: Better yet, look at log loss or Brier score! Both. Those are  strictly proper scoring rules.


Tolerance for pseudoinverse

Question: In calculating the pseudoinverse of a matrix $A$, of size (m,n), I need to choose a tolerance threshold for the eigenvalues. I'm trying to understand how I should pick this. Matlab default is to use the $norm(A)*max(m,n)*eps$, where the norm is norm-2 and the eps is the machine epsilon. Any ideas as to why this is the default value?
Moreover, there are cases in which $ A $ is only interesting in comparison to other values. For example, I'd like to calculate the eigenvalues of $C + BB^TA^{-0.5}$ where A,B,C are matrices, but B can have a very large second dimension. In this example, how should we choose the tolerance?
Answer: I suspect that MATLAB does that because the machine epsilon is either the smallest value it can represent accurately or because it's the smallest value the it considers useful or worth bothering with.
In theory the pseudoinverse of A is a matrix of size (n,m) and that's that. If some of the eigen values are zero, that's fine but the psuedoinverse has to be multiplicable by A to fit its definition. In practice if some of the eigenvalues of it are 0, then whatever practical use you have for the psuedo-inverse can be accomplished with a smaller matrix, which is ideal computationally. And in many applications one of the goals is to reduce dimension, so specifically chopping off as many low eigenvals as possible is ideal. As to what the cutoff should be, that depends on what you consider significant. As a rule of thumb though, anything that's less than 0.01% of your largest eigenvalue is probably worth thinking about discarding (that's not hard and fast, but off the top of my head it's a reasonable place to start). Usually you can look at the dropoff in eigenvalues and figure a cutoff subjectively for when they start becoming insignificant.

Tolerance for pseudoinverse


two slightly different header files in c++ used (one .lib file though) causing lots of errors but no warning generated. how to prevent it?

Question: I had a library that was compiled with one set of header files and I copied the header files to a folder where I put all my header files as it is in MSVC include search path.
But the compiled .lib was with different set of headers and my program using that header file was using original (unchanged) set of header files.
I noticed problem as debugger was showing wrong values. For example I was setting
<code>mystruct->item1 = 1
</code>
but after this line debugger was showing mystruct->item1 = 0. Although printing it was fine.
But I then noticed passing this structure to library functions have different values.
Which made me realize there were different structures.
As size of structures are different, I think compilers should understand this and generate warnings. (which I didnt get any).
I spent several days to debug this and I was wondering for similar cases how can I prevent it? For example if there is a compiler switch that helps generating warnings for cases like this. Or how can I prevent nasty errors like this?
Answer:
As size of structures are different, I think compilers should understand this and generate warnings.

The problem is that by the time you are connecting your code with the code of the library the compiler is out of the picture. The program that makes the connection (the linker) has no idea that the two binary codes that it tries to connect use structures of different size - in fact, linker has no idea that the programs used any data structures at all: it operates on a different level.
That is why it is not possible for the tools in the C++ toolchain to produce a reliable warning for you. This does not mean, however, that you cannot prevent it yourself: all it takes is embedding some sort of a build number into the library and its header, and comparing the two at the time when you initialize the library.


How to capture an image and save from webcam using grails?

Question: Trying to capture an image from webcam and wanted save on a drive
Using Grails 2.3.7
script code
var video = document.querySelector("#videoElement");
var imageW;
                //check for getUserMedia support
                navigator.getUserMedia = navigator.getUserMedia
                        || navigator.webkitGetUserMedia
                        || navigator.mozGetUserMedia
                        || navigator.msGetUserMedia || navigator.oGetUserMedia;

                if (navigator.getUserMedia) {
                    // get webcam feed if available
                    navigator.getUserMedia({
                        video : true
                    }, handleVideo, videoError);
                }

                function handleVideo(stream) {

                    video.src = window.URL.createObjectURL(stream);
                }

                function videoError(e) {

                }
                var v, canvas, context, w, h;
                var imgtag = document.getElementById('imgtag');
                var sel = document.getElementById('"avatar"');

                document.addEventListener('DOMContentLoaded', function() {

                    v = document.getElementById('videoElement');
                    canvas = document.getElementById('canvas');
                    context = canvas.getContext('2d');
                    w = canvas.width;
                    h = canvas.height;

                }, false);

                function draw(v, c, w, h) {

                    if (v.paused || v.ended)
                        return false; // if no video, exit here

                    context.drawImage(v, 0, 0, w, h); // draw video feed to canvas

                    var uri = canvas.toDataURL("image/png"); // convert canvas to data URI

                      imageW = canvas.toDataURL("image/png");
        imageW = imageW.replace('data:image/png;base64,', '');

                    imgtag.src = uri;
                }

                document.getElementById('save').addEventListener('click',
                        function(e) {

                            draw(v, context, w, h);

                        });

                var fr;

                sel.addEventListener('change', function(e) {
                    var f = sel.files[0];

                    fr = new FileReader();
                    fr.onload = receivedData;

                    fr.readAsDataURL(f);
                })

                function receivedData() {

                    imgtag.src = fr.result;
                }
                function webImageSubmit() {

                    alert(imageW);

                    var pars = "id=" + $('#id').val() + "&imageW=" + imageW;
                    $.ajax({
                        type : 'POST',
                        url : '/controller_name/saveWebCamImage',
                        data : pars,
                        error : function(request, status, error) {

                            document.getElementById('load').style.visibility = "hidden";
                        },
                        beforeSend : function() {
                            document.getElementById('load').style.visibility = "visible";
                        },
                        success : function(data) {
                            location.reload();
                        }
                    });
                }

Controller Side :
def encodedData = javax.xml.bind.DatatypeConverter.parseBase64Binary(params.imageW.toString());
    def defaultPath = "/images/userImages";
        def webRootDir = servletContext.getRealPath("/");
        def systemDir = new File(webRootDir, defaultPath);
        if (!systemDir.exists()) {
            systemDir.mkdirs();
        }

        String file_name = "webImage.jpg";
        def csvFileDir = new File( systemDir, file_name);
        new File(systemDir, file_name).withOutputStream {
            it.write(encodedData);
          };

But image is not saving on drive.
Please Help me..
Thank you..
Comment: Why are you creating the `csvFileDir` object? I don't see it being used. What is the full path in variable `systemDir`? Have you confirmed `encodedData` contains data? Does the operating system user account which is running the webapp have write access to `systemDir`?
Comment: for saving an image on local drive.
Comment: Ok... what about the remaining three questions?
Answer: Code for preview captured image
<!doctype html>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>WebcamJS Test Page</title>

Your captured image will appear here...

WebcamJS Test Page

Demonstrates 320x240 capture & display with preview mode

    <form>

            <input type=button value="Take Snapshot" onClick="preview_snapshot()">

            <input type=button value="< Take Another" onClick="cancel_preview()">
            <input type=button value="Save Photo >" onClick="save_photo()" style="font-weight:bold;">

    </form>

Answer: Complete code to capture and download
<!DOCTYPE html>

<button disabled>
  take a snapshot</button>
<video></video>

Answer: Complete code to record and download
<!DOCTYPE html>

<button disabled>start recording</button>
<video></video>


what condition of A makes transpose(A)*A nonsingular?

Question: What contidion of A makes $$A^TA$$ nonsingular?
If so, that is $$A^TA$$ is non-singular than a unique solution exists.
Answer: Let $A$ be a $m \times n$ matrix. The rank of $A^T A$ turns out to be the same as the rank of $A$, and $A^T A$ is a $n \times n$ matrix. So $A^T A$ is nonsingular if and only if the rank of $A$ is $n$, i.e. if $A$ has linearly independent columns.
Answer: Here is a conceptual approach. Note that
$$\langle Au, Av \rangle = \langle A^T A u, v \rangle$$
where, say, $u, v \in \mathbb{R}^n$ and $Au, Av \in \mathbb{R}^m$. Then $A^T A$ is nonsingular iff the bilinear form on the RHS, and hence the bilinear form on the LHS, is nondegenerate, which is true iff $A$ has full rank, or equivalently is injective as a linear map $\mathbb{R}^n \to \mathbb{R}^m$.


Linux-like Ctrl-C (KeyboardInterrupt) for the Windows cmd line?

Question: I've been forced into using a command line in windows and wondered if there were Linux-like keyboard shortcuts? I googled and didn't find what I was looking for.
Things like ^C, ^Z and such?
Answer: Try Ctrl+Break: some programs respond to it instead of Ctrl+C. On some keyboards Ctrl+Break translates to Ctrl+Fn+Pause.
Note also that nothing can cancel synchronous network I/O (such as net view \\invalid) on Windows before Vista.
Answer: You can trap ^C on Windows with SIGINT, just like Linux. The Windows shell, such as it is, doesn't support Unix style job control (at least not in a way analogous to Unix shells), and ^Z is actually the ^D analog for Windows.
Comment: I don't know about Unix style job control, but now in Windows you have [Powershell Background Jobs](https://msdn.microsoft.com/en-us/library/dd878288(v=vs.85).aspx).
Answer: There are two keyboard combinations that can be used to stop process in Windows command line.

Ctrl+C is the "nicer" method. Programmers can handle this in software. It's possible to write programs that ignore Ctrl+C as SIGINT signal completely, or handle Ctrl+C like a regular keyboard combination.
Ctrl+break is the "harder" method, always sends SIGBREAK signal and cannot be overridden in software.
Answer: Ctrl-C does a similar thing in windows as it does in linux.
Comment: In Linux Ctrl-C can halt many stuck processes, but it seems that my stuck windows command is Ctrl-C proof! Thanks though!
Comment: This is simply not true
Comment: @AKA which bit is the "not true" bit?


Two exercises about hypermetric spaces

Question:
Take $S$ to be the collection of all subsets of $\{1,\dots,n\}$. If $x, y$ are in $S$, define $d(x,y)$ as the number of elements of the symmetric difference $x\triangle y$.
Exercise 2.1. Show that $(S,d)$ is a metric space. Moreover, $S$ satisfies the following stronger version of the triangle inequality: if $x_1,\dots,x_k$ are elements of $S$ and $b_1,\dots,b_k$ are integers that add up to $1$, then $$\sum_{i,j=1}^k b_ib_jd(x_i,x_j)\le0$$
Exercise 2.2. Define the distance between two points $(x,y,z)$ and $(x',y',z')$ in a three-dimensional space as the biggest of the numbers $|x-x'|$, $|y-y'|$, $|z-z'|$. Show that this distance is not hypermetric.

(Transcribed from this image)
These two exercises are about hypermetric. For the first one, I believe that induction for k should work. But I don't know what's special about the metric, and cannot prove the inequality for $k=4$. As for the second exercise, it seems that the inequality might fail when $k=4$, but I have no idea how to find those four points. Any help is appreciated, thank you!
Answer: For the first exercise: write the metric $d$ as the sum of semi-metrics
$$d_r(A,B)=\begin{cases}1,\quad \text{ if }r\in A\triangle B \\ 0 \quad\text{otherwise}\end{cases}$$
For each $r$, we have
$$\sum_{i,j} b_ib_jd_r(A_i,A_j)
= \sum_{r\in A_i,\ r\notin A_j} b_ib_j+ \sum_{r\notin A_i,\ r\in A_j} b_ib_j \\= 2 \sum_{r\in A_i}b_i \sum_{r\notin A_j} b_j = 2\sum_{r\in A_i}b_i\left(1-\sum_{r\in A_i}b_i\right)\le 0
$$
because $N(1-N)\le 0$ for every integer $N$. Then sum over $r$.

For the second one, I don't have a conceptual explanation, but a computer search brought up this example: take five points with coordinates given by the columns of the matrix
$$ \begin{pmatrix}    1& 1& 1& 2& 0\\    0& 2& 2& 1& 1\\    0& 1& 0& 1& 0\\    \end{pmatrix}   $$
and check that the sum in the hypermetric inequality is $1>0$.
I don't have a counterexample with $4$ points; it's been more than a year since I looked for them, but I believe my search did not turn up anything.

For more on hypermetrics, I can offer my blog posts Pentagrams and hypermetrics and Polygonal inequalities: beyond the triangle.
Comment: Thanks for your elegant solution, and the blog posts are more than helpful! Here is another question about projective metric and I am looking for your help. http://math.stackexchange.com/questions/1300446/metric-invariant-under-translations-is-projective

Two exercises about hypermetric spaces


How to know all the permission I have in a GCP I have?

Question: I have been added and granted viewer access to project however almost every page I open shows that I don't have permission to view this particular GCP service of that particular project. How can I know all the permission I have for a certain project?
Comment: Menu > IAM & Admin > IAM
Comment: Roles about bound to a resource with an identity as part of a policy. Therefore you need the correct roles to read the resource policy. There are many resources in GCP (project, storage, KMS, Functions, Run, etc.) Based on your question, you do not have those permissions. The command to read a Project's IAM policy is **gcloud projects get-iam-policy**. If that command fails, then you will not be able to determine which resources you have been granted roles (permissions) to access.
Answer: Use asset inventory search IAM policies
<code>gcloud asset search-all-iam-policies \
  --scope='projects/<PROJECT_ID>' \
  --query='policy:"<YOUR_EMAIL>"'
</code>


javascript validation not activating possibly due to php mailer on HTML form

Question: I have a problem with my Javascript validation not activating. I also have a PHP mailer on this HTML form and as soon as submit is pressed it submits the form and no validation is done.
Here is the code for the HTML form:
 <form name="contact" action="mailer.php" onsubmit="return ValidateForm()" method="post"><fieldset>

Name:
<input type="text" name="name" id="name" placeholder="Enter your full name" />

Email:
<input type="email" name="email" id="email" placeholder="Enter your email address" />

Phone:
<input type="tel" name="tel" id="tel" placeholder="Enter your phone number" />

Message:
<textarea id="message" name="message" placeholder=""></textarea>

<input type="submit" value="Submit" name="submit" />
</form>

Here is the code for the PHP mailer:
<?php
if(isset($_POST['submit'])) {
$to = "email@example.com";
$subject = "Subject";
$name_field = $_POST['name'];
$email_field = $_POST['email'];
$tel_field = $_POST ['tel'];
$message = $_POST['message'];

$body = "From: $name_field\n E-Mail: $email_field\n Phone: $tel_field\n Message:\n $message";

mail($to, $subject, $body, "From: $email_field");
echo "Data has been submitted to $to!";
echo "Go back to Home Page";
} else {
echo "error";
}
?>

Here is the code for the validator:

I have tried putting the javascript validator in as a separate file on the server or the script itself in the form html document and neither way seems to work. I have done a lot of searching and have checked the code and am pretty sure it's right however could not find anything to say whether or not these two functions are possible together. If someone could please confirm that it would be awesome!
Any advice would be appreciated!
Thanks in advance!
Kaz
Comment: when does validateForm() gets called ?
Comment: Are there any javascript errors? You can catch them in your Chrome browser or Firebug debugger ...
Comment: show us the  open tag as well....
Comment: where is the function's closing parenthesis?
Comment: I've never used FF debugging before. Just installed Firebug and can't see any issues.

Sry never used this site before, getting used to the coding of the msg. Forgot to hit enter.

I had closing parenthesis but then a bunch of scripts I looked at online didn't have them they just looked like this so I removed them so they looked the same just incase that was the problem.
Comment: Just realized I missed the enter for all of them. All codes now should be appearing correctly.
Answer: In your onSubmit you are calling return ValidateForm() when you should be doing return validateForm() with a lowercase "v". There are some others:
{function validateForm() should be function validateForm()
You never close the functions opening brace after the if statement.
<form name="contact" action="mailer.php" onsubmit="return validateForm()" method="post">

and then:
function validateForm() {
    var x = document.forms["contact"]["name"].value;
    if (x == null || x == "") {
        alert("Name field must be filled out");
        return false;
    }

    var x = document.forms["contact"]["email"].value;
    var atpos = x.indexOf("@");
    var dotpos = x.lastIndexOf(".");
    if (atpos < 1 || dotpos < atpos + 2 || dotpos + 2 >= x.length) {
        alert("Not a valid e-mail address");
        return false;
    }

    var x = document.forms["contact"]["tel"].value;
    if (y == null || y == "") {
        alert("Phone number must be provided");
        return false;
    }

   var x = document.forms["contact"]["message"].value;
    if (x == null || x == "") {
        alert("You have not entered a message");
        return false;
    }
}

Comment: It was lower case until i read somewhere else that there's was solved by changing to uppercase. >.< I've tried this both ways and neither work.
I just read through what you wrote for your second comment about not closing the opening brace. And it's exactly the same as my code just reformatted a little.
So unfortunately this does not resolve the problem.
Comment: @kazie151 If what you posted is your code, then your code is wrong and it needs to be changed. What I posted above, works. You admitted to it being the same (not closing the functions opening brace) and yet you say that it won't resolve the issue? If you don't close a brace, it is going to cause an error and you won't get the result you want
Comment: I apologise I read what you wrote wrong. It's been a long day and i've got a bad headache now. Yes that worked. Thank you so much! :)


Finding the distance of points after projective transformation

Question: This is more of a follow-up question to the previous: Recreating a line after projective transformation
Referring to the below diagram (adapted from the wiki page on vanishing points):

Say if I have a line $L$ that:

Lies exactly on the plane together with $L_1, L_2$,
Bounded by $L_1, L_2$,
Parallel to both $L_1, L_2$,
contains a point $P$.

Assuming I know the perpendicular distances from $P$ to $P_1$ and $P_2$ (which sits on $L_1, L_2$), as $d_1, d_2$ respectively.
Given the projection of $P$ on the image plane as $P'$, can I always assert that:
$\frac{\|{P_1' - P'}\|}{\|P_2' - P'\|}$ = $\frac {d_1}{d_2} $
where $\|P_1' - P'\|$ is the euclidean distance (in image coordinates) between $P_1'$ and $P'$ ?
Comment: Is the line through $P_1'$ and $P_2'$ parallel to the line through $P_1$ and $P_2$? If yes, then the answer to your question is yes, by the similarity of the triangles $O P_1' P_2'$ and $O P_1 P_2$.
Comment: @flawr it is unsure. All that is know of the line is that it passes through all the points, $P_1', P_2', P'$. In other words, I am not sure whether a line segment, $L_0$ perpendicular to and lies on the same plane as with $L_1, L_2$ and its projection $L_0'$ will be necessarily parallel or not.
Answer: If you don't know whether this line through $P_1, P_2$ is parallel to the image plane, then you cannot cannot infer the relationship of the two projected distances: To illustrate this, consider the extreme case where $L=V$ and where $\Vert P_1' - P'\Vert = \Vert P_2' - P \Vert$. Then we could have the situation where $P_1 = P_1'$ and $P_2$ is a lot further away from $P_2'$. That means
$$\frac{\Vert P_1' -P'\Vert}{\Vert P_2' - P'\Vert} = 1$$
but
$$\frac{d_1}{d_2} << 1.$$
Comment: To clarify it is known that the line through $P_1, P_2$ is parallel to the ground plane (in fact it lies exactly on the ground plane). Did u mean line through $P_1', P_2'$ on the image plane instead?
Comment: I edited my answer: I mean the question is whether $P_1, P_2$ is parallel to the image plane.


Handling tabs with Angular CDK FocusKeyManager

Question: I am using the CDK FocusKeyManager to manages a list of items based on keyboard interaction. The individual items can receive native focus. This works fine and I am able to tab through the items as well as navigate via the keyboard keys.
Problem is that the FocusKeyManager gets confused if I start mixing those two ways to navigate (via tab and arrow keys). It seems that the manager is not tracking the active item correctly. If I start moving the focus via arrow keys, then continue with the tab key and then return to arrow keys, the focus will continue at the point where I left with the arrow keys and not where the current focus is.
As this is quite hard to describe, I have created a StackBlitz to play around with: https://stackblitz.com/edit/cdk-focuskeymanager-zfy8pt.
What am I doing wrong?
Comment: Don't know if it's by design, but it seems `FocusKeyManager.activeItemIndex` doesn't get updated when using the tab/shift+tab keys, but does when using the arrow keys: https://stackblitz.com/edit/cdk-focuskeymanager-cbvekw?file=app%2Fapp.component.ts Looking at the [source for FocusKeyManager](https://github.com/angular/components/blob/master/src/cdk/a11y/key-manager/list-key-manager.ts), it seems as tho it doesn't consider tab/shift+tab for navigating so I suspect the navigation is happening due to browser tabbing and not linked to the `FocusKeyManager`
Comment: hello can you resolve this? i have a same issue :(
Comment: same problem here, did you find a solution? ;)
Answer: I got it working for me now, I listen to the focus event on the item, and set the active item of the keymanager of the parent.
Child Component:
private _parent = inject(ParentComponent);
constructor() {
    fromEvent(this._elementRef.nativeElement, 'focus')
        .pipe(takeUntilDestroyed())
        .subscribe(() => {
            this._parent.setActiveItem(this.id);
        });
}

Parent Component:
public setActiveItem(id: number) {
    this._keyManager?.setActiveItem(id);
}


Proving equation for normal distributed variables

Question: I'm struggling at finishing an exercise and hope some of you can help me!
Let be $X_i$ a set of random variables being normal distributed $N(0, \sigma_i^2$ for $i = 1,...,n$. I need to prove, that $\mathbb{E}[\max_{1\leq i \leq n}|X_i|] \leq \sqrt{6 \log(n)} \max_{1\leq i \leq n}\sigma_i $.
I followed a hint and proved for a konvex $\psi: \mathbb{R}^{+}\rightarrow \mathbb{R}^{+}$ that, if there exist $c_i > 0, i=1, … ,n$ and $c>0$, such that $\mathbb{E}[\frac{|X_i|}{c_i}] < c, \forall i = 1,...,n $ it holds:
$\mathbb{E}[\max_{1\leq i \leq n}|X_i|] \leq \psi^{-1}(c~ n) \max_{1\leq i \leq n}c_i$
Now I tried to apply this little lemma and took $\psi(x) = e^{x^2}$. This one is convex and strictly positive and has the inverse $\psi^{-1}(y) = \sqrt{log(y)}$.
I guess now I should take into account, that the $X_i$ are normal distributed. Unfortunatelly I didn't get the expected result, so it seems as if I would do something wrong...
Can anyone help me?
Thank you very much! :)
Answer: I don't know why you didn't get the expected result. Take $c_i=\sigma_i$. Since $E|X_i| \leq \sqrt {EX_i^{2}}=\sigma_i$ the hypothesis of the lemma holds for any $c>1$ so we get $E\max_i |X_i| \leq \sqrt {log\, cn} \max_i \sigma_i$ for any $c >1$. Letting $c \to 1$ we get a stronger result than what we are asked to prove.
Comment: Hi! I was some kind of confused, due to I got a much "better" result, than expected, so I thought I made some (stupid?) misstake.

Thank you very much for your help!


Attaching image to email sent through golang gmail api

Question: I'm trying to attach a picture to an email sent through the gmail golang api, but the image does not attach. I've looked extensively at the documentation, but I have not been able to figure out how to attach an image to an email. Here is the code that I have so far. It only sends the text.
<code>// SendEmailEmbed2 Sends email through gmail
func (config *GmailConfig) SendEmailEmbed2(person int, formattedTemplate string) {

    msg := formattedTemplate

    mmbody := gmail.MessagePartBody{
        Data: encodeWeb64String([]byte(msg)),
    }

    headers := []*gmail.MessagePartHeader{
        &gmail.MessagePartHeader{
            Name:  "From",
            Value: config.FromAddress,
        },
        &gmail.MessagePartHeader{
            Name:  "To",
            Value: config.CSVReference.Elem(person, config.PlugIns[0].ToEmail).String(),
        },
        &gmail.MessagePartHeader{
            Name:  "Subject",
            Value: config.EmailSubject,
        },
        &gmail.MessagePartHeader{
            Name:  "Content-Disposition",
            Value: "./prac2_files/image001.jpg",
        },
    }

    Parts := []*gmail.MessagePart{
        &gmail.MessagePart{
            Filename: "./prac2_files/image001.jpg",
        },
    }

    partgmsg := gmail.MessagePart{
        Body: &mmbody,

        Headers: headers,

        Parts: Parts,

        MimeType: "multipart",
    }

    im, _ := os.Open("./prac2_files/image001.jpg")
    gmsg := gmail.Message{
        Payload: &partgmsg,
        Raw:     encodeWeb64String([]byte(msg)),
    }
    call, err := config.srv.Users.Messages.Send("me", &gmsg).Media(im).Do()

    im.Close()
    if err != nil {
        log.Println("em %v, err %v", gmsg, err)
        // return err
    }
    // return err

    fmt.Println(person, ":", headers[1].Value, " : ", call.LabelIds)
}

func encodeWeb64String(b []byte) string {

    s := base64.URLEncoding.EncodeToString(b)

    var i = len(s) - 1
    for s[i] == '=' {
        i--
    }

    return s[0 : i+1]
}
</code>
Answer: Answer:
You need to attach the file inline in the body of the message.
Code:
<code>message := gmail.Message

attachmentBytes := ioutil.ReadFile(fileDir + fileName)

attachmentMimeType: = http.DetectContentType(attachmentBytes)

attachmentData: = base64.StdEncoding.EncodeToString(attachmentBytes)

boundary: = randStr(32, "alphanum")

messageBody: = [] byte("Content-Type: multipart/mixed; boundary=" + boundary + " \n" +
    "MIME-Version: 1.0\n" +
    "to: " + config.CSVReference.Elem(person, config.PlugIns[0].ToEmail).String() + "\n" +
    "from: " + config.FromAddress + "\n" +
    "subject: " + config.EmailSubject + "\n\n" +

    "--" + boundary + "\n" +
    "Content-Type: text/plain; charset=" + string('"') + "UTF-8" + string('"') + "\n" +
    "MIME-Version: 1.0\n" +
    "Content-Transfer-Encoding: 7bit\n\n" +
    content + "\n\n" +
    "--" + boundary + "\n" +

    "Content-Type: " + "image/jpg" + "; name=" + string('"') + fileName + string('"') + " \n" +
    "MIME-Version: 1.0\n" +
    "Content-Transfer-Encoding: base64\n" +
    "Content-Disposition: attachment; filename=" + string('"') + fileName + string('"') + " \n\n" +
    chunkSplit(attachmentData, 76, "\n") +
    "--" + boundary + "--")

message.Raw = base64.URLEncoding.EncodeToString(messageBody)

_, err := service.Users.Messages.Send("me", &message).Do()
if err != nil {
    log.Fatalf("Email sending failed: %v", err)
} else {
    log.Println("Email message sent")
}
</code>


Phanomjs not properly capturing screenshot of a web page

Question: I am testing phantom.js for creating screenshots from a web page at my local end.
This is how HTML is rendering in localhost - Output
But on creating PDF from code, width is not setting properly in PDF, i.e. some text gets cut as shown below.
Following is the code I tried -
var url = 'http://localhost:5656';
page.open(url);

page.onLoadFinished = function() {
    var pdfName = 'screen';
    page.render(pdfName + ".pdf");
    var height = page.evaluate(function() { return document.body.offsetHeight }),
        width = page.evaluate(function() { return document.body.offsetWidth });
    console.log(height,width);
};

I also tried these two properties but getting same result -
page.clipRect = { top: 0, left: 0, right: 0, width: 1286};
page.viewportSize = { width: 1024, height: 768};

Let me know what I am doing wrong here.
Output I am getting - PDF image I am getting]2
Answer: Following is the working code:
var url = 'http://localhost:5656';
page.open(url);

page.onLoadFinished = function() {
    //page.viewportSize = { width: 1400, height: 1200 };
    page.paperSize = {format: 'A4'};
    var pdfName = 'screen';
    page.render(pdfName + ".pdf");
    var height = page.evaluate(function() { return document.body.offsetHeight }),
        width = page.evaluate(function() { return document.body.offsetWidth });
    console.log(height,width);
};

As you can see, following is the line that performed the magic for me -
page.paperSize = {format: 'A4'};


show invertibility of linear transformations

Question: My understanding of this part of linear algebra is somewhat clouded. I fail to see how these two problems are related and/or how to prove them:
1) Show that if $A$ is a linear transformation with $A^2 - A + 1 = 0$, then $A$ is invertible.
2) If $A$ and $B$ are linear transformations on the same vector space and $AB = 1$. Prove that if A has exactly one $right\space inverse\space B$, then A is invertible. (Consider $BA + B - 1$)

My Attempt:
1) Show that if $Ax = 0$, then $x$ must be $0$. i.e. $A^2 - A + 1 = 0$ implies $A^2 + 1 = A$, implies $(A^2 + 1)x = Ax$ imples $A(A(x)) + x = A(x)$ imples $A(0) + x = 0$. Since A is linear ($A(0) = 0$), $x = 0$.
2) No idea.
Comment: For (2) show that $BA+B-I$ is also a right inverse of $A$.
Comment: I see the possible duplicate post linked by @Lord Shark the Unknown, but doesn't that solution make problem 2 trivial? In the sense that if I can say A is invertible b/c there exist (1 - A) such that A(1 - A) = 1, I can trivially come to the conclusion in problem 2 that A is invertible sense there exist a B such that AB = 1?
Comment: I read (1) and (2) as independent problems.
Comment: Simple proof of 1): $$A^2-A+1=0 =>A*(1-A)=1 => A^{-1}=1-A$$
Answer: Hint
$$A^2-A+1=0\iff A(1-A)=1$$
and
$$A(BA+B-1)=A+1-A=1=AB$$
Comment: I see the possible duplicate post linked by @Lord Shark the Unknown, or the hint you've given above, but doesn't that solution make problem 2 trivial? In the sense that if I can say A is invertible simply b/c there exist (1 - A) such that A(1 - A) = 1, I can trivially come to the conclusion in problem 2 that A is invertible since there exist a B such that AB = 1?


in C what is the proper way to pass a pointer to a string from a function to the main

Question: this is the rough idea of what I am trying to do:
I want the pointer in main to point to the word I just in my function.
my actual code is very long so please excuse this format.
main()
{
char *word;
int lim 256;
*word = function(word,lim)//I am not returning the address back only the first letter
}

function(word,lim)
{
//memory allocation
//getting word
//reset address
return(*word);//I am passing the correct address here
}

Comment: This code wouldn't compile by a long shot, and I'm not clear where the memory for the word is coming from?
Comment: If you are passing pointer as one of the arguments, then why you need a return of same pointer from your function?
Comment: yeah its not supposed to compile.  All I am care about is about how to properly pass the pointer from the function to the main.  The memory is just a word=(char*)malloc(256*sizeof(char*))
Comment: If you had this code to allocate the word, why didn't you include it in your sample?
Answer: char* allocate_word(int lim)
{
   // malloc returns a "void*" which you cast to "char*" and
   //return to the "word" variable in "main()"
   // We need to allocate "lim" number of "char"s.
   // So we need to multiply the number of "char"s we need by
   //the number of bytes each "char" needs which is given by "sizeof(char)".
   return (char*)malloc(lim*sizeof(char));
}

int main()
{
char *word;
// You need to use "=" to assign values to variables.
const int lim = 256;
word = allocate_word(lim);
// Deallocate!
free(word);

return 0;
}

Functions used in the sample code above:
malloc
free
This seems like a decent tutorial:
C Tutorial – The functions malloc and free
Comment: Even if this is a requirement of the homework, it is much better for beginners to learn correct techniques from the start and not have to unlearn them later.
Comment: This is not a good approach -- you're allocating memory in the function and requiring the caller (the main function) to release the allocated memory.  This approach, in general, will lead to memory leaks.  The correct approach is left as an exercise for the reader.
Comment: @David: I understand. But this is what the OP was trying to accomplish.
Comment: @Zack - I would only ever do that in functions that explicitly state that in their function name, like "AllocateWord()", and even then preferably only if you have a matching "FreeWord()" function. Otherwise, the risk of memory leaks is just too great.
Comment: Don't cast the result of `malloc` in C, do check for it returning `NULL`. `sizeof (char)` is always 1 so redundant.
Answer: char* func()
{
    return (char*)malloc(256);
}

int main()
{
    char* word = func();
    free(word);
    return 0;
}


jbehave run only specific story

Question: I have jbehave integrated with Selenium. I am running my tests through command line as below
C:\eclipse_workspace\MySeleniumTests>mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe"
I have used jbehave-maven-plugin. Maven picks up all the Embedder impl (JunitStories in my case) from the source directory and execute them one by one. Configuration for that is <include>**/*Stories.java</include> in pom.xml
It then looks for relevant .story files in the specified dir and executes them. Say, I have two story files one.story and two.story, both of them are executed.
Over a time, number of story files are going to increase I only want to execute specific story files should there be a way to do this? I am thinking to pass specific story file names as run time parameters but don't know what is required to make that happen.
Answer: I got it working with the below code
mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe" -Dstory=myStory.story

Override storyPaths() method in embedder class as below.
public class MyTestStories extends JUnitStories /* InjectableEmbedder */{

    @Override
    protected List<String> storyPaths() {
        List<String> storiesToRun = new ArrayList<String>();
        String storyProperty = System.getProperty("story");

        if (storyProperty == null || storyProperty.isEmpty()) {
           throw new RuntimeException("Please specify which stories to run");
        }

        String[] storyNames = storyProperty.split(",");
        StoryFinder sf = new StoryFinder();
        URL baseUrl = CodeLocations.codeLocationFromClass(this.getClass());

        for (String storyName : storyNames) {
           storiesToRun.addAll(sf.findPaths(baseUrl, storyName, ""));
        }

        return storiesToRun;
    }

Comment: i tried your code but its returning storiesToRun size as 0. any idea y i am getting that?
Comment: @Kaizer, I am using  JBehaveRunnerStories extends JUnitStories { } AND override the Configuration configuration(), InjectableStepsFactory stepsFactory() and the method you provided. Its take the stories from parameter from run configuration but when it came at for loop  for (String storyName : storyNames), loop executed two times as i have passed only two stories but when its return storiesToRun, it has zero count/size.
Comment: if it is possible then can i share my code with you...my email id is firstname.lastname@example.com
Comment: It could be possible that the .story files in your project are not at the correct location hence not in the classpath therefore the StoryFinder might not be able to find them. Can you share your project structure?
Comment: Could you please help me, Whare and what I have to check that so that I can identify the cause..because of security reasons i can not share code here..I can mail you that, my email id is email@example.com..Thank you in advance.
Comment: @SachinB not the code just the project structure, with .story files location.
Comment: @Kaizer, I have maven project set up and story files under src/test/resources/stories package.
Comment: if you have any working code(sample project) for this, it will be great help if you share that..
Comment: Sorry don't have the code now with me as I changed the job.
Answer: Try the following:
mvn clean test -Dwebdriver.firefox.bin="C:\Program Files\Mozilla\Firefox\firefox.exe" -Djbehave.story.name=<story filename without extension (wildcards are supported)>

You should also use custom test suite implementation:
public abstract class JBehaveTestSuite extends ThucydidesJUnitStories {

    private static final String STORY_NAME_PATTERN = "**/${jbehave.story.name:*}.story";

    public JBehaveTestSuite() {
        findStoriesCalled(storyNamesFromEnvironmentVariable());
    }

    @Override
    public void run() throws Throwable {
        super.run();
    }

    private String storyNamesFromEnvironmentVariable() {
        return SystemPropertyUtils.resolvePlaceholders(STORY_NAME_PATTERN);
    }
}

Comment: This could work in Thucydides but I am not using any framework. However I managed to do with someting similar to your approach.

jbehave run only specific story


Como fazer um timer no Python 3?

Question: Tenho que fazer um timer que conte x segundos, mas que não fique em loop esperando o tempo passar, ao invez disso ele deve contar o tempo e gerar um evento ao encerrar a contagem do tempo pré definido.
Preciso saber se existe algum módulo em Python 3 que possa realizar essa função e que tenha um start e um resetar na contagem?
Answer: Assim como citei na resposta abaixo
Timeout na função input do Python
Você pode utilizar o módulo signal, em sistemas Unix, para efetuar a chamada a uma função depois de um determinado tempo, sem bloquear a execução do programa principal.
import signal

def evento(*args):
    print("Evento disparado")

signal.signal(signal.SIGALRM, evento)
signal.alarm(5)

resposta = input('Por favor, aguarde 5 segundos...')

Veja funcionando no Repl.it | GitHub GIST
Para Windows, você pode adaptar a solução que apresentei na outra resposta da mesma forma.

Como fazer um timer no Python 3?


possible "pkill -f node" alternative on windows

Question: I am currently trying to fix:
[nodemon] app crashed - waiting for file changes before starting...

One solution was to run pkill -f node but that is a macOS-specific thing. Any suggestions?
Comment: I assume "pkill" is a "process kill" command, the windows equivalent would be `taskkill -f -im "node.exe"`, assuming node.exe is the process you want to kill.
Comment: @404 The difference is that `pkill` kills any processes that contain a certain phrase. For example `pkill a` would kill all of the imaginary processes `apple`, `banana` and `carrot`.
Comment: Ok, in that case `taskkill -f -im "node*"`. Can't start with a wildcard though.
Comment: @404 when I try both of your solutions the error message "ERROR: The process "node*" not found." pops up. I have node downloaded, set up and everything. Any suggestions? Thank you
Comment: What do you mean _fix_ it? That suggests your app has the problem, why does a process need killing?
Comment: @jonrsharpe I did some research and the solution was to do the above. This is because of all the running server process in the background. So all I need to do is stop them from the terminal.
Comment: It's unclear what your actual problem is, what research you did, where you found that. The message on its own just tells you the app has crashed, and nodemon will restart it once you've edited a file it's watching. This is the expected behaviour.
Comment: @jonrsharpe ohhh, thank you
Answer: You can use taskkill as suggested in the comments. Use an asterisk for a partial name of a process. For example, to kill all python scripts:

taskkill -f -im python*

And in your case, the solution can look like:

taskkill -f -im node*
Comment: Thanks for pointing out that `taskkill` accepts its options with hyphens, not just slash. I was having issues with GitBash reinterpreting `/F` as `F:/`, but `-f` works perfectly.


Python3 Problem - Last value printing to new line using format / selenium webdriver

Question: My goal: using self.cookie = '{}{}user-data-dir={}'r'{}{}{}'.format("(","'","'",'r',self.line," )")
I am setting that to be used in my main script to set a value, ok so my problem? Well I run it, and it is 99.5% the way it needs to be, I can not for hours now figure out why it keeps printing the last ) on a new line, which i am printing it to debug.. it needs to be on the same line so it can be used in actual code like so: ('user-data-dir='r'C:\Users\0\AppData\Local\Google\Chrome\User Data')

    def __init__(self):

        with open('cookies.txt', 'r+', encoding='UTF-8') as f:
            for self.line in f:

                self.chrome_options = webdriver.ChromeOptions()

                self.cookie = '{}{}user-data-dir={}'r'{}{}{}'.format("(","'","'",'r',self.line," )")
                print(self.cookie)
CookieSet()

Answer: rstrip() is what you want to remove the newline:
            self.cookie = '{}{}user-data-dir={}'r'{}{}{}'.format("(","'","'",'r',self.line.rstrip()," )")

Comment: @NZT please select it as the correct answer if it worked for you.
Answer: Use string f string is much cleanerz also can use just trim to remove line from both left and right of the string.
self.cookie= (f"(user-data-dir='{self.line.strip()}')")

print(self.cookie)


the moment generating function of the negative binomial distribution

Question: According to the textbook I use, it states that:
$X$~$Neg.bin(x;k,\theta) = {n-1 \choose k-1}\theta^k(1-\theta)^{n-k}$
Which I have no problem. The problem arises when I try to find the moment generating function. I know the procedure, I just can't compute it. I've seen many other solutions - I could follow them, but my equation is too 'different' from them and their solutions won't easily work in this case.
Can you help me with the computation? also, if possible, can you show me how to derive the mean and variance from the resulting MGF? Thank you in advance
Answer: Let $\mathbb P(X=n) = \binom{n-1}{k-1}\theta^k(1-\theta)^{n-k}$ for $n=k,k+1,\ldots$. Then the moment generating function of $X$ is given by
\begin{align}
M_X(t) :&= \mathbb E[e^{tX}]\\ &= \sum_{n=k}^\infty e^{tn}\cdot\mathbb P(X=n)\\
&= \sum_{n=k}^\infty e^{tn}\binom{n-1}{k-1}\theta^k(1-\theta)^{n-k}\\
&= \theta^k e^{tk} \sum_{n=k}^\infty \binom{n-1}{k-1}((1-\theta)e^t)^{n-k}\\
&= \frac{\theta^k e^{tk}}{(1-(1-\theta)e^t)^k}\\
&= \left(\frac{\theta e^t}{(1-(1-\theta)e^t)}\right)^k,
\end{align}
where the sum converges for all $t\in\mathbb R$. Recall that the $n^{\mathrm{th}}$ moment of $X$ is given by $\lim_{t\to0}\frac{\mathsf d^n}{\mathsf dt^n}M_t(X)$. Differentiating $M_t$, we have
$$
M_X'(t) = \frac{k \theta ^k e^{k t}}{\left((\theta -1) e^t+1\right)^{k+1}}\stackrel{t\to 0}\longrightarrow \frac k\theta = \mathbb E[X],
$$
and further
$$
M_X''(t) = \frac{k e^{k t} \left(k-(\theta -1) e^t\right)}{((\theta -1) e^t+1)^{k+2}}\stackrel{t\to 0}\longrightarrow \frac{k (-\theta +k+1)}{\theta ^2} = \mathbb E[X^2].
$$
Hence
$$
\operatorname{Var}(X) = \mathbb E[X^2] - \mathbb E[X]^2 = \frac{k (-\theta +k+1)}{\theta ^2} - \left(\frac k\theta\right)^2 = \frac{k(1-\theta)}{\theta ^2}.
$$


Create smaller lists from existing list based on delimiter

Question: I have a list that is made up of a collection of smaller lists. I need to split that aggregated list into smaller lists based on a specific value.
I have tried to use the .Take() method but that takes in an integer and I do not necessarily know how long the smaller list is going to be.
My list large list looks something like this
List<T> Foo = new List<T>();
Foo.Add("Test1", "LineTypeA");
Foo.Add("Test2", "LineTypeA");
Foo.Add("Test3", "LineTypeB");
Foo.Add("Test4", "LineTypeA");
Foo.Add("Test5", "LineTypeB");

List<List<T> Bar = new List<ListT>>();
Bar.Add(Foo);

I need to be able to split "Bar" into smaller lists based on "LineTypeB". So one list would contain 3 elements, and another list would contain two from the example above. There is a similar solution in this thread to do what I want but in python - Python splitting a list based on a delimiter word
Comment: It's not clear what your data looks like.  Is `Foo` a `List>` where each sub-list has two values or is `Foo` a `List` where `SomeType` is a combination of two strings and you meant to do something like `Foo.Add(new SomeType("Test1", "LineTypeA"));`?
Comment: Apologies for not being more thorough. Foo is a list of SomeType. I would then need Bar to loop through Foo and add values to a list until a certain value is found. In the example above Bar would loop through Foo and add the values to a list until the entry is reached that contains "LineTypeB" and then a second list made up of the remaining values up until the final "LineTypeB"
Answer: Let's assume that you have your type which your list will contain:
public class MyItem
{
    public string Key { get; set; }

    public string Value { get; set; }

    public MyItem(string key, string value)
    {
        Key = key;
        Value = value;
    }
}

And you have initial lists created like this:
List<MyItem> Foo1 = new List<MyItem>();
Foo1.Add(new MyItem("Test1", "LineTypeA"));
Foo1.Add(new MyItem("Test2", "LineTypeA"));
Foo1.Add(new MyItem("Test3", "LineTypeB"));
Foo1.Add(new MyItem("Test4", "LineTypeA"));
Foo1.Add(new MyItem("Test5", "LineTypeB"));

List<MyItem> Foo2 = new List<MyItem>();
Foo2.Add(new MyItem("Test1", "LineTypeA"));
Foo2.Add(new MyItem("Test2", "LineTypeA"));
Foo2.Add(new MyItem("Test3", "LineTypeB"));
Foo2.Add(new MyItem("Test4", "LineTypeA"));
Foo2.Add(new MyItem("Test5", "LineTypeB"));

List<MyItem> Foo3 = new List<MyItem>();
Foo3.Add(new MyItem("Test1", "LineTypeA"));
Foo3.Add(new MyItem("Test2", "LineTypeA"));
Foo3.Add(new MyItem("Test3", "LineTypeB"));
Foo3.Add(new MyItem("Test4", "LineTypeA"));
Foo3.Add(new MyItem("Test5", "LineTypeB"));

List<List<MyItem>> Bar = new List<List<MyItem>> ();
Bar.Add(Foo1);
Bar.Add(Foo2);
Bar.Add(Foo3);

Next, you can use LINQ to make a flat list (maybe it is not even needed if you will have just one inner List e.g. only Foo1):
List<MyItem> flatList = Bar.SelectMany(innerList => innerList).ToList();
string expectedValue = "LineTypeB";

And now to the most important part, you can do it by creating extension method for a list:
public static class ListExtensionMethods
{
    public static List<List<MyItem>> SplitListByValue(this List<MyItem> list, string value)
    {
        List<List<MyItem>> splittedList = new List<List<MyItem>>() { new List<MyItem>() }; // initial set

        for (int i = 0; i < list.Count; i++)
        {
            var currentItem = list[i];
            if (i == 0 && currentItem.Value == value) //if the first element already meets the condition, add it to the first sublist and create new one
            {
                splittedList.LastOrDefault().Add(currentItem);
                splittedList.Add(new List<MyItem>());
            }
            else if (currentItem.Value == value)
            {
                splittedList.Add(new List<MyItem>()); //if i-th element meets the condition, create new sublist and add it there
                splittedList.LastOrDefault().Add(currentItem);
            }
            else
            {
                splittedList.LastOrDefault().Add(currentItem);
            }
        }

        return splittedList;
    }
}

And then you call it like it follows on your flatList:
var splitList = flatList.SplitListByValue(expectedValue);

I hope this helps.
Comment: The layout of your answer in terms of the different lists is correct @zpouip however that's not how I want to create these new lists based on a certain value. In my above example, I'd need to loop through the list until "LineTypeB" is found, and add that value and all before it to a new list. This process repeats until I'm left with something like List1 = "Test1", "LineTypeA", "Test2", "LineTypeA", "Test3", "LineTypeB" and List2 = "Test4", "LineTypeA", "Test5", "LineTypeB". Formatting sucks because I'm on mobile. Hope this clears it up a little.
Comment: Ah ok, now its much clearer, I will update my answer accordingly.
Comment: @Kizzle I updated my answer. Please note in my extension method that when I find element with the expected value, I immediately cut it and create new sublist. I am not sure if you want to do it this way. If that is not the case, you will just have to update else if statement (probably you can just switch the order of two lines inside.
Comment: You're a genius @zpouip
Comment: You can just use `Last` instead of `LastOrDefault` as you know the main list always has at least one sub-list.
Answer: Here's the C# equivalent of the answer in the Python question.  I changed it to not take a delimiter and instead take a delegate that determines if the item is a delimiter.  Also I fixed it to not return empty collections when the collection is empty or the first item matches.
public static IEnumerable<IEnumerable<T>> SplitOn<T>(
    this IEnumerable<T> items,
    Func<T, bool> split)
{
    var g = new List<T>();
    foreach(var item in items)
    {
        if(split(item) && g.Count > 0)
        {
            yield return g;
            g = new List<T>();
        }

        g.Add(item);
    }

    if(g.Count > 0) yield return g;
}

Then you can use it with your example like this.
var results = Bar.SelectMany(x => x).SplitOn(x => x.Value == "LineTypeB");


Exeception get inputStream from connection 404 responce and file not found exception?

Question: I want to try download a file from resume in android studio, i have try my code with open stream and don't have any problem but in this code:
public void startStream2(Context context) {
    try {
        URL url = new URL(file.getFileUrl());
        StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(ploicy);
        URLConnection connection = url.openConnection();
        int downloaded = 0;
        BufferedOutputStream bout;
        f = new File(downloadPath, file.getName());
        if (f.exists()) {
            downloaded = (int) f.length();
            connection.setRequestProperty("Range", "bytes=" + (f.length()) + "-");
        }
        connection.setDoInput(true);
        connection.setDoOutput(true);
        connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
        connection.setRequestProperty("Accept","*/*");
        connection.connect();

        int response=((HttpURLConnection)connection).getResponseCode();
        Log.e(TAG, "startStream2: "+ response );
        if (response>399 && response<601){
            InputStreamReader sr;
            sr = new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8");
            StringBuilder builder = new StringBuilder();
            for (int bt = 0; (bt = sr.read()) != -1;) {
                builder.append((char)bt);
            }
            sr.close();
            Log.e(TAG, "startStream2: "+builder.toString());
        }
        InputStream inp=connection.getInputStream();
        BufferedInputStream in = new BufferedInputStream(inp);
        stream2 = (downloaded == 0) ? new FileOutputStream(f) : new FileOutputStream(f, true);
        bout = new BufferedOutputStream(stream2, 1024);
        byte[] data = new byte[1024];
        int x = 0;
        while ((x = in.read(data, 0, 1024)) >= 0) {
            bout.write(data, 0, x);
            downloaded += x;
            int percent = ((int) downloaded * 100) / (int) Size;
            //set percent progress
        }
    }catch (Exception e){
        Log.e(TAG, "startStream2: ",e );
    }
}

Error log:

startStream2: 405 startStream2:
                                                                           405 Not Allowed

                                                                           405 Not Allowed
nginx

startStream2:
                                                                               java.io.FileNotFoundException:
                                                                                at
  com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:242)

I have test the url before and don't have any problem with download from first or resume.
How can i resolve it?
thanks.
Answer: remove these lines
connection.setDoInput(true);
connection.setDoOutput(true);

Answer: connection.setDoOutput(true);

The problem is here. It changes the HTTP verb from GET to POST. You don't want to POST. You aren't sending any output. Remove it.
And if the response code isn't 200 you should not get the input stream, otherwise it will throw an exception. You can get the error stream if you want more info, but there isn't an input stream at this point.
You can also remove
connection.connect();

and
connection.setDoInput(true);

They don't do anything that doesn't already happen.
Comment: i send property it need do output dont need?
Comment: No. You only need `setDoOutput(true)` if you're going to call `getOutputStream()` and write something to it.
Comment: I asssume that means resolved by this answer. If there was a different resolution you should post it here for the benefit of others.And you should consider upvoting and/or accepting answers you find useful here.
Answer: Finally i have resolved it by this code:
public void startStream2(Context context) {
try {
    URL url = new URL(file.getFileUrl());
    StrictMode.ThreadPolicy ploicy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(ploicy);
    URLConnection connection = url.openConnection();
    int downloaded = 0;
    BufferedOutputStream bout;
    f = new File(downloadPath, file.getName());
    if (f.exists()) {
        downloaded = (int) f.length();
        connection.setRequestProperty("Range", "bytes=" + (f.length()) + "-");
    }
    connection.setDoInput(true);
    connection.setRequestProperty("User-Agent","Mozilla/5.0 ( compatible ) ");
    connection.setRequestProperty("Accept","*/*");

    int response=((HttpURLConnection)connection).getResponseCode();
    Log.e(TAG, "startStream2: "+ response );
    if (response>399 && response<601){
        InputStreamReader sr;
        sr = new InputStreamReader(((HttpURLConnection) connection).getErrorStream(), "UTF-8");
        StringBuilder builder = new StringBuilder();
        for (int bt = 0; (bt = sr.read()) != -1;) {
            builder.append((char)bt);
        }
        sr.close();
        Log.e(TAG, "startStream2: "+builder.toString());
    }
    InputStream inp=connection.getInputStream();
    BufferedInputStream in = new BufferedInputStream(inp);
    stream2 = (downloaded == 0) ? new FileOutputStream(f) : new FileOutputStream(f, true);
    bout = new BufferedOutputStream(stream2, 1024);
    byte[] data = new byte[1024];
    int x = 0;
    while ((x = in.read(data, 0, 1024)) >= 0) {
        bout.write(data, 0, x);
        downloaded += x;
        int percent = ((int) downloaded * 100) / (int) Size;
        //set percent progress
    }
}catch (Exception e){
    Log.e(TAG, "startStream2: ",e );
}
}

Thank a lot!
Comment: This provides no explanation, and merely repeats what other answers have already stated.


How do you find the limit as $x$ approaches infinity of $x! / a^x$ for some integer $a$

Question: The limit as $n$ approaches infinity of the following functions diverges:

$n! / a^n$
$n! / n^a$

For any fixed integer $a$. How can I show this?
EDIT: The original question was the second bullet point; however I really meant to type the first bullet.
Comment: One way: https://en.wikipedia.org/wiki/Stirling%27s_approximation
Comment: Use $x!\geq2^{x-1}$.
Answer: Hint
By ratio test
$$\lim_{n\to+\infty}\frac{\frac{a^{n+1}}{(n+1)!}}{\frac{a^n}{n!}}=0$$
thus the series $\sum\frac{a^n}{n!}$ is convergent and
$$\lim_{n\to+\infty}\frac{a^n}{n!}=0$$
thus
$$\lim_{n\to+\infty}\frac{n!}{a^n}=+\infty$$
For the second, it is easy to prove by induction that
$n!\geq 2^{n-1}$ and
$$\frac{n!}{n^a}\geq \frac{2^{n-1}}{n^a}$$
as the exponential is faster than the polynomial, the limit is $+\infty$.
Answer: $\newcommand{\bbx}[1]{\,\bbox[8px,border:1px groove navy]{{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$

$\ds{\large{n! \over a^{n}}:\ ?\quad \mbox{as}\ n \to \infty}$.
\begin{align}
{n! \over a^{n}} & \sim {\root{2\pi}n^{n + 1/2}\expo{-n} \over a^{n}} =
\root{2\pi}n^{1/2} \pars{n \over a\expo{}}^{n} \to \infty
\end{align}

$\ds{\large{n! \over n^{a}}:\ ?\quad \mbox{as}\ n \to \infty}$.

\begin{align}
\mbox{Similarly,}\quad
{n! \over n^{a}} & \sim {\root{2\pi}n^{n + 1/2}\expo{-n} \over n^{a}} =
\root{2\pi}n^{1/2}n^{n - a + 1/2}\expo{-n} \to \infty
\end{align}
Answer: HINT:
For $x$ and $a$ integers with $x>a$ we can write
$$\frac{x!}{x^a}=\left(\frac{x}{x}\frac{x-1}{x}\frac{x-2}{x}\cdots \frac{x-a+1}{x}\right)(x-a)!$$

If $x$ is real and $a$ is an integer, then
$$x!=\int_0^\infty t^{x}e^{-t}\,dt$$
Now, apply L'Hospital's Rule $a$ times.
Comment: My apologies, I actually typed the exponential incorrectly, the original I do understand. Thank you.
Answer: For a fixed $a$;
$n!=n(n-1)(n-2)(n-3)\ldots (n-(a-1)(n-a)(n-(a+1))\ldots 3.2.1$
So we have $\dfrac{n!}{n^a}=\dfrac{n(n-1)(n-2)(n-3)\ldots (n-(a-1)(n-a)(n-(a+1))\ldots 3.2.1}{n^a}$
$=(1-\dfrac{1}{n})(1-\dfrac{2}{n})\ldots \times f(n)$  where $f(n)>1$ and the product $ \to \infty $as $n\to \infty$
Answer: Intuitively, both the numerator and denominator of $\frac {n!}{a^n}$ have $n$ factors.   As $n$ gets large, most of the factors in the numerator are much larger than $a$, while all the factors in the denominator are $a$, so the numerator grows faster and the fraction diverges.
To be more rigorous, note that for $n=a^2$, you can pair the factors of $n!$ as $(1,a^2), (2,a^2-1), (3,a^2-2), \ldots (\frac 12a^2-1, \frac 12a^2)$ and the product of each pair is greater than $a^2$, so the whole product is greater than $(a^2)^{a^2/2}=a^{(a^2)}$ and the fraction is greater than $1$.  Each upward step in $n$ now multiplies the fraction by something greater than $\frac {a^2}a$, so the fraction diverges.
You can also use Stirling's approximation for the factorial, that $n! \approx \frac {n^n}{e^n}\sqrt{2 \pi n}$ and find $\frac {n!}{a^n} \gt \frac {n^n}{(ae)^n}=\left(\frac n{ae} \right)^n$  which clearly diverges as $n$ gets greater than $ae$