Let $f(x) = 1$ for rational numbers $x$, and $f(x)=0$ for irrational numbers. Show $f$ is discontinuous at every $x$ in $\mathbb{R}$

Question: I am working on this proof, and wanted someone to check it and to help me understand what is happening in case (ii). The proof:

Let $f(x) = 1$ for rational numbers $x$, and $f(x)=0$ for irrational numbers. Show $f$ is discontinuous at every $x$ in $\mathbb{R}$.
We will consider two cases.
(i) $x \in \mathbb{Q}$
Consider the sequence $x_n = x + \frac{\sqrt{2}}{n}$. We have that $(x_n) \rightarrow x$, yet $x_n$ is irrational $ \forall \ n$.
  $\Rightarrow x_n \ \in \ \mathbb{Q}$
$\Rightarrow x_n - x \in \ \mathbb{Q}$.
$\Rightarrow n(x_n-x) = \sqrt{2} \in \mathbb{Q}$.$\ \rightarrow \leftarrow$
This is a contradiction, therefore we have that $f(x_n) = 0 \ \forall \ n \Rightarrow \lim f(x_n) =0 \neq 1 =f(x)$.
Therefore, $f(x)$ is not continuous at any $x \in \mathbb{Q}$.
(ii) $x \not\in \mathbb{Q}$
Given the density of the rationals, there exists a subsequence of rational numbers that must converge to $x$. Call this subsequence $(x_{n_r})$. Therefore, we have that:
$f(x_{n_r})=1 \ \forall \ n \Rightarrow \lim f(x_{n_r}) = 1 \neq 0 = f(x).$
$\therefore \ f(x)$ is not continuous at any $x \in \mathbb{Q}$.
By cases (i) and (ii), we have that $f(x)$ is not continuous at any $x \in \mathbb{R}$.

For case (i), I used the hint in the back of my text book to generate a sequence I could work with. Part (ii) is almost identical to the solution in my text- I don't fully understand why we are using the denseness of the rationals, rather than just working with the sequence made in case (i).
Comment: @uniquesolution That's a pretty harsh thing to say to aomeone trying to learn and showing effort.
Comment: It is pretty easy to prove using the topological definition of continuity. Are you familiar?
Comment: @TylerHG- I am not familiar. I'll look into for my own studies, though I expect my prof will want us to stick with our sequential definitions and/or the epsilons and deltas...
Comment: @YoTengoUnLCD I certainly thought so... my question was not far removed from the questions s/he has answered in the past. Ah well.
Answer: When $x$ is irrational, the sequence defined in case (i) might not consist of only rationals. For example, if $x = \sqrt 2$, then $x + \frac {\sqrt 2}
 n = \frac {n+1} n {\sqrt 2} $ is irrational. (It will converge to $x$, but it doesn't accomplish what's needed.)
For (ii), you need a sequence of rationals converging to the irrational $x$. In theory, we already know one: consider the decimal expansion of $x$.  When $x$ is irrational, the sequence is necessarily infinite, doesn't eventually repeat itself forever. Suppose $$x = m + 0.d_1  d_2 \dotso$$ where $m$ is an integer. Let
$$x_n = m + 0. d_1 \dotso d_n$$
In other words, $x_n$ is the  decimal representation of $x$ cut off at the $n^{th}$ digit after the decimal point ($x_0 = m$). Then every $x_n$ is rational, and:
$$lim_{n \to \infty} x_n = x$$
Comment: Ok. My professor actually suggested $x + \frac{\pi}{n}$ instead, though that would yield the same problem, wouldn't it?
Comment: Using $\pi$, some of the terms of the resulting sequence might be rational (suppose $x = k\pi)$. A little extra verbiage is needed to show that the sequence is eventually all rationals. I emended my answer to give a more concrete example of a sequence that works.
Comment: Thank you- that was helpful for part (ii). So, this would suffice to serve as a better way to show the second case (instead of trying to use the denseness argument), correct?
Comment: Actually yes, I thnk so. The argument using density of the rationals is certainly *true*, but it's *nonconstructive*, in that it just plucks a sequence $x_n$ out of thin air, The sequence I gave is more constructive in flavor: if you can say anything explicit about the irrational $x$ (e.g. how to compute it, to within any degree of precision), then we can figure out how to convert that to our standard sequence of approximations given by the decimal representation.
Comment: Thanks, BrianO. I often struggle with when we really need to construct something vs when we can just use pick something that will suffice for the proof.
Comment: You're not alone :) -- [(link)](https://en.wikipedia.org/wiki/Constructivism_%28mathematics%29)
Answer: Rationals and irrationals are both dense in the real numbers. If we pick $x\in\mathbb{Q}$ then there is a sequence of irrationals converging to $x$, thus proving discontinuity of $f$ at $x$. If $x\notin\mathbb{Q}$ then there is a sequence of rationals...(I'm sure you can now formally make your complete proof)
Comment: Ah, gotcha. Thanks, that makes sense.


How π = πP is equivalent to (I − P^T )π = 0 in stationary distribution of markov chain

Question: How
pi=pi * P
where P is a probability transition matrix.
is equivalent to the equation
(I-transpose(P)) * pi = 0
where I is the Identity matrix.?
Answer: Recall, $\pi$ is a row vector.
$$\pi = \pi P \Rightarrow \pi I = \pi P$$
$$I^T \pi^T = P^T \pi ^ T \Rightarrow (I^T - P^T) \pi^ T = 0$$
The last puzzle piece is $I^T = I$.
Comment: Thanks a lot, @dEmigOd. One small clarification, Here P is a matrix if we bring a matrix from RHS to LHS doesn't it become inverse of P..?
Comment: If you do it by multiplication, then yes. But we here subtract vectors and change from row form to column.
Comment: Thanks for your prompt response @dEmigOd.


Animating SurfaceView with MediaPlayer inside

Question: In my application I am using MediaPlayer and SurfaceView with online video streaming. In the beggining, I am stretching SurfaceView to match the parent layout's screen height; when I click it, I want my SurfaceView to change it's size to match screen width (all the time preserving proportions). Resizing works as intended, so I added animation. And animation also works as intended... except for MediaPlayer inside, which changes it's size in different pace than SurfaceView.
Effect is, my SurfaceView scales as it should through time (I gave it a try without MediaPlayer pinned to it), but MediaPlayer video is scaling a bit faster than SurfaceView, which causes shaking effect (faster the transition, more shaky it becomes). Is there any way to control how does MediaPlayer draw itself on surface?
Comment: If that will help - as far as I remember VideoView had no such problem, it was resizing perfectly fine (unfortunately using it is not an option).
Answer: Writing this just in case anyone will have same problems - change your SurfaceView to TextureView. TV is as easy to use as SV, but works MUCH better with animations (seems like SV can't handle them as good as TV does).


Compatibility of jPOS and Weblogic

Question: Hello I'm having hard time trying to configure jPOS to run in weblogic.
jPOS version 1.9.8
weblogic 10.3.6.0
I got this error:
14:01:12 [[ACTIVE] ExecuteThread: '19' for queue: 'weblogic.kernel.Default (self-tuning)'] (T0100.java:31)
     org.jpos.iso.ISOException: java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl cannot be cast to javax.xml.parsers.SAXParserFactory (java.lang.ClassCastException: org.apache.xerces.jaxp.SAXParserFactoryImpl cannot be cast to javax.xml.parsers.SAXParserFactory)
            at org.jpos.iso.packager.GenericPackager.readFile(GenericPackager.java:223)
            at org.jpos.iso.packager.GenericPackager.<init>(GenericPackager.java:130)
            at ums.expy.ulink.T0100.<clinit>(T0100.java:28)
            at ums.expy.ws.service.T0100Service.doJob(T0100Service.java:75)

Code:
private static ISOPackager T0100Packager;
static {
    try {
        T0100Packager = new GenericPackager(
                T0100.class.getResourceAsStream("/ulink0100.xml"));
    } catch (ISOException e) {
        logger.error("", e);
    }
}

Somebody says it will be OK when remove xml-apis.jar, but jPOS needs xml-apis.jar.
I have configed WEB-INF/weblogic.xml
    <?xml version="1.0" encoding="UTF-8"?>
<weblogic-web-app xmlns="http://www.bea.com/ns/weblogic/weblogic-web-app"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.bea.com/ns/weblogic/weblogic-web-app http://www.bea.com/ns/weblogic/weblogic-web-app/1.0/weblogic-web-app.xsd">
    <container-descriptor>
        <prefer-web-inf-classes>true</prefer-web-inf-classes>
    </container-descriptor>
</weblogic-web-app>

My maven pom.xml
    <dependency>
        <groupId>org.jpos</groupId>
        <artifactId>jpos</artifactId>
        <version>1.9.8</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.7</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis-spring</artifactId>
        <version>1.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc14</artifactId>
        <version>10.2.0.5.0</version>
    </dependency>
    <dependency>
        <groupId>org.mybatis</groupId>
        <artifactId>mybatis</artifactId>
        <version>3.2.3</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>3.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>3.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>3.2.6.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>commons-codec</groupId>
        <artifactId>commons-codec</artifactId>
        <version>1.9</version>
    </dependency>
    <dependency>
        <groupId>c3p0</groupId>
        <artifactId>c3p0</artifactId>
        <version>0.9.1.2</version>
    </dependency>
    <dependency>
        <groupId>commons-beanutils</groupId>
        <artifactId>commons-beanutils</artifactId>
        <version>1.9.1</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>2.6</version>
    </dependency>
    <dependency>
        <groupId>xalan</groupId>
        <artifactId>xalan</artifactId>
        <version>2.7.1</version>
    </dependency>

Why it's not workig?
Answer: Looks like a conflict in your Xerces versions, perhaps WebLogic's taking precedence over jPOS' xercesImpl-2.10.0.jar
Answer: In your weblogic.xml add the following
    <prefer-application-packages>
        <package-name>org.apache.xerces.*</package-name>
    </prefer-application-packages>

If you have prefer-web-inf-classes in it,  remove or comment it out.

Compatibility of jPOS and Weblogic


SSH keys no longer work?

Question: The Problem
Trying to connect to my QNAP server from two different computers, which previously could connect via ssh without a password -- but now a password is asked for each time.  What went wrong, and how do I fix it?
What I've tried
I've rooted around for ideas and checked the following, all with no success:

Add public keys from client(s) to server: (~/.ssh/authorized_keys).
Set file permissions: The admin (root) account on the server has run both:
chmod 700 ~/.ssh/
chmod 600 ~/.ssh/*
Recreating and uploading the keys

Addtional info
I can't think of updates that would have happened that match the timeline of the keys no longer working, so I don't know why thy would have stopped functioning suddenly.
@Yevhen-Stasiv suggested checking my config -- turns out this file is reset every time the server reboots; nonetheless the values are:
[~/.ssh] # cat sshd_config
Protocol 2
HostKey /etc/ssh/ssh_host_rsa_key
HostKey /etc/ssh/ssh_host_dsa_key
PermitRootLogin yes
UseDNS no
Subsystem sftp /usr/libexec/sftp-server
AllowTcpForwarding no
AllowUsers admin

The version of OpenSSL on my server is:
# ssh -V
OpenSSH_8.0p1, OpenSSL 1.0.2k  26 Jan 2017

I'm not sure if that makes any difference, but there it is.
Finally, increasing verbosity of the command (and preventing password authentication), this is what I'm seeing:
$ ssh -vv -o PasswordAuthentication=no admin@nasdekleer
OpenSSH_8.1p1, OpenSSL 1.1.1d  10 Sep 2019
debug1: Reading configuration data /etc/ssh/ssh_config
debug2: resolving "nasdekleer" port 22
debug2: ssh_connect_direct
debug1: Connecting to nasdekleer [10.132.197.180] port 22.
debug1: Connection established.
debug1: identity file /c/Users/Ryan/.ssh/id_rsa type 0
debug1: identity file /c/Users/Ryan/.ssh/id_rsa-cert type -1
debug1: identity file /c/Users/Ryan/.ssh/id_dsa type -1
debug1: identity file /c/Users/Ryan/.ssh/id_dsa-cert type -1
debug1: identity file /c/Users/Ryan/.ssh/id_ecdsa type -1
debug1: identity file /c/Users/Ryan/.ssh/id_ecdsa-cert type -1
debug1: identity file /c/Users/Ryan/.ssh/id_ed25519 type -1
debug1: identity file /c/Users/Ryan/.ssh/id_ed25519-cert type -1
debug1: identity file /c/Users/Ryan/.ssh/id_xmss type -1
debug1: identity file /c/Users/Ryan/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.0
debug1: match: OpenSSH_8.0 pat OpenSSH* compat 0x04000000
debug2: fd 3 setting O_NONBLOCK
debug1: Authenticating to nasdekleer:22 as 'admin'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug2: local client KEXINIT proposal

[...output omitted for brevity...]

debug1: Server host key: ssh-rsa SHA256:/WeVhQddipMcY6dZdQ+HgL+kMhsUEqRqLEsdpK14MNM
debug1: Host 'nasdekleer' is known and matches the RSA host key.
debug1: Found key in /c/Users/Ryan/.ssh/known_hosts:1
debug2: set_newkeys: mode 1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug2: set_newkeys: mode 0
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /c/Users/Ryan/.ssh/id_rsa RSA SHA256:1sWIuTIkFdDZ9cxqz1va1Y+SnAXAkL61/0lLTe9fW5c
debug1: Will attempt key: /c/Users/Ryan/.ssh/id_dsa
debug1: Will attempt key: /c/Users/Ryan/.ssh/id_ecdsa
debug1: Will attempt key: /c/Users/Ryan/.ssh/id_ed25519
debug1: Will attempt key: /c/Users/Ryan/.ssh/id_xmss
debug2: pubkey_prepare: done
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519,ssh-rsa,rsa-sha2-256,rsa-sha2-512,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521>
debug2: service_accept: ssh-userauth
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Next authentication method: publickey
debug1: Offering public key: /c/Users/Ryan/.ssh/id_rsa RSA SHA256:1sWIuTIkFdDZ9cxqz1va1Y+SnAXAkL61/0lLTe9fW5c
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug1: Trying private key: /c/Users/Ryan/.ssh/id_dsa
debug1: Trying private key: /c/Users/Ryan/.ssh/id_ecdsa
debug1: Trying private key: /c/Users/Ryan/.ssh/id_ed25519
debug1: Trying private key: /c/Users/Ryan/.ssh/id_xmss
debug2: we did not send a packet, disable method
debug1: Next authentication method: keyboard-interactive
debug2: userauth_kbdint
debug2: we sent a keyboard-interactive packet, wait for reply
debug1: Authentications that can continue: publickey,password,keyboard-interactive
debug2: we did not send a packet, disable method
debug1: No more authentication methods to try.
admin@nasdekleer: Permission denied (publickey,password,keyboard-interactive).

Update
I haven't been able to track down the logs on the server, however, I found I could run my own ssh daemon on a different port by logging into the server and running:
/usr/sbin/sshd -p 50023 -D -d -e -f /etc/config/ssh/sshd_config
...and then going to my client and attempting a login like so:
ssh -v -p 50023 admin@nasdekleer
and then can see the output on the server-side:
[ ... ]

debug1: userauth-request for user admin service ssh-connection method none [preauth]
debug1: attempt 0 failures 0 [preauth]
debug1: userauth-request for user admin service ssh-connection method publickey [preauth]
debug1: attempt 1 failures 0 [preauth]
debug1: userauth_pubkey: test pkalg rsa-sha2-512 pkblob RSA SHA256:1sWIuTIkFdDZ9cxqz1va1Y+SnAXAkL61/0lLTe9fW5c [preauth]
debug1: temporarily_use_uid: 0/0 (e=0/0)
debug1: trying public key file /root/.ssh/authorized_keys
debug1: fd 4 clearing O_NONBLOCK
Authentication refused: bad ownership or modes for directory /mnt/HDA_ROOT

[...]

So it's seeming to me that this might be a file permission issue still?
Answer: So getting more info from the server was definitely the ticket.
Open a ssh daemon on the server with:
/usr/sbin/sshd -p 50023 -D -d -e -f /etc/config/ssh/sshd_config
and try to log in from a client with :
ssh -v -p 50023
Seeing the message:
Authentication refused: bad ownership or modes for directory /mnt/HDA_ROOT

Checking the directory with ll /mnt, got this output:
drwxr-xr-x   10 admin    administ       220 Jan  8 17:44 ./
drwxr-xr-x   20 admin    administ       480 Jan  8 19:35 ../
drwxrwxrwx   12 httpdusr administ      4.0k Jan  8 17:44 HDA_ROOT/

Yikes.  I must have somehow changed the ownership of the root home directory when setting up some stuff for a webserver!
Switched the ownership back with chown admin /mnt/HDA_ROOT.
And now everything is hunky-dory again! :D
Answer: The issue is most likely server side. A software update may have rewritten the sshd config, verify that the configuration is still set to permit key auth.
Have a look at the sshd log file for any clues, if there is nothing there try running sshd in foreground & debug mode to quickly see what is going on.
Comment: Finding the log will definitely help, it is possible that you may need to bump up the verbosity of the logging by setting `LogLevel VERBOSE` within `sshd_config`.

`grep SyslogFacility /etc/config/ssh/sshd_config` this should specify the 'facility' where the logs will be recorded. That setting would correspond to `/etc/syslog.conf` in which the 'syslog facilities' are defined.
Comment: Good call.  I tried adding `PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys` but it clears out when I reboot the server, so I can't tell if its actually making a difference.  I've updated the question to include the config file.
Comment: I've tried to hunt down the sshd log but the QNAP setup is a bit different than the usual linux setups, and it doesn't seem to be in the regular places.
Comment: Try to run 'netstat -netupl' on the server then look at the PID listening on your SSH port 'ps ww PID'. What does that show? Is it 'dropbear' or 'sshd'? Sometimes the config will be revealed in the command line arguments.
Comment: I'm a little lost as to what I need to look for in the netstat output, and I had to drop the -p flag as it's apparently unsupported on my system.
I'm getting a hit on `sshd` (nothing for `dropbear`) when grepping `ps -ww PID`, but the cmdline args are just for the executable (`/usr/sbin/sshd`) and the config (`/etc/config/ssh/sshd_config`).  From what I am reading in QNAP's documentation, it says " For AFP and SSH connections, the system can only record login and logout events", so I'm not sure that's even going to help...

SSH keys no longer work?


Is there a way to access a private property in a where clause?

Question: I'm trying to directly map my domain model to EF. To that end I introduced a private property in my model like so:
private ICollection<Tag> TagsInternal { get; set; }

public Article(Guid id, ... , IEnumerable<Tag> tags) : base(id)
{
   ...
   this.TagsInternal = new List<Tag>(tags.Where(i => i != null));
}

public IEnumerable<Tag> Tags { get { return this.TagsInternal.AsEnumerable(); } }

In order to get access for EF to the 'backing property' I added a handful of extension methods:
public static class FluentApiExtensions
{
   public static ManyNavigationPropertyConfiguration<TEntityType, TTargetEntityType>
      HasMany<TEntityType, TTargetEntityType>(this EntityTypeConfiguration<TEntityType> mapper,
      string propertyName)
      where TEntityType : class
      where TTargetEntityType : class
   {
      var lambda = GetLambdaExpression<TEntityType>(propertyName);

      return mapper
         .HasMany((Expression<Func<TEntityType, ICollection<TTargetEntityType>>>)lambda);
   }

   public static ManyToManyNavigationPropertyConfiguration<TEntityType, TTargetEntityType>
      WithMany<TEntityType, TTargetEntityType>(this ManyNavigationPropertyConfiguration<TEntityType, TTargetEntityType> mapper,
      string fieldName)
      where TEntityType : class
      where TTargetEntityType : class
   {
      var lambda = GetLambdaExpression<TTargetEntityType>(fieldName);

      return mapper
         .WithMany((Expression<Func<TTargetEntityType, ICollection<TEntityType>>>)lambda);
   }

   private static LambdaExpression GetLambdaExpression<T>(string propertyName)
   {
      var type = typeof (T);
      var parameterExpression = Expression.Parameter(type, "type");
      var expression = (Expression)parameterExpression;

      var propertyInfo = type
         .GetProperty(propertyName, BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance);

      if (propertyInfo == null)
         throw new ArgumentException(string.Format("There is no property named '{0}' on type '{1}'.",
            propertyName, type.Name));

      var propertyExpression = Expression.Property(expression, propertyInfo);

      return Expression.Lambda(propertyExpression, parameterExpression);
   }
}

I am then in a position to pick up navigation properties like this:
public Maybe
 GetArticle(Guid articleId)
{
   articleId.MustNotBeNull();

   var article = this.unitOfWork.Context.Articles
      .Include("TagsInternal")
      .FirstOrDefault(a => a.Id == articleId);

   return article == null
      ? new Maybe
()
      : new Maybe
(article);
}

However, the whole thing breaks down when trying to execute a query that has "Tags" in its where clause, for example
var test = this.unitOfWork.Context.Articles
   .Where(a => a.Tags.Count() > 0);

The message is
The specified type member 'Tags' is not supported in LINQ to Entities. Only initializers, entity members, and entity navigation properties are supported.
Is there any way to remedy the situation? I'm seriously considering to just keep EF out of the model and map instead or maybe go down the state object route as suggested by Vaughn Vernon.
Answer: You said you're mapping your domain model to EF.  So with that in mind you'll want to keep your EF classes as simple as possible.  They only exist to persist your domain model.  That means:

Do not put methods on EF classes.
Do not use anything other than the default constructor.
Only use public methods.
Map your domain classes to EF classes manually or use something like AutoMapper.

Also, based on your code above it looks like you have your own UnitOfWork implementation.  This is unnecessary as DbContext is the unit of work.  (Use your DI container to manage DbContext sessions.  You probably want them to have a per-request lifetime if this is a web application.)
I'm also not sure why you're returning Maybe
 above.  If it's not found, just return null.  In your controller, if GetArticle() returns null, then return HttpNotFound() from the controller.
If do the above, that should clear up the confusion and remove a lot of the mapping code you have above.


Why is LVS dropping packets?

Question: I am currently trying to get down to the core of a problem where my
LVS-director seems to drop a packet coming from a client from time to
time. We have this problem on our production systems and can reproduce
the problem on staging.
I posted this problem on the lvs-users-mailing-list and got no response so far.
Our setup:
We are using ipvsadm with Linux CentOS5 x86_64 in a PV XEN-DomU.
Current Version details:

Kernel: 2.6.18-348.1.1.el5xen
ipvsadm: 1.24-13.el5

LVS-Setup:
We use IPVS in DR-mode, for managing the running connections we use
lvs-kiss.
ipvsadm is running in a heartbeat-v1-cluster (two virtual nodes), master
and backup are running constantly on both nodes.
For the LVS-services we use logical IPs being setup by heartbeat
(active/passive-clustermode)
The real-servers are physical Linux-machines.
Network-Setup:
The VM acting as director is running as XEN-PV-DomU on a Dom0 using bridged networks.
Networks "in play":

abn-network (staging-network, used to connect the client to the director),
used by the real-servers to send the answer to the clients (direct routing approach),
used for ipvsadm slave/master multicast-traffic
lvs-network: This is a dedicated VLAN which connects director and real-servers
DR-arp-problem: solved my suppressing arp-answers on the real-servers for the service-ip
The service-IP is configured as logical IP on the lvs-interface on the real-servers.
In this setup ip_forwarding is not needed anywhere (neither on
director, nor on real-server).

VM details:
1 GB RAM, 2 vCPUs, system-load almost 0, memory 73M free, 224M buffers, 536M cache, no swap.
top shows almost always 100% idle, 0% us/sy/ni/wa/hi/si/st.
Configuration details:
ipvsadm -Ln for the service in question shows:
TCP  x.y.183.217:12405 wrr persistent 7200
 -> 192.168.83.234:12405   Route   1000   0          0
 -> 192.168.83.235:12405   Route   1000   0          0

x.y first two octets are from our internal class-B-range.
We use 192.168.83.x as lvs-network for staging.
Persistent ipvsadm-configuration:
/etc/sysconfig/ipvsadm: --set 20 20 20
Cluster-configuration:
/etc/ha.d/haresources: $primary_directorname lvs-kiss x.y.183.217
lvs-kiss-configuration-snippet for the service above:
<VirtualServer idm-abn:12405>
  ServiceType       tcp
  Scheduler         wrr
  DynamicScheduler    0
  Persistance         7200
  QueueSize           2
  Fuzz              0.1
  <RealServer rs1-lvs:12405>
    PacketForwardingMethod  gatewaying
    Test ping -c 1 -nq -W 1 rs1-lvs >/dev/null
    RunOnFailure   "/sbin/ipvsadm -d -t idm-abn:12405 -r rs1-lvs"
    RunOnRecovery   "/sbin/ipvsadm -a -t idm-abn:12405 -r rs1-lvs"
  </RealServer>
  <RealServer rs2-lvs:12405>
    PacketForwardingMethod  gatewaying
    Test ping -c 1 -nq -W 1 rs2-lvs >/dev/null
    RunOnFailure   "/sbin/ipvsadm -d -t idm-abn:12405 -r rs2-lvs"
    RunOnRecovery   "/sbin/ipvsadm -a -t idm-abn:12405 -r rs2-lvs"
  </RealServer>
</VirtualServer>

idm-abn, rs1 and rs2 resolve via /etc/hosts.
About the service:
This is a soa-web-service.
How we reproduce the error:
From a client we run constant calls to the web-service at an interval of one call in three seconds.
From time to time there will be a connection reset from the director to the client.
Interesting: This happens on n x 100th + 1 tries - interesting is the one.
What we did to trace down the problem:

Checked /proc/sys/net/ipv4/vs: all values are set to default, so drop_packet is NOT in place (=0)
tcpdump on client, fronted/abn of the director, backend/lvs of the directory, lvs and abn of the real-servers

In this tcpdump we could see a request from the client, answered by a
connection-reset by the director.
The packet was NOT forwarded via LVS.
I welcome any ideas on how to track this problem further down.
If any information is unclear/missing to drill down the problem - please
ask.
Answer: Do you have any stateful iptables rules on the LVS-DR director? As I can see you are using port 12405, so if you have a rule like this:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -m state --state NEW -m tcp -p tcp --dport 12405 -j ACCEPT

In LVS-DR real servers are replying to requests from clients (and not the director), the director won't add those connections in the connection tracking table and the FIN packets won't be detected on the director's iptables with the rules ESTABLISHED,RELATED. Since you only allow NEW (SYN) packets on port 12405, FIN will be blocked. You have to use a stateless firewall on an LVS-DR director for load balanced services:
iptables -A INPUT -m tcp -p tcp --dport 12405 -j ACCEPT

Comment: I forgot to mention that the default rule for incomming is DROP and since `ESTABLISHED,RELATED` wouldn't do anything the director would drop the FIN packet.

If you don't have these rules, the only thing I could suggest is that you try to disable firewall on both the director and the real servers and see if that helps, but you probably tried that already.
Comment: Sadly yes. If we had Firewall problems the drop-rate would be much higher.
Comment: How would this explain the behaviour? Apart from that no, there are no rules like that on the director. And even if they were, the established,related would do just nothing (as - like you correctly noted on DR the real-server replies to the client directly).

Why is LVS dropping packets?


VBA Query that pulls data from both excel table and SQL database

Question: I have been working on this for quite a while and I can't seem to figure it out. I am trying to create a query in excel that combines information from an excel table and database. I can do each of them separately no problem.
Here is the VBA code for the excel query:
    Sub ExcelQuery()
'
    Range("Table_Query_from_Excel_Files5[[#Headers],[Customer:]]").Select
    With Selection.ListObject.QueryTable
        .Connection = Array(Array("ODBC;DSN=Excel Files;DBQ=Z:\OEM Office\Trevor Weinrich\Projects\BOM Template 2.0\BOM template 2017-08-16 1.xlsm;DefaultDir=Z:\OEM Office\Trevor Weinrich\Projects\BOM Template 2.0;DriverId=1046;MaxBufferSize=2048;PageTimeout=5;"))
        .CommandText = Array( _
        "SELECT `BOM$`.`Customer:`" & Chr(13) & "" & Chr(10) & "FROM `BOM$` `BOM$`" & Chr(13) & "" & Chr(10) & "WHERE (`BOM$`.`Customer:` Is Not Null)" _
        )
        .Refresh BackgroundQuery:=False
    End With
'
End Sub

And here is the VBA code for the database query:
Sub DatabaseQuery()
'
    With Selection.ListObject.QueryTable
        .Connection = _
        "ODBC;DSN=OEM;Description=OEM;UID=trevor.weinrich;Trusted_Connection=Yes;APP=Microsoft Office 2016;WSID=DFP-OEM-0913-A;DATABASE=OEM"
        .CommandText = Array( _
        "SELECT DISTINCT p21_view_item_uom.item_id, p21_view_item_uom.unit_of_measure, p21_view_item_uom.purchasing_unit" & Chr(13) & "" & Chr(10) & "FROM OEM.dbo.p21_view_item_uom p21_view_item_uom" & Chr(13) & "" & Chr(10) & "WHERE (p21_view_item_uom.delete_flag=" _
        , "'N')" & Chr(13) & "" & Chr(10) & "ORDER BY p21_view_item_uom.item_id")
        .Refresh BackgroundQuery:=False
    End With
'
End Sub

I want to join these together because there are about 140,000 line items in the database query, and I only care about the the instances where the "item_id" field from the database matches up with the "Customer:" field.
I just can't figure out how to join the two of them. I would greatly appreciate the help.
Here is the code where I am just trying to pull in a variable that gives me the error after 165 characters:
Sub Update_Item_Tables()
'
' UOM_Update Macro
'
Dim Items As String
Items = Sheets("UOM").Range("K1").Value

    Sheets("UOM").Visible = True
    Sheets("UOM").Select
    Range("Table_Query_from_OEM[[#Headers],[item_id]]").Select
    With Selection.ListObject.QueryTable
        .Connection = _
        "ODBC;DSN=OEM;Description=OEM;UID=trevor.weinrich;Trusted_Connection=Yes;APP=Microsoft Office 2016;WSID=DFP-OEM-0913-A;DATABASE=OEM"
        .CommandText = Array( _
        "SELECT DISTINCT p21_view_item_uom.item_id, p21_view_item_uom.unit_of_measure, p21_view_item_uom.purchasing_unit" & Chr(13) & "" & Chr(10) & _
        "FROM OEM.dbo.p21_view_item_uom p21_view_item_uom" & Chr(13) & "" & Chr(10) & _
        "WHERE (p21_view_item_uom.item_id In (" _
        , _
        "" & Items & ")) AND (p21_view_item_uom.delete_flag='N')" & Chr(13) & "" & Chr(10) & _
        "ORDER BY p21_view_item_uom.purchasing_unit DESC" _
        )
        .Refresh BackgroundQuery:=False
    End With

End Sub

Comment: Might be better to insert your excel data into a table in SQL and then do a join there, rather than trying to join in the application.
Comment: I don't believe you can create a query that goes against both a database and Excel at the same time.  You could however retrieve the data from each query into separate DataTables and merge the results.
Comment: I don't know anything about actually inserting data into SQL. The data I am pulling is from my companies MRP system.
Comment: Maybe I am going at this from the wrong angle. All I really need is to specify that the query pulls information based upon the contents of a column in the excel spreadsheet. I have a list of part numbers in the spreadsheet, and I want the query to only pull information on those part numbers. I have tried passing a string variable into a "WHERE item_id IN(StringFromExcel), but I get a Type mismatch error as soon as the characters in the string reach 165 or greater.
Comment: What is the datatype of the item_id field in the db?
Comment: @TWeinrich: Test your `StringFromExcel` for length of 165.  If greater, create a `StringFromExcel2` and then add a `OR item_id IN(StringFromExcel2)` to your WHERE code.  It's sloppy, but it'll work.  Only you know the real size of your data.
Comment: @TWeinrich:  Also... I agree with those saying this should be done from the main SQL database.  But if your company doesn't give you access to writing your own procs there, you might want to see if they'll let you use Access and link your tables in there.  (Of course, you'd then have to get your data back to Excel, but if the size of your tables are big enough and parameters of your project allow, it may be worth the trouble.)
Comment: The datatype for item_id is varchar
Comment: abraxascarab, I don't fully understand what you mean by that. How would that look as code?
Comment: @abraxascarab: The odds are very slim that they would give me that kind of access. Could you please explain what you meant by the StringFromExcel2 segment you posted? What would the content of that string be?
Comment: @TWeinrich:  If you are making a comma delimited string for `StringFromExcel`, you need to end it before it's bigger than 165.  You can then continue a new string with the remaining part numbers in `StringFromExcel2`.  (And theoretically, you may need a 3rd, 4th or 5th)  Then you can add the appropriate `OR` statements to include the remaining part numbers.  Does that make sense?  Also, I don't see your code with the IN function, this would be easy for me or others to explain with the code. :)
Comment: @abraxascarab I attached the code above, and thank you for the help.
Answer: Yikes... some of that code is... let's call it unique.  But I won't ask questions, lets just deal with the problem you are asking about for now.
The goal is to check your long Items string so you only need one Sql command to grab your data.  But the string needs to be in chunks less than 165 characters.
As I mentioned before, this is a sloppy way to do it, but if they have you locked out of your SQL database for development purposes, this approach will work:
You can split your Item string into multiple strings each less than 165 characters.  Then you can use OR statements in your SQL to use the IN function with each Item string that has data.
First, in your VBA, you need to:
'Delcare your Variables

Dim SQLText As String
Dim Items2 As String
Dim Items3 As String
Dim boolItemFlag2 As Boolean
Dim boolItemFlag3 As Boolean
Dim i As Integer

' Initialize Variables

boolItemFlag2 = False
boolItemFlag3 = False

Then you can do your processing to build your 'Items' string.  I'm assuming it's comma delimited something like this:
Items = "'123456','234567'"

After it's built you can:
' Get your Items list and check for length over 164

If Not IsNull(Len(Items)) Then
   If Len(Items) > 164 Then
      boolItemFlag2 = True
      i = 0

      ' Find last comma delimiter before the cut off of 164

      While (Mid(Items, 164 - i, 1) <> ",")
         i = i + 1
      Wend

      ' Set Items2 to everything after the last comma (before pos 164).  Then Left Trim away possible spaces.

      Items2 = LTrim(Mid(Items, 164 - i + 1, Len(Items) - 164 + i))

      ' Reset Items to everything up to that comma we found but not including it.

      Items = Left(Items, 164 - i - 1)

      ' Your Item list is now split into 2 different string variables

   End If
End If

If Not IsNull(Len(Items2)) Then
   If Len(Items2) > 164 Then
      boolItemFlag3 = True
      ' Use the same logic above to split Items2 into the Items3 variable.
      ' You may need to duplicate again for Items4 etc if your Item List is huge.
      ' If you'd need beyond Items5, I'd probably go back to the two query approach.
      ' But it's your choice.
   End If
End If

Then, after you have your Item lists split into variables that are each small enough, you can build a unique SQL string (called SQLText) and insert OR statements to include items strings that have data:
' Build your SQL String Here (after you have determined how many item strings you have)

SQLText = "SELECT DISTINCT p21_view_item_uom.item_id, p21_view_item_uom.unit_of_measure, p21_view_item_uom.purchasing_unit" & Chr(13) & "" & Chr(10) & _
     "FROM OEM.dbo.p21_view_item_uom p21_view_item_uom" & Chr(13) & "" & Chr(10) & _
     "WHERE (p21_view_item_uom.item_id In (" _
     & _                  ' <-- I changed your comma into an & because I have no clue...
     "" & Items & ")"

If boolItemFlag2 Then   ' Add this OR statement if Items2 has data
     SQLText = SQLText & " OR p21_view_item_uom.item_id In (" & Items2 & ")"
End If

If boolItemFlag3 Then   ' Add this OR statement if Items3 has data
     SQLText = SQLText & " OR p21_view_item_uom.item_id In (" & Items3 & ")"
End If

' Add more OR statements if you have Item lists beyond 3.

' Now tack on your remaining SQL code:

SQLText = SQLText & ") AND (p21_view_item_uom.delete_flag='N')" & Chr(13) & "" & Chr(10) & _
     "ORDER BY p21_view_item_uom.purchasing_unit DESC"

Now the String SQLText will have your complete SQL code that you want to send to the server.  You can execute this message box if you want to verify the string is correct:
MsgBox SQLText

You need to make one more modification.  When you call the database, you need to put the variable SQLText into the commandtext array (in place of your old code):
' Now you can put your SQLText variable into your ODBC call:

With Selection.ListObject.QueryTable
    .Connection = _
"ODBC;DSN=OEM;Description=OEM;UID=trevor.weinrich;Trusted_Connection=Yes;APP=Microsoft Office 2016;WSID=DFP-OEM-0913-A;DATABASE=OEM"
    .CommandText = Array(SQLText)
    .Refresh BackgroundQuery:=False
End With

Hope that helps :)
Answer: Run your Excel query.
Loop through the result set and build a list of OR conditions, such as item_id = 'ABC' Or item_id = 'PQR' and so on.
Instead of using an IN condition, use the (huge) list of OR conditions in a single large string.
Concatenate this list of OR conditions with rest of your query as Criterion for your database query.
While the length of an IN phrase may have a restriction, the length of the complete SQL statement may be higher.
Of course, if the no. of customers going to appear in your query is too large, this will have unexpected issues depending on how large it is.


Understanding of differential of a curve as a vector field

Question: I have trouble understanding the definition for a geodesic curve:
Let $\gamma:I \rightarrow M$ be a curve, $M$ pseudo.Riemannian manifold.
Then $\gamma$ is geodesic, if $\nabla_{\gamma^{'}} \gamma^{'}=0$
Now, if we look at the Levi-Civita connection for example, then $\nabla: X(M) \times X(M) \rightarrow X(M)$, therefore, for the expression $\nabla_{\gamma^{'}} \gamma^{'}=0$  to make sense, $\gamma^{'}$ has to be interpreted as a vector field.
Now I see you can define a vector field in the following way: $X(p):=\gamma^{'}(t)$ where $\gamma(t)=p$ but this is only a definition on $\gamma(I)$, not on all of $M$..
Comment: https://en.wikipedia.org/wiki/Levi-Civita_connection#Derivative_along_curve has a short explanation. Intuitively, the covariant derivative of some field $X$ at a point $p$ in the direction of a vector $v$ only cares about what happens to $X$ along the direction of $v$ from $p$. I don't have a good enough grip on this to give a formal and rigorous answer, though.
Answer: There are two ways to do this.
(1) If you understand pullback connexions and bundles, you pull the tangent bundle $TM$ and the Levi-Civita connexion $\nabla$ back to $I$ via $\gamma\colon I\to M$, and the geodesic equation is defined as $(\gamma^*\nabla)_{d/dt}\dot\gamma=0$.
(2) If you don't know pullback connexions, you can simply extend $\dot\gamma$ arbitrarily to a smooth vector field $V$ on some neighbourhood of your point $p=\gamma(t)\in M$.  Then prove that $(\nabla_VV)(p)$ is independent of the choice of extension $V$, so $\nabla_{\dot\gamma}\dot\gamma=0$ makes sense.
Comment: Ok this makes sense, thank you!


How to initial exist image matrix with snap svg?

Question: Thanks lan who helped me to complete svg transform with slider Fiddle.
After some progresses I got another problem about it.If the image has exist matrix, how should I apply it to the slider at the beginning? As Fiddle example, the image behavior looks strange while I drag the slider.
$(function() {
    Snap.plugin(function(Snap, Element, Paper, global) {
        Element.prototype.updateTransform = function() {
            var newTransform = 't' + (this.data('xy').x) + ',' + (this.data('xy').y);
            newTransform += 'r' + (this.data('r'))
            newTransform += 's' + (this.data('s'))
            this.transform(newTransform)
            return this;
        }
        Element.prototype.init = function() {
            return this.data('xy', {
                    x: 0,
                    y: 0
                })
                .data('r', 0)
                .data('s', 1)
        }

    });

    function dragStart(x, y) {
        this.data('oxy', this.data('xy'));
    }

    function dragMove(dx, dy, x, y) {
        this.data('xy', {
                x: this.data('oxy').x + dx,
                y: this.data('oxy').y + dy
            })

            .updateTransform();
    }
    obj = Snap(".cat");
    obj.init();
    obj.drag(dragMove, dragStart);

    //Slider resize
    $("#slider_resize").slider({
        max: 4,
        min: 1,
        step: 0.1,
        slide: function(event, ui) {
            obj.data('s', ui.value)
                .updateTransform();
        }
    });
    //Slider rotate
    $("#slider_rotate").slider({
        max: 360,
        min: 0,
        step: 1,
        value: 0,
        slide: function(event, ui) {
            obj.data('r', ui.value)
                .updateTransform();
        }
    });

    //Reset button
    $('#reset').click(function() {
        $("#slider_resize").slider('value', 0);
        $("#slider_rotate").slider('value', 0);
        obj.init().updateTransform();
    });
});

Answer: You can store the initial transform, and then add it on each time to the start of the new transform..so relevant bits would be...
In our init function, check if we have stored an initial transform, if not store it. We only want to do this once (otherwise you can't use the reset button).
if( !this.data('origTransform') ) this.data('origTransform', this.transform())

In the update transform function, we can add the original transform to the new one which we are manipulating, e.g
this.transform( this.data('origTransform') + newTransform )

jsfiddle
This 'may' be what you want, depending on what's in that initial transform (as subsequent transforms are affected by it). It may start to get very complicated if it isn't, as matrices aren't really reversable (eg the proper translation from a matrix) otherwise, but there may be a way depending on requirements :). I would test it with an initial rotation and scale on the image and see if it does what you want (if that's what you will end up supporting).
Comment: Thanks a lot!! You saved my life :DDD. The answer almost solved the problem. I have applied it to my project. It works good except drag and move. https://youtu.be/zg2bFLzUXf4. As the demo video shows, different original matrix causes different moving situation. Is this solvable?
Comment: Sorry, I was wrong. It should be different original image width and height causes different moving situation.
Comment: If  keep dragging the image, the browser gets slower and slower.  It's really strange....Should I try translate and change image width and height without matrix to deal with this? This is what I have done https://jsfiddle.net/o497akje/7/ . But this one also has a lots of problems if I give it initial matrix. I don't know what to do now..any suggestion?
Comment: Sorry, I gave you too many messages at once. I just found browser gets slower bug. That my problem. Please ignore it.
Comment: The easiest way is to put the image inside a  element and put the drag on that, eg https://jsfiddle.net/jy9noyf5/8/
Comment: Genius!! I love you lan! Everything works find now. Thank you thank you.


Declaring Pointer to Pointer in an Array causes Segmentation Fault

Question: I'm currently working on a project where I am implementing my own Threads API.
I have a function where I aim to return a pointer to a given thread that corresponds to a unique id. I have defined it as follows:
thread *getThread(Tid id){
    thread *want = threadsArr[id]; // threadsArr is an array that keeps track of created threads
    if (want != NULL && want->id == id){
        // free(want);
        printf("Arrived here");
        return want;
    }
    else {
        for (int i = 0; i < THREAD_MAX_THREADS; i++){
            want = threadsArr[i];
            if (want == NULL){
                continue;
            }
            else {
                if (want->id == id){
                    return want;
                }
            }
        }
    }
}

I ran gdb and it says that the line thread *want = threadsArr[id]; is causing a Segmentation Fault.
I have initialized everything in a function as follows:
void
thread_init(void)
{
    int idx = 0;
    for (idx = 0; idx < THREAD_MAX_THREADS; idx++){
        threadsArr[idx] = NULL;
    }
    currunThread = (thread *)malloc(sizeof(thread));
    currunThread->id = 0;
    currunThread->status = RUNNING;
    //initialize structure pointed to by ucp to the current user context of the calling thread
    getcontext(&(currunThread->context));

    readyThreads = (queue *)malloc(sizeof(queue));
    initQ(readyThreads);

    threadsArr[currunThread->id] = currunThread;
}

I've used this method in previous programs before and haven't got Seg Faults, so I am wondering why its happening now??
Thanks in advance.
Edit: If I get rid of the return statement in getThread(Tid id), I no longer receive a Sementation Fault. But if I keep it the Segmentation fault occures when I do thread *want = threadsArr[id];.
Comment: Code is too incomplete. Please provide a [complete minimal verifiable example](https://stackoverflow.com/help/minimal-reproducible-example). One thing that should be done regardless is that `id` should be validated before using it as an array index. What value is it when the seg fault occurs?
Comment: `threadsArr[]` seems to be a global variable, is that so?
Comment: Keep removing code and confirm that you get the fault. Then give us the minimum amount of code necessary to replicate the fault. There's a good chance you'll find the issue yourself in this process.
Comment: @MukulKumar yes the threadsArr is a global variable, declared as, `thread *threadsArr[THEAD_MAX_THREADS]`
Comment: @DavidSchwartz If I get rid of the return statement in getThread(Tid id), I no longer receive a Sementation Fault. But if I keep it the Segmentation fault occures when I do thread *want = threadsArr[id];
Comment: Run your code through valgrind.  If you're mismanaging memory it will tell you where.
Answer: One possibility is the "id" variable is out of the scope:
thread *getThread(Tid id){
    if (id >= THREAD_MAX_THREADS)
        return NULL;

    thread *want = threadsArr[id];
...

And you have a missing ';' at the end of questionable line.
If threadsArr is declared as a global pointer and then malloc to an array like:
thread *threadsArr;

some_function()
{
    threadsArr = malloc(THREAD_MAX_THREADS * sizeof(thread *));
}

there is a possibility that the variable has been overwritten by some other functions, e.g. out-of-bound array write.


duplicate entry on using commerce_product_ui_product_type_save()?

Question: Reference to the code https://www.drupal.org/node/1059624#comment-4083864
 $product_type = commerce_product_ui_product_type_new();

  $product_type['type'] = 'prestation';
  $product_type['name'] = t('Prestation');
  $product_type['description'] = t('A basic prestation.');
  $product_type['is_new'] = TRUE;

  commerce_product_ui_product_type_save($product_type, FALSE);

When I execute the folllowing code given above I get a

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062
  Duplicate entry 'prestation' for key 'PRIMARY': INSERT INTO
  {commerce_product_type} (type, name, description, help, revision)
  VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1,
  :db_insert_placeholder_2, :db_insert_placeholder_3,
  :db_insert_placeholder_4); Array ( [:db_insert_placeholder_0] =>
  abcs_new15 [:db_insert_placeholder_1] => abcs new15
  [:db_insert_placeholder_2] => abcs new15 [:db_insert_placeholder_3] =>
  [:db_insert_placeholder_4] => 1 ) in drupal_write_record() (line 7239
  of
Answer: You are getting the error because you are setting $product_type['is_new'] to TRUE, which is saying to the module the product type is new, but that evidently is not true. That is what the error is telling you.
Set $product_type['is_new'] to FALSE, or don't set it at all, and you will not get any error.
If you are programmatically creating new product types, be sure you give to $product_type['type'] a unique value for each of the product types you create.
Reference

commerce_product_ui_product_type_save()
Comment: You are clearly using for `$product_type['type']` a value that is already present in the table used by the module, or the database engine would not return an error saying "Duplicate entry 'prestation'."
Comment: I am giving a unique value to $product_type['type']
Comment: also $product_type['new'] = TRUE since I need to add a new product type programmatically


Unable to use autossh in background even with absolute path

Question: I would love to set autossh to run at boot adding it to /etc/rc.local.
This command works:
autossh -i /root/.ssh/id_rsa -R 2522:localhost:22 user@address
But, if I add the -f option
autossh -f -i /root/.ssh/id_rsa -R 2522:localhost:22 user@address
The ssh session is not started.
As you can see, I'm using an absolute path for my identity file, so this seems to be a different problem from the one stated here: autossh in background does not work
From /var/log/syslog:
Oct 18 11:08:39 raspberrypi autossh[2417]: starting ssh (count 1)
Oct 18 11:08:39 raspberrypi autossh[2417]: ssh child pid is 2418
Oct 18 11:08:39 raspberrypi autossh[2417]: ssh exited with status 0; autossh exiting

I'm using it with debian wheezy on a raspberry pi, autossh version 1.4c.
Could it be that it's passing the -f option to ssh instead?
Answer: When you start autossh without -f, you get a shell. While the shell is working, you get port forwarding. After you logout, ssh terminates with exit code 0 and autossh knows it does not need to launch ssh session again.
When you start autossh with -f, it passes -f to ssh too. ssh is then running in the background and does not give you a shell. As you did not specify any other flags or remote command, ssh exits immediately with status 0 (nothing to do), and autossh does not start it over.
Just add -N option to avoid it:
-N      Do not execute a remote command.  This is useful for just forwarding
        ports (protocol version 2 only)

Like this:
autossh -f -N -i /root/.ssh/id_rsa -R 2522:localhost:22 user@address

Comment: This seems to work, but contradicts what the man page says:

`-f  causes autossh to drop to the background before running ssh. The -f flag is stripped from arguments passed to ssh.`


Meteor app crash trying to secure it

Question: This app was working fine till I tried to "secure it":

remove insecure
remvoe autopublish
add accounts-ui accounts-password
made some changes to the code as below
//both.js

FooterButtons = new Mongo.Collection('footerButtons');
Tasks1 = new Mongo.Collection('tasks1');
Tasks = new Mongo.Collection('tasks');
Tasks1.allow({
  insert: function (userId, doc) {
    return userId;
  }
});

 // deny anyone that tries to update the document userId
Posts.deny({
    update: function (userId, docs, fields, modifier) {
   // can't change owners
   return _.contains(fields, 'userId');
   }
});

//server.js
Meteor.publish('tasks', function(){
  return Tasks.find({userId: this.userId});
 });

 Meteor.publish('tasks1', function(){
   return Tasks1.find({userId: this.userId});
 });

Meteor.publish('footerButtons', function(){
   return FooterButtons.find({userId: this.userId});
 });

Now the app is crashing with browser saying

/Users/myName/.meteor/packages/meteor-tool/.184.108.40.206b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:245
                          throw(ex);
                                ^
  ReferenceError: Posts is not defined
      at both/both.js:15:1
      at both/both.js:20:4
      at /Users/myName/Documents/meteor/microscope/.meteor/local/build/programs/server/boot.js:242:10
      at Array.forEach (native)
      at Function..each..forEach (/Users/empl1/.meteor/packages/meteor-tool/.126.96.36.199b51q9m++os.osx.x86_64+web.browser+web.cordova/mt-os.osx.x86_64/dev_bundle/server-lib/node_modules/underscore/underscore.js:79:11)
      at /Users/myName/Documents/meteor/microscope/.meteor/local/build/programs/server/boot.js:137:5
  Exited with code: 8
  Your application is crashing. Waiting for file change.

Why is it crashing and how should it be fixed? Thanks
Answer: You could start here:
ReferenceError: Posts is not defined at both/both.js:15:1 at both/both.js
Your are missing a collection assignment for the Posts collection.
Answer: Based on your error message, it's because you're not defining the Posts collection.  Add this line of code:
Posts = new Mongo.Collection('posts');

That will remove the error, but I think you probably copied/pasted this code from an example and want to enact a deny rule on a different collection?
Answer: I think you already have the post collection definition somewhere in the project, because it worked before you removed the autopublish package. You should find it and put all the collection definition file into lib folder instead of any randomly named folder for the shake of order loading. Read more about the meteor app structure in here

Meteor app crash trying to secure it


PostgreSQL lots of writes

Question: I am using postgreSQL for a scientific application (unsupervised clustering). The python program is multi-threaded so that each thread manages its own postmaster process (one per core). Hence, their is a lot of concurrency.
Each thread-process loop infinitely though two SQL queries. The first is for reading, the second is for writing. The read operation considers 500 time the amount of rows the write operation considers.
Here is the output of dstat:
<code>----total-cpu-usage---- ------memory-usage----- -dsk/total- --paging-- --io/total-
usr sys idl wai hiq siq| used  buff  cach  free| read  writ| in   out | read writ
  4   0  32  64   0   0|3599M   63M   57G 1893M|1524k   16M|  0    0 |  98  2046
  1   0  35  64   0   0|3599M   63M   57G 1892M|1204k   17M|  0    0 |  68  2062
  2   0  32  66   0   0|3599M   63M   57G 1890M|1132k   17M|  0    0 |  62  2033
  2   1  32  65   0   0|3599M   63M   57G 1904M|1236k   18M|  0    0 |  80  1994
  2   0  31  67   0   0|3599M   63M   57G 1903M|1312k   16M|  0    0 |  70  1900
  2   0  37  60   0   0|3599M   63M   57G 1899M|1116k   15M|  0    0 |  71  1594
  2   1  37  60   0   0|3599M   63M   57G 1898M| 448k   17M|  0    0 |  39  2001
  2   0  25  72   0   0|3599M   63M   57G 1896M|1192k   17M|  0    0 |  78  1946
  1   0  40  58   0   0|3599M   63M   57G 1895M| 432k   15M|  0    0 |  38  1937
</code>
I am pretty sure I could write more often than that for I have seen it write up to 110-140M on dstat. How can I optimize this process?
Answer: I am the author of dstat and a system engineer. I noticed a 60% avg iowait time. Given your output I would say that your disks are quite busy. You could try the new --disk-util plugin option in recent dstat versions.
This will show you the utilization rate of your disk(s), and I would expect it to be close to 100% for the disk(s) you are using. So given your particular I/O pattern, your disk is sufficiently busy handling read or write requests.
Why is this below benchmark numbers ? Because often when you benchmark disk throughput, you are stressing your disk in a certain pattern that is optimal for your disk/caches (like eg. linear reads or writes with large blocksizes with a single thread) while in your current workload the particular patterns may be less optimal (random reads or writes with small or varied blocksizes with multiple threads asking for resources).
This difference in pattern can make a huge difference in throughput. And getting a better throughput during a real workload means you will have to do benchmarking with a workload that matches more closer your real workload, to see what the maximum is you can achieve under those conditions. Or you can influence the real workload by changing your design (eg. align blocksizes in your application with filesystem/disk subsystem) or improve caching and/or read-ahead.
There's no simple way to fix this without analyzing what your workload looks like.
Comment: Very good explanation. Just want to point out that throwing faster hard disks on IO bound databases tends to be a quick and dirty way to improve performance. SSD are especially nice for this.
Answer: I'd say it very much depends on the code of the program, it's possibly waiting for a worker thread to resynchronize before starting the next operation.
Does the read operation involve the same data that is being written, if so, you may get concurrency/race conditions if you stop it from waiting for the other threads to catch up.
Probably best to move this to stack overflow.
Comment: No, I am positive that it has nothing to do with python. The python app is only there to call SQL Functions or to manage the 8 postmasters.
Answer: Actually it may well be Python code level. Python uses a Global Interpreter Lock to handle threading and you could be bumping against the lock. There is a post on StackOverflow about the GIL and multi-core systems.
I would look into using a process per postmaster and a "master master" if needed to manage those process, or possibly Twisted to get around the GIL.
Comment: No I can confirm that this is not the problem. I know about the GIL. Other workloads (longer Selects with less Writes) can use up all RAM. What python does in nothing time-compared to what each postmaster does. Python uses less than 1% of a CPU-core. Thanks for the thought though :D

PostgreSQL lots of writes


MVVM and Dagger 2 - Benefits

Question: We are developing new Android application.
We are trying to use MVVM and Dagger 2.  What are all the benefits of using Dagger 2 if we use MVVM?
Answer: MVVM and dagger 2 are different things. Dagger 2 implements dependency injection pattern, which is really beneficial for testing, because you can test dependent classes without instantiating classes that require those dependent classes. Also makes code more readable and easy to modify later.
Comment: In terms of ViewModel (Arch Components wise), if you're creating ViewModel Factories is there any reason to use it? seems like the factory can be treated as a module and essentially swapped out/ mocked in that way.

MVVM and Dagger 2 - Benefits


Deterministic sampling from discrete distribution

Question: I'm working on a generalization of the Min-Hash algorithm to allow the meaningful comparison of ordered values such as integers. The core trick is to use deterministic randomness as a replacement for hash-functions.
However, to get this thing to work I need a way to deterministically sample from a discrete distribution. I.e. if I repeatedly draw samples from a discrete distribution I need to get the same set of values for every call.
Library functions like sample of the languageR use standard sampling algorithms which typically use uniform-sampling from the cumulative distribution. Hence, they need some pseudo-random numbers. By seeding and resetting this random number generator prior to each call, it is possible to cut out the randomness and make things deterministic. sample then behaves like a mathematical function.
Consider following example:
value:    a       b      c     d    e     f     g      h     i    j
prob:    0.070 0.0774 0.083 0.090 0.096 0.103 0.109 0.116 0.122 0.129

setSeed(1)
sample(value,5, prob, replace = TRUE)
>>> a, c, e, j, a
setSeed(1)
sample(value,5, prob, replace = TRUE)
>>> a, c, e, j, a

However, in case the distribution changes slightly (i.e another value k gets inserted) the values returned from these standard sampling algorithms change drastically.
setSeed(1)
sample(value, 5, prob, replace = TRUE)
>>> a, c, e, j, a
insert('k')
>>> value:    a     b     c     d    e     f     g      h     i     j    k
>>> prob:  0.062 0.068 0.073 0.079 0.085 0.090 0.096 0.102 0.107 0.113 0.119
setSeed(1)
sample(value,5, prob, replace = TRUE)
>>> b, d, f, k, b

So, despite the distribution changed only marginally, the drawn sample changed broadly. However, I would like to see just a minor change, something like
>>> a, c, e, j, a   <-- initial sample from initial density
>>> a, c, k, j, a   <-- desired sample after small density change
>>> b, d, f, k, b   <-- actual sample after small density change

A slow but working algorithm would be to 'simulate' this sampling by using 10.000 bins. Each bin contains a single 'value'. The number of bins of a certain value corresponds to the probability of the value in the distribution. Drawing from this simulation then works by drawing integers i 1-10.000 and returning the value of the bin with number i. If bins get replaced with another value, the drawn sample changes only slightly, because on every call the same bins get selected.
So the problems with standard algorithms is, that they typically sort/rearrange these bins to get the speedup. However, thats not possible in that case.
Is there a way to sample from the density distribution itself while ensuring that the change in the drawn sample is similar to the change of the distribution?
Comment: `sample` samples values (pseudo-) **randomly**, so you should expect the output values to be rather random than predictable... `sample` does not use any CDF, it just returns random integers. Also: what do you mean by sampling from density?
Comment: Sorry but your code and your question are unclear. `sample` works in a very simple manner: it samples values (pseudo-)randomly, by default each value with the same probability, but if you use `prob` parameter it uses different probabilities as defined by user. Change in output is the expected behavior of this function if you change the input. Refer to the function documentation if it is not clear.
Comment: I think the question remains unclear. I have the feeling that the point you are leaning to is "why don't small changes to the sampling frame result in small changes in the sample drawn, if we use the same random numbers for the selection?" But if you change the $n$ that you are drawing from, then clearly some change is inevitable. Perhaps what you really need to do is clarify what kind of change is concerning you?
Comment: **WHAT** property? You have not defined what is the expected behavior of the algorithm that you are describing.
Comment: I updated the post
Comment: The change of the drawn sample with respect to the change of the distribution depends on the algorithm used for sampling.

A slow but working algorithm would be to 'simulate' this sampling by using 10.000 bins.  Each bin contains a single 'value'. The number of bins of a certain value corresponds to the probability of the value in the distribution.
Drawing from this simulation then works by drawing integers `i` `1-10.000` and returning the value of the bin with number `i`

If bins get replaced with an other value, the drawn sample changes only slightly.
Comment: What is "similar" and "drastically different" in relation to random samples? I would say any two random samples from the same distribution must be similar or even "the same" in some sense.
Comment: Similarity could be described by _matching letters in drawn samples_.
Samples are not really random in that sense. Samples in this context are more like the output of functions of the distribution. So the same distribution always results in the same sample. A slightly different distribution, results in a slightly different sample. Just like continuity on functions.
Answer: One general way to generate similar sequences for distributions with similar probabilities is the following.  Suppose $A$ is an ordered finite alphabet $(a,b,c,\ldots)$ with a probability distribution $p_A$.  To draw a value at random from $A$, generate a vector of independent uniform variates $\mathbf{U}=(U_a, U_b, U_c, \ldots)$.  If $U_a \le p_A(a)$, choose $a$.  Otherwise, recursively draw a value from the remaining letters $A^\prime = A-\{a\} = (b,c,\ldots)$ using the vector $\mathbf{U}^\prime=(U_b, U_c, \ldots)$ and the probabilities $$p_{A^\prime}(b) = \frac{p_A(b)}{1-p_A(a)},\ p_{A^\prime}(c) = \frac{p_A(c)}{1-p_A(a)},$$ etc.
You can re-use the same vector $\mathbf{U}$ for any other distribution on $A$.
Using this method, the expected frequency with which the same letter would be drawn from distributions $p_A$ and $q_A$ is the frequency with which $a$ would be drawn from both distributions, equal to the smaller of $p_A(a)$ and $q_A(a)$, plus the expected frequency with which the same letter would be drawn from $(b,c,\ldots)$, conditional on $a$ not being drawn from either distribution.
This method is the best you can do by assigning each letter to its own connected interval.  With additional work it's possible to make the two sequences agree even more frequently, but you would have to assign the extra letter "k" to a complicated subset of $(0,1]$.

Here is R code to generate n symbols from an alphabet with probability vector prob.
s <- function(n, prob) {
  k <- length(prob)
  q <- prob / rev(cumsum(rev(prob)))
  u <- matrix(runif(n*k), nrow=k, byrow=TRUE) < q
  apply(u, 2, function(x) match(TRUE, x))
}

Let's generate samples of size 10,000 from distributions like those in the question.  The output shows the first 60 draws from each, using the same starting seed.  They are remarkably similar.
prob <- c(75, 77, 83, 90, 96, 103, 109, 116, 122, 129, rep(0, 16))
prob.k <- prob
prob.k[11] <- 0.119/(1-0.119)*sum(prob)

seed <- 17
N <- 1e4
set.seed(seed); x <- letters[s(N, prob)]
set.seed(seed); x.k <- letters[s(N, prob.k)]

rbind(First=paste0(head(x, 60), collapse=""),
      Second=paste0(head(x.k, 60), collapse=""))

Here it is:
First  "geifgefhiifafbhfhijgfcdiebjfjegajgggidghchhjjfgdjheicbhbjica"
Second "geifgefhiikafbhgiikifcdiibjfkjkakiggkdghchijjfgdkhhkkbhbkkcj"

You may check that the actual frequencies are close to the intended ones:
rbind(First=c(table(x), k=0), Second=table(x.k))

This output is
         a   b   c   d   e    f    g    h    i    j    k
First  755 775 808 842 995 1068 1111 1184 1206 1256    0
Second 657 666 693 739 872  955  973 1056 1074 1144 1171

The degree of similarity (that is, proportion of time the two sequences are expected to agree) is readily computed recursively.
similarity <- function(x, y) {
  if (min(length(x), length(y)) == 0) return (0)
  a <- min(x[1], y[1])
  x.s <- sum(x[-1])
  y.s <- sum(y[-1])
  if (x.s > 0 & y.s > 0) {
    b <- max(x[1], y[1])
    x <- x[-1]/sum(x[-1])
    y <- y[-1]/sum(y[-1])
    b <- (1-b) * similarity(x, y)
  } else {
    b <- 0
  }
  return (a + b)
}
similarity(prob/sum(prob), prob.k/sum(prob.k))

The output is
0.7568941

In fact, in this simulation the observed frequency was close to that:
mean(x.k == x)

[1] 0.754

Comment: This approach looks really promising. I'll give it a try. Thanks a lot!
Comment: After testing it I stumbled upon a major flaw.

If new values at the left side of the distribution (i.e. at the left of the `a`) are inserted, the change of the sample is again broadly. Just like in other algorithms the problem is the uniform sampling from the CDF.
This flaw can be omitted, by knowing the whole alphabet of values beforehand and setting their probability to `0` . However, that's not straight forward in my case.
Comment: TLDR: removing the `a` results in a big change, but setting the probability to `0` is fine.


Does a closed form exist for this specific triangle of numbers?

Question: As part of a bigger project, I need to find a closed form for this triangle of numbers below:

Note that in each incremental row (let's label them $n=1,2,3,...$), a new sequence starts with $\dfrac{1}{\pi^2},\dfrac{1}{\pi^4},\dfrac{1}{\pi^6},...$ multiplied by a rational. These sequences are the columns of the triangle (let's label them $k=0,1,2,3..$). I already did manage to find for cells $TT[n,k]$ (the $\frac{1}{\pi^{2m}}$'s are left out for simplicity, I am primarily after the rationals):
$TT[n,0]= \small 2^{2n-0}\cdot\dfrac{1}{(2n-1)}$
$TT[n,1]= \small 2^{2n-1}\cdot\dfrac{n}{(2n-1)(2n-2)(2n-3)}$
$TT[n,2]= \small 2^{2n-2}\cdot\dfrac{n\,(3n-1)}{(2n-1)(2n-2)(2n-3)(2n-4)(2n-5)}$
$TT[n,3]= \small 2^{2n-3}\cdot\dfrac{n\,(15n^2-15n+4)}{(2n-1)(2n-2)(2n-3)(2n-4)(2n-5)(2n-6))(2n-7)}$ (with help here).
$TT[n,4]= \small 2^{2n-4}\cdot\dfrac{n\,(???)}{(2n-1)(2n-2)(2n-3)(2n-4)(2n-5)(2n-6))(2n-7)(2n-8)(2n-9)}$
It might also be the case that the cells in this triangle could be constructed top-down with a formula like: $TT[n+1,k+1] = x(n)\cdot[TT[n,k+1] \pm y(n)\cdot TT[n,k]$, however I have not been able to spot the rhythm.
I'd be highly grateful for any help on how to potentially progress this.
Thanks!
Comment: It will likely help to describe this "bigger project" and where these numbers are coming from. Without that, this community would be no more help than OEIS. If you don't think this is necessary, that's fine too, but just so you know more help may come from that direction.
Comment: Appreciate and understand the feedback, Bob. I was actually planning to post the full context of the question, however it consists of a long series of closed forms that I have already found. I actually just need this final one to complete the story. If it doesn't get cracked by the community in the next few days, I'll follow you advice and post the full context.
Answer: Looking into $TT[n,k]$ formulas, it seems that they follow a pattern,
So judging on the previous ones that you managed to find a closed form which are $TT[n,0],TT[n,1],TT[n,2],TT[n,3]$, its easy to see that $???$ you are looking for is a cubic equation of the form $a x^3 +b x^2 +c x +d$.
And since you have a lot of data (at least four equations with four variables $a,b,c,d$) you can easily find them (Using Wolfram) or any other Software.
So Assuming all the above i found using Wolfram|Alpha that  the $???$ is equal to the cubic polynomial $105n^3-210n^2+147n-34$.
Testing the result with your data and i found a perfect match.
Comment: I will post the full version soon indeed (probably on MO, will link it to here). You put me on the right track, Ahmad. Thanks again!
Comment: Excellent Ahmad and many thanks! Did you spot any other pattern in the data or maybe in the polynomials (I was hoping they might be factored in some smart way)?
Comment: @Agno the coefficient of the leading power in the polynomials have the sequence $1,3,15,105,945,\cdots$ which is $\prod 2k-1 $ so the $TT[n,5]$ will have the leading term $945n^4$ but other than that i could not find any thing useful.
Comment: I just did find something! Check out this OEIS series: https://oeis.org/A094665. I believe it has all the coefficients of all the polynomials. If right, I conjecture the TT[n,5] polynomial is: $945 n^4-3150 n^3+4095 n^2-2370 n+496$. Just checked and it works perfectly :-) This is even better than I expected.
Comment: @Agno very good, i think you found what you are looking for, at least now you can attach your project for related math online.
Comment: Have now posted the full question here. https://mathoverflow.net/questions/273375/another-integral-that-has-a-closed-form-involving-finite-series-of-zeta2k1 Fingers crossed.


how to add tag suggestion functionality to my blogware?

Question: I am creating a blogging functionality to my website running on a cms.
My requirement is this:
When a person has written an article, he must automatically provided with 'suggested tags'. These words must come from the article. How can I implement this functionality?
I thought some ideas. Like:

Suggest words which are the longest. Using this, I can filter out 'a', 'of', 'my' etc but not 'because'.
Create a blacklist of words. But I couldn't find any such ready-made list and creating such a list will take a very long time.

So, any other ideas?
Answer: You could do Bayesian classification and see what happens. Here's some example code.
UPDATE: This presupposes that you have some tags for the classifier to choose from. Here is a simple algorithm for extracting keywords from text if you need to initialize your list of tags.


Grid com gráficos que substituem um ao outro

Question: Estou criando uma página com um grid que deve ter 3 gráficos, um em cada posição do grid. Mas todos os gráficos ficam em uma mesma posição do grid e acabam substituindo os gráficos anteriores (o gráfico 2 substitui o gráfico 1, e o 3 substitui os gráficos 1 e 2). Como corrijo isso? Seguem os códigos abaixo:

fieldset{
border: 1px solid #6f3658;
}

#fieldG {
font-family: 'Century Gothic';
border-radius: 5px;
text-align: center;
width: 900px;
margin: auto;
}

.titGraf {
font-family: 'Bahnschrift Condensed';
text-align: center;
padding: auto;
}
.row {
display: grid;
grid-template-columns: 33.3% 33.3% 33.3%;
grid-template-rows: auto auto auto;
grid-template-areas: "." "." ".";
}
#g1.highcharts-figure, #G1.highcharts-data-table table {
min-width: 310px;
max-width: 800px;
margin: 1em auto;
}

#G1#container1 {
height: 400px;
}

#G1.highcharts-data-table table {
  font-family: 'Bahnschrift';
  border-collapse: collapse;
  border: 1px solid #EBEBEB;
  margin: 10px auto;
  text-align: center;
  width: 100%;
  max-width: 500px;
}

#G1.highcharts-data-table th {
  font-weight: 600;
padding: 0.5em;
}
#G1.highcharts-data-table td, #G1.highcharts-data-table th, #G1.highcharts-data-table caption {
padding: 0.5em;
}

#G1.highcharts-data-table thead tr, #G1.highcharts-data-table tr:nth-child(even) {
 background: #f8f8f8;
}

#G1.highcharts-data-table tr:hover {
background: #f1f7ff;
}

#G2.highcharts-figure, #G2.highcharts-data-table table {
min-width: 320px;
max-width: 660px;
margin: 1em auto;
}

#G2.highcharts-data-table table {
  font-family: 'Bahnschrift';
  border-collapse: collapse;
  border: 1px solid #EBEBEB;
  margin: 10px auto;
  text-align: center;
  width: 100%;
  max-width: 500px;
 }

#G2.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: #555;
}
#G2.highcharts-data-table th {
  font-weight: 600;
padding: 0.5em;
}
#G2.highcharts-data-table td, #G2.highcharts-data-table th, #G2.highcharts-data-table caption {
padding: 0.5em;
}
#G2.highcharts-data-table thead tr, #G2.highcharts-data-table tr:nth-child(even) {
background: #f8f8f8;
}
#G2.highcharts-data-table tr:hover {
background: #f1f7ff;
}

#G3.highcharts-figure, .highcharts-data-table table {
min-width: 310px;
max-width: 800px;
margin: 1em auto;
}

#G3#container {
height: 400px;
}

#G3.highcharts-data-table table {
  font-family: Verdana, sans-serif;
  border-collapse: collapse;
  border: 1px solid #EBEBEB;
  margin: 10px auto;
  text-align: center;
  width: 100%;
  max-width: 500px;
}
#G3.highcharts-data-table caption {
 padding: 1em 0;
 font-size: 1.2em;
 color: #555;
}
#G3.highcharts-data-table th {
   font-weight: 600;
 padding: 0.5em;
}

#G3.highcharts-data-table td, #G3.highcharts-data-table th, #G3.highcharts-data-table caption {
 padding: 0.5em;
}
#G3.highcharts-data-table thead tr, #G3.highcharts-data-table tr:nth-child(even) {
 background: #f8f8f8;
}

#G3.highcharts-data-table tr:hover {
background: #f1f7ff;
}
<!DOCTYPE html>

<meta charset="utf-8">

    <link rel="stylesheet" type="text/css" href="Teste2.css" />

    <fieldset id="fieldG">
        <legend>Bem vindo</legend>

                    <fieldset class="graf" id="graf1">
                        <legend class="titGraf" id="grafico1">Gráfico 1</legend>

                    </fieldset>

                    <fieldset class="graf" id="graf2">
                        <legend class="titGraf" id="grafico2">Gráfico 2</legend>

                    </fieldset>

                    <fieldset class="graf" id="graf3">
                        <legend class="titGraf" id="grafico3">Gráfico 3</legend>

                    </fieldset>

    </fieldset>

Comment: Não precisa colocar "resolvido" no título. Sei que é comum em muitos fóruns, mas aqui funciona diferente. Você já marcou uma resposta como aceita (quando clicou no ✓) e isso já é o suficiente para indicar que foi resolvido.
Answer: Você está declarando os 3 gráficos na mesma div usando Highcharts.chart('container',. Se trocar isso para o nome da div que você quer que o gráfico apareça vai funcionar.
Tem alguns outros erros no seu código de copiar e colar. Tem duas divs com o mesmo id container e você não precisa declarar os scripts em cada div, se pode deixar tudo no header, declarando só uma vez.
Comment: Era isso, mesmo. Resolvido.
Obrigado pela explicação.


JAVA JPA ValidationException

Question: I have a problem with one JPA exception when I want to insert row in database I got this exception:
javax.persistence.RollbackException: Exception [EclipseLink-7124] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ValidationException
Exception Description: An unsuccessful writeChanges() has been called on this UnitOfWork.  Given the danger that partial changes have been written to the datastore but not rolled back (if inside external transaction), the only supported operations now are release, global transaction rollback, any non-object level query or SQLCall execution.  The operation commit was attempted.
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:102)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commit(EntityTransactionImpl.java:63)
    at database.TarpineManager.commitTransaction(TarpineManager.java:23)
    at gijos.InsertToDB.insertLink(InsertToDB.java:57)
    at gijos.LygiagretusIrasymas.run(LygiagretusIrasymas.java:26)
Caused by: Exception [EclipseLink-7124] (Eclipse Persistence Services - 2.3.2.v20111125-r10461): org.eclipse.persistence.exceptions.ValidationException
Exception Description: An unsuccessful writeChanges() has been called on this UnitOfWork.  Given the danger that partial changes have been written to the datastore but not rolled back (if inside external transaction), the only supported operations now are release, global transaction rollback, any non-object level query or SQLCall execution.  The operation commit was attempted.
    at org.eclipse.persistence.exceptions.ValidationException.unitOfWorkAfterWriteChangesFailed(ValidationException.java:1802)
    at org.eclipse.persistence.internal.sessions.UnitOfWorkImpl.commitAndResume(UnitOfWorkImpl.java:1119)
    at org.eclipse.persistence.internal.jpa.transaction.EntityTransactionImpl.commitInternal(EntityTransactionImpl.java:84)
    ... 4 more

Comment: What's your question? You are clearly attempting to insert to some column something that doesn't belong there. Without some code to see, we can't tell you what.
Answer:
You have a javax.persistence.RollbackException
This is only raised when EntityTransaction.commit() fails
The underlying cause within EclipseLink is org.eclipse.persistence.exceptions.ValidationException An unsuccessful writeChanges() has been called on this UnitOfWork.
Given the danger that partial changes have been written to the
datastore but not rolled back (if inside external transaction), the
only supported operations now are release, global transaction
rollback, any non-object level query or SQLCall execution.  The
operation commit was attempted.

You need to provide some code or some description of your transactions up to this point.
I think maybe you:
 1. started a transaction, updated/persisted data, failed on the commit, received a RollbackException
 2. continued to use the same entity objects in the same persistence context and received another RollbackException.  This is illegal.  The first time you receive a RollbackException, you must clear the persistence context and start over:
If this is try: use EntityManager.clear() in your error handling code, then re-retrieve objects before you start a new transaction.

JAVA JPA ValidationException


Rails: What is the location option for in the render method

Question: Hey I am wondering what the location option for the render method in rails is. The docs here http://guides.rubyonrails.org/layouts_and_rendering.html states:
"You can use the :location option to set the HTTP Location header:"
But I have no idea why you would do this, or what you would use this for.
Answer: Actually location option is used to redirect to a new resource as part of processing the request. For example,
 render :xml => post.to_xml, :status => :created, :location => post_url(post)

is telling the recipient that a XML file for the post is created and you will get this from post_url(post). Hence GO THERE ;)
render method does this by setting the Location option in response object
... ... ...
if location = options[:location]
    response.headers["Location"] = url_for(location)
end
... ... ...

You can find details about Location header here    http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.30.
Comment: oh ok. so for example if the client did a ajax call to the server, and then in the response, location was defined to some url, then the ajax is supposed to (according to the response) go the url specified by the location. Otherwise if there is no location, then done
Comment: Yes... Exactly!!
Though I didnt try with an Ajax call but should work like that!!
Comment: What about during an ajax request? Why would you want to set the location header? Wouldn't it redirect from the ajax call?
Answer: The Location header is for redirecting the page.


Utilizando return de outra classe

Question: Bom dia amigos, Estou tentando implementar esse código mas está dando erro.
 public int totalFaltasAluno(int matricula) {
        int totalFaltas = 0;
        for(Integer item : faltas){
            if(item == matricula){
                totalFaltas++;
            }
        }
        return   totalFaltas;

 public boolean verificarReprovacaoPorFalta(int matricula) {
    int presenca = 90 - totalFaltas;//90 são os dias de aulas
    if (presenca  < (90-0.75-90));// presenca menor que 75% de 90 dias de aulas
         return true;
}

erro:
    Exception in thread "main" java.lang.Error: Unresolved compilation
    problem:
    totalFaltas cannot be resolved to a variable

at Gerenciar_frequencia.Pauta.verificarReprovacaoPorFalta(Pauta.java:45)

Classe main está assim.
 if (pauta.verificarReprovacaoPorFalta(aluno.getMatricula())){
            System.out.println(" Presença: reprovado");
        }
        else{
            System.out.println(" Presença: normal");
        }

Answer: Você precisa chamar o método para fazer o uso do mesmo. Deixo abaixo um exemplo comentado:
 public int totalFaltasAluno(int matricula) {
        int totalFaltas = 0;
        for(Integer item : faltas){
            if(item == matricula){
                totalFaltas++;
            }
        }
        return   totalFaltas;
 }
 public boolean verificarReprovacaoPorFalta(int matricula) {
    // aplicando regra de 3 para saber o percentual dessa quantidade de faltas
    double percentualDePresenca = ((90 - totalFaltasAluno(matricula)) * 100) / 90;

    // comparando o percentual para ver se é menor que 75% de 90 dias.
    return (percentualDePresenca < 75);
 }

Não entendi muito bem a sua lógica para o método totalFaltasAluno mas para resolver o problema mencionado, basta que você chame o método ao invés da variável pois a mesma não existe na definição da classe apenas localmente no método.
Comment: obrigado pela resposta, acrescentei uma parte da classe main, não consegui implementar , o retorno é sempre fica  "true".
Comment: para ficar sempre true a presença está sendo menor que (90-0.75-90) = -0.75..
Comment: Editei o algoritmo, se vc quer 75% de 90 é só mutiplicar 90 por 0.75 e não subtrair. Para saber quantos % a quantidade X de faltas representa em 90 é necessário fazer uso da regra de 3. Editei o algoritmo.
Comment: hiago Souza, está retornando true sempre, mas obrigado.
Comment: Que valor tem no totalFaltas?
Comment: Agora que notei o nome do método atualizei as rotinas..
Comment: Tenho 3 totais de faltas , são 27, 6, 0
Comment: Tente novamente, editei o script... Eu me confundi aqui
Comment: obrigado mais uma vez Hiago, mas está retornando true novamente.
Comment: Vamos [continuar esta discussão no chat](https://chat.stackexchange.com/rooms/93259/discussion-between-bruno-and-hiago-souza).

Utilizando return de outra classe


How to fix the useless 'favicon.ico page not found' log message?

Question: Why is Drupal looking for it in the root dir in first place? The favicon is always placed inside theme at sites/all/themes/mytheme/, but it's looking for favicon.ico inside home / dir.
When browsing website it spams the log error message in the Recent log messages page.
Comment: Can you please apply [patch](https://www.drupal.org/files/issues/favicon404-174940-92.patch) for error message to remove.
Answer: It's not really a Drupal bug, but a browser bug (I think Firefox might be a culprit)
The best way I found to fix it is to put:
<Location /favicon.ico>
  ErrorDocument 404 "No favicon"
</Location>

This code should work in your vhost file or should also work in the .htaccess file, see http://httpd.apache.org/docs/2.4/sections.html for more details
Comment: Although adding this to .htaccess gives 500 Internal Server error.
Comment: It's a feature (not a bug) of all major browsers - if the page lacks an explicit favicon set in meta, the browser will query domain/favion.ico by convention. https://en.wikipedia.org/wiki/Favicon#How_to_use has more details
Comment: Either way it's not a Drupal bug :)


Convergence of the series $ \sum\limits_{n=3}^\infty \frac {1}{n\log^2n} $

Question: I want to show that
$$ \sum_{n=3}^\infty \frac {1}{n\log^2n} $$
converges.
My work:
Since for n = 3 to infinity, the summands are positive, then we know that the sequence of partial sums,
$$S_N =  \sum_{n=3}^N \frac {1}{n\log^2n},$$
is monotonically increasing.
So, it remains to show that $S_N$ is bounded.
Can I get a tip on how to show the boundedness?
Thanks,
Comment: Do you know about the integral test for convergence of a series?
Comment: Integral test with $f(x) = \frac{1}{x\log^2(x)}$ and using the substitution $u = \log(x) \implies \int f(x)dx = \int \frac{du}{u^2}$.
Comment: Hi @User, thanks so much for the hint - I will try it now.  I didn't think of it because I noticed that log^2 (on Wolfram Alpha) does not behave monotonically -- but then $log_e(3) > 1$, and since the series does start at 3, I can be sure that the summands are monotonically decreasing (to zero), so the test is applicable.
Comment: Thanks so much for the hint @Winther :-)
Answer: As indicated in the comments, you can use the integral test.  Alternatively, you can use the Cauchy condensation test: if $a_n$ is non-negative and non-increasing,  then $$\sum a_n \mbox{  converges}\Leftrightarrow\sum 2^n a_{2^n} \mbox{  converges}$$ In your case, you get
$$\sum_{n=3}^{+\infty}2^n\frac{1}{2^n(\ln{2^n})^2}=\frac{1}{\ln^2{2}}\sum_{n=3}^{+\infty}\frac{1}{n^2}$$
which converges.
Comment: Very cool answer @Capublanca - thanks so much :-).
Comment: For completeness it is worth noting that the convergence equivalence  is true if $a_n$ is positive and non-increasing. Which it is here.
Comment: Thank you Simon! I've edited my answer.


Subspace for 2x2 matrix

Question: Consider the set of S of 2x2 matricies $\begin{bmatrix}a&b\\c&0\end{bmatrix}$ such that a +2b+3c = 0.
Then S is 2D subspace of M2x2.
How do you get S is a 2 dimensional subspace of M2x2. I don't understand this. How do you determine this is 2 dimensional, there are no leading ones to base this of
Answer: Since $a=-(2b+3c)$, we can write the matrices as $$\begin{bmatrix}-(2b+3c) & b\\c & 0\end{bmatrix}=b\begin{bmatrix}-2&1\\0&0\end{bmatrix}+c\begin{bmatrix}-3&0\\1&0\end{bmatrix}$$ The two matrices above span the subspace (assuming you have verified that it is a subspace).
It is two-dimensional because two "vectors" (matrices in this case) span the "vector space." The dimension of a vector space is the number of vectors which make up a basis for that space. You can also think of it as "degrees of freedom." In this instance we are effectively dealing with two variables - there are three, but the last one (I chose a) is determined by the other two.
Comment: May I add the (obvious) requirement that the OP needs to check that the two matrices that span $S$ are linearly independent.
Comment: @Martin Argerami Yes, thank you.
Comment: I don't understand how it is 2 dimensional subspace of M2x2. My main question was how do you determine what dimensional subspace it is. For example why is it not a 1D or 3D subspace.
Comment: @user137673: what's the definition of dimension?
Answer: One way to think about it is that you have two conditions. One of them being $a+2b+3c = 0$ and the other being that the element in the $(2,2)$ position is equal to zero. If your matrix was$\begin{bmatrix}a&b\\c&d\end{bmatrix}$ the second condition would be $d = 0$. Because you have two conditions on a four dimensional space your subspace should have dimension $4-2 = 2$.
To continue the argument, write your matrix in the subspace as $x_1e_1 + x_2e_2 + x_3e_3+x_4e_4$ where:
$$ e_1 = \left( \begin{array}{ccc}1 & 0  \\ 0 & 0  \\ \end{array} \right)$$
$$ e_2 = \left( \begin{array}{ccc}0 & 1  \\ 0 & 0  \\ \end{array} \right)$$
$$ e_3 = \left( \begin{array}{ccc}0 & 0  \\ 1 & 0  \\ \end{array} \right)$$
$$ e_4 = \left( \begin{array}{ccc}0 & 0  \\ 0 & 1  \\ \end{array} \right)$$
Let $M$ be a matrix in your subspace. $M = x_1e_1 + x_2e_2 + x_3e_3+x_4e_4$. It should be clear that $x_4 = 0$ and that $x_1 = a$, $x_2 = b$ and $x_3 = c = \frac{-a-2b}{3}$.
We are left with only two free variables so the dimension of the subspace will be two.

Subspace for 2x2 matrix


Z3 switching between unknown/unsat by just changing the range sort of array

Question: I am facing a weird result when using Z3. Consider these two benchmarks written in smt-lib:
(declare-fun a () (Array (_ BitVec 32) (_ BitVec 32)))
(declare-fun b () (Array (_ BitVec 32) (_ BitVec 32)))
(assert (forall ((fqv (Array (_ BitVec 32) (_ BitVec 8))))
  (= (select a
                (concat (select fqv #x00000003)
                        (concat (select fqv #x00000002)
                                (concat (select fqv #x00000001)
                                        (select fqv #x00000000)))))
        (select b
                (concat (select fqv #x00000003)
                        (concat (select fqv #x00000002)
                                (concat (select fqv #x00000001)
                                        (select fqv #x00000000))))))))

(assert (= false (= (select a #x00000000) (select b #x00000000))))
(check-sat)
(get-model)

and
(declare-fun a () (Array (_ BitVec 32) (_ BitVec 8)))
(declare-fun b () (Array (_ BitVec 32) (_ BitVec 8)))
(assert (forall ((fqv (Array (_ BitVec 32) (_ BitVec 8))))
  (= (select a
                (concat (select fqv #x00000003)
                        (concat (select fqv #x00000002)
                                (concat (select fqv #x00000001)
                                        (select fqv #x00000000)))))
        (select b
                (concat (select fqv #x00000003)
                        (concat (select fqv #x00000002)
                                (concat (select fqv #x00000001)
                                        (select fqv #x00000000))))))))

(assert (= false (= (select a #x00000000) (select b #x00000000))))
(check-sat)
(get-model)

Their only difference is in the range of a and b. In the first case, it is a bit-vector of size 32. While in the second case, it is a bit-vector of size 8.
The interesting part is that (check-sat) is returning unsat for the first case, and unknown for the second one.
Is there an explanation for this? Here is a link where you can quickly run this experiment: https://people.csail.mit.edu/cpitcla/z3.wasm/z3.html
Answer: This looks odd indeed. I'd have expected you to get unknown regardless of which, since you have a quantified assert that ranges over an array.
The most likely reason is that there's some "internal" heuristic that kicks in for the first case, but fails to do so for the second. (So, in a sense, you're getting "lucky" for the first one, and the second one is the expected behavior given the quantified assert.)
Please report this at https://github.com/Z3Prover/z3/issues. Even though this isn't strictly a bug, I'm sure the developers would appreciate taking a look to see what internal heuristic might be improved to handle both of these cases.


How to read a parameter in url coming via POST method in spring boot

Question: The application requires driver and their locations to be registered. There are two api's one to register driver and one to save location.
The api's are

POST /api/v1/driver/register/

POST /api/v1/driver/:id/sendLocation/

The first api will request, response structure is as below
Request:
{
    "name": "Rakesh123",
    "email":"firstname.lastname@example.com",
    "phone_number":9876899979,
    "license_number":"DL12CDRWG1",
    "car_number":"DL1T43241"
}

Response:
{
    "driverId": 2,
    "name": "Rakesh123",
    "email": "email@example.com",
    "phone_number": "9876899979",
    "license_number": "DL12CDRWG1",
    "car_number": "DL1T43241",
    "location": null
 }

In the second api, the ":id" will be the driver id received and contains the request body with latitude and longitude
Request:
{
    "latitude": 12.972442,
    "longitude": 77.580643
}

I am facing problem while reading the ":id" parameter from request. I tried printing the request URI value and that's how the request is coming
/api/v1/driver/$id/sendLocation/

The methods are  written as below
1.
@PostMapping("/register")
public ResponseEntity<?> registerDriver(@RequestBody DriverDetails driver){
    responseMap = new HashMap<>();
    try {
        DriverDetails registeredDriver = service.registerDriver(driver);

        responseMap.put("driver", registeredDriver);

        responseEntity = new ResponseEntity<>(responseMap, HttpStatus.CREATED);
    }catch(Exception e) {
        responseMap.put("status", "failure");
        responseMap.put("reason", e.getMessage());
        responseEntity = new ResponseEntity<>(responseMap, HttpStatus.BAD_REQUEST);
    }

    return responseEntity;
}

@PostMapping("/{id}/sendLocation")
public ResponseEntity<?> saveDriverLocation(@PathVariable String id, @RequestBody Location location){

    responseMap = new HashMap<>();
    try {
        service.addLocation(Integer.parseInt(id), location);
        responseMap.put("status", "success");

        responseEntity = new ResponseEntity<>(responseMap, HttpStatus.ACCEPTED);
    }catch(Exception e) {
        responseMap.put("status", "failure");
        responseMap.put("reason", e.getMessage());
        responseEntity = new ResponseEntity<>(responseMap, HttpStatus.BAD_REQUEST);
    }

    return responseEntity;
}

Will appreciate help
Answer: Your code looks fine to me. Maybe it's a problem with the client that sends the request? Did it realy contains the Id value in the URL?
Here a small example with spring boot that reads the Id and print to console:
DemoApplication.java
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

ExampleController.java
@RestController
@RequestMapping("/api/v1/driver")
public class ExampleController {
    /**
     * Call this Endpoint with e.g. http://localhost:8080/api/v1/driver/123/sendLocation
     */
    @PostMapping("/{id}/sendLocation")
    public ResponseEntity saveDriverLocation(@PathVariable String id, @RequestBody String location) {
        System.out.println("received id: " + id);
        System.out.println("received location: "+ location);
        return ResponseEntity.ok().build();
    }
}

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.2</version>
        <relativePath/>
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

Comment: Hi Oliver, Thanks for the response. The client was expecting the response of register driver api should contain in the response json due to which the client was not able to pass the generated id back to second request. Thank you for responding.