Union generic type without Either Scala

Question: This works fine:
def echo[A, B](a: A, b: B): A = ???

This is also fine:
def echo[A, B](a: A, b: B): B = ???

However, how do we achieve this to return either type A or B?
// error
def echo[A, B](a: A, b: B): A|B = ???

Is it simply possible to have a union type of generic types A and B?
Cheers.

UPDATE1
Either is an option but not ideal, as it requires a pattern match when processing the returned result. Effectively, I want this: A <: A|B, B <: A|B, which Either does not achieve.
Another extreme, I can do this, but type is too loose:
def echo[A, B](a: A, b: B): Any = ???

UPDATE2
Why I don't want Either?
(Reason 2)
The returned result would actually be a Dataset of Spark, which requires Encoder for any type that is not a subtype of Product (see this topic).
When calling this method many times, it ends up with too many Either wrapping each other, e.g. Either[Either[Either[...]]]. This requires too many encoders to be defined.
So I'm actually doing this:
def echo[A, B](a: A, b: B): Dataset[A|B] = ???

If I do this it would require to have many different encoders due to Either type:
def echo[A, B](a: A, b: B): Dataset[Either[A, B]] = ???
val result1: Either[Cat, Dog] = echo(a: Cat, b: Dog)
val result2: Either[Either[Cat, Dog], Pig] = echo(result1: Either[Cat, Dog], c: Pig)

// we have to define encoders:
implicit encoder1: org.apache.spark.sql.Encoders.kryo[Either[Cat, Dog]]
implicit encoder2: org.apache.spark.sql.Encoders.kryo[Either[Either[Cat, Dog], Pig]]

// if we keep iterating, then too many encoders to define...

Comment: well, there's [Either](https://www.scala-lang.org/api/2.12.0/scala/util/Either.html)
Comment: Union type also requires a pattern match.
Comment: Sounds like function specialization from C++ would have been handy here
Comment: @smac89 Thanks for that, good to know. But it seems `@specialized` only works for specific types, meaning it cannot do `def echo[@specialized(A,B) AB](a: A, b: B, ab: AB) = ???` (where `A` and `B` are generic types). Secondly, it seems it cannot return a `@specialized` type e.g. `def echo[A, B](a: A, b: B): @specialized(A,B) = ???` Both errors!
Comment: @Dima Thanks! `Either` is not bad, but it requires a pattern match when processing the returned result. Effectively, I want this: `A <: A|B`, `B <: A|B`, which `Either` does not achieve.
Comment: @jack If you don't know the actual type of the returned value, you are going to have to match in the end to be able to do anything meaningful with it (other than trivial stuff like `print` or `equals`, but for that you could just use `Any`).
Comment: It seems you have a design problem in your **Spark** code since Scala 2 doesn't really support union types your current approach is doomed.
Answer: Soon-to-be-released Scala 3 has it.
def echo[A, B](a: A, b: B): A|B = ???  //this compiles

As can be seen in this Scastie session.
Comment: Thanks for reaching out. In Scala 2.11, it still does not compile, is there way to achieve this?

Union generic type without Either Scala


How Can i get the value from SessionStorage using JAVA

Question: I developed some code with javascript to retrieve an xhr response and I stored the value in the sessionStorage,
<code>var token = xhr.response;
sessionStorage.setItem('Token=', token);
</code>
Now I need to retrieve this value in my java class. Do you have any idea on how I can retrieve the value of sessionStorage or how to pass the javascript variable in the java class?
Thank you for your answers.
Answer: sessionStorage lives only in browser, to make the data available in your server you need to send it to the server. here is a similar question that should explain on how you could do it HTML5 session storage send to server.


Running a meteor server from terminal

Question: What is the default way of running a meteor server ? I used the command
meteor run

and
meteor

inside my meteor application folder, and I always get the same output (as long as I have not introduced any bugs) :
=> Started proxy.
=> Started MongoDB.
=> Started your app.

=> App running at: http://localhost:3000/

Also is there a way to speed this up ?
As of this post, I am running Meteor 22.214.171.124.
Edit
Here are two references which use the two different commands

Your First Meteor Application: A Complete Beginner's Guide to the
Meteor JavaScript Framework
Discover Meteor - Building Realtime JavaScript Web Apps
Answer: meteor and meteor run are equivalent and are the standard way to run the Meteor server via the command line utility during development.

Usage: meteor [--release ] [--help]  [args]
       meteor help
       meteor [--version] [--arch]

With no arguments, 'meteor' runs the project in the current
directory in local development mode. You can run it from the root
directory of the project or from any subdirectory.

Use 'meteor create ' to create a new Meteor project.
Commands:
   run                [default] Run this project in local development mode.
...

run is the default in case no other command is specified after meteor.
The initial run can be a rather slow, and sometimes takes longer than desired to pick up changes.
There is an unofficial webpack build package that could be faster in some cases, but there are some things to consider before using it (I would say that it has a slightly higher learning curve than the standard build system, and the file structure and features are slightly different between vanilla meteor and the webpack version).
In the (not-so-distant) future, there are plans to move to the LTS version of Node.js and migrate the build tool to npm, so things are likely to change.

Running a meteor server from terminal


Connect to hbase from remote machine

Question: I have VM where  habse is installed.
IP:192.168.20.10
I want to try to connect to hbase from my desktop:
Here is what I am trying>
public static void main(String[] args) throws IOException {
    AbstractApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/hbase-beans.xml", HBaseConnection.class);
    context.registerShutdownHook();
            UserRepository userRepository = context.getBean(UserRepository.class);
    List<User> users = userRepository.findAll();

    System.out.println("Number of users = " + users.size());
    System.out.println(users);
}

public List<User> findAll() {
        return hbaseTemplate.find(tableName, "cfInfo", new RowMapper<User>() {

            public User mapRow(Result result, int rowNum) throws Exception {
                return new User(Bytes.toString(result.getValue(CF_INFO, qUser)),
                                Bytes.toString(result.getValue(CF_INFO, qEmail)),
                                Bytes.toString(result.getValue(CF_INFO, qPassword))

                                );
            }
        });

    }

And this is my bean.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:hadoop="http://www.springframework.org/schema/hadoop" xmlns:lang="http://www.springframework.org/schema/lang"
    xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/hadoop http://www.springframework.org/schema/hadoop/spring-hadoop.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.0.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">

    <context:component-scan base-package="com.hbase.dao"/>
    <context:component-scan base-package="com.hbase.object"/>

     <hadoop:configuration id="hadoopConfiguration">fs.default.name=hdfs://192.168.20.10:9000</hadoop:configuration>

    <hadoop:hbase-configuration configuration-ref="hadoopConfiguration" zk-port="2181" zk-quorum="192.168.20.10"></hadoop:hbase-configuration>

    <bean id="hbaseTemplate" class="org.springframework.data.hadoop.hbase.HbaseTemplate">
        <property name="configuration" ref="hbaseConfiguration" />
    </bean>

</beans>

This code works fine when I run directly in remote machine.When I am running same code from my windows machine by providing ip of hbase .,it doesnot return any value.
Comment: Are you able to ping your VM ip address from your host machine? Also are you able to telnet to the ZooKeeper and Hadoop ports from your host machine. This may be just an issue with network connection rather than a hbase issue
Answer:
Configure your windows machine  C:\Windows\System32\drivers\etc\hosts
  file to resolve your HBase cluster node details. Recommended Make sure
  DNS configuration is right!. i.e. addresses of all
  nodes(Regionservers) of HBase Cluster is resolved from windows
  machine.

Connect to hbase from remote machine


What does HTTP code 172 mean?

Question: browsing access logs, sometimes I see
"POST / HTTP/1.0" 200
which should be OK
but other times I see
"POST / HTTP/1.0" 200 172
What does the 172 next to 200 mean?
Also, I see a few with 7250, or 44
Answer:
The last entry indicates the size of the object returned to the client, not including the response headers. If no content was returned to the client, this value will be "-".

From: http://httpd.apache.org/docs/2.2/logs.html#accesslog, Section Common Log Format
Answer: That is a post to the access log, which by default is formatted like this:
LogFormat "%h %l %u %t \"%r\" %>s %b" common

Where %b is your elusive '172', the size of the response in bytes. see this site.
Answer: According to Ken Quon at this link:

For default Apache log files, that would be the size of the object
  (number of bytes) returned to the client, not including the response
  headers. If no content was returned to the client, this value will be
  "-".

What does HTTP code 172 mean?


Log execve's, along with parent process argv?

Question: I'm trying to figure out if I can decomission an old server. I need the information about automated processes running there. So far I tried the following:
auditctl -a exit,always -F arch=b64  -S execve -k any-commands

At log analysis stage, I discovered two pieces of context missing:

How did those programs get executed? What process was their parent and what was its argv?
Where did the stdin/stdout go? Ideally I'd love to see the shell command reconstructed, but I know I'm probably asking for too much, so at least having pipe descriptor ID would do (so that I can try to re-construct it with my own scripts).

How can I approach such a problem?
Comment: That information isn't available in the execve system call. Perhaps you should be tracking other syscalls, like clone or open.
Answer: The execve system call replaces the current process. If a program wants to retain control after starting another program, it needs to create a new process first (using fork or vfork) that then calls execve.
The open file descriptors and permissions are taken over when the program image is replaced in execve (except those marked with the CLOEXEC flag), so open files are inherited from the parent process during fork, then modified between fork and execve (e.g. using dup2), and then finally filtered during the execve call.
So getting a complete picture from audit data will be difficult.


why topological conjugacy does not preserve ergodicity?

Question: I want to know why topological conjugacy does not transfer ergodicity from one dynamical system to another?and if we change the maps from continous to C1-differentiable will the problem solve or not?
Answer: As far as I am aware, topological conjugacy does transfer ergodicity.  That is, if the map $\tau: X \to X$ preserves the Borel probability measure $\mu$ and is ergodic (for that measure), and $\phi: X \to Y$ is a homeomorphism, then $\phi \circ \tau \circ \phi^{-1}: Y \to Y$ preserves the Borel probability measure $\mu \circ \phi^{-1}$ and is ergodic (for that measure).
What topological conjugacy doesn't preserve is absolute continuity of the invariant measure with respect to, say, Lebesgue measure.
Answer: It depends what you mean. The problem of topological conjugacy is that it can be not absolutely continuous (but if you ask for $C^1$, it is), which implies that it can map a set of positive measure to a set of zero measure or set of zero measure to a set of positive measure. So if you in a bad situation, you have one measure with a nice SRB (ergodic with Lebsgue as a reference measure) measure, for example, after applying a homeo you can get a measure that would be ergodic (as R. Israel said), but will not be SRB.


How does Custom User Control send data to Hosting Form

Question: I have implemented a custom user control using c#. This control has a button.
Then the control is added to a WinForm Form1 which has list box.
The problem is : how can I add some value in the list box when i click the button in the user control?
Comment: raise an event from the usercontrol
Answer: You have to raise an event from the user control when you click the button, and then catch it in your form1.
You can do something like this :
User Control
public event EventHandler CLickFromUserControl;

private void click_event_on_the_button()
{
    //Null check makes sure the main page is attached to the event
    if (this.CLickFromUserControl != null)
       this.CLickFromUserControl(new object(), new EventArgs());
}

Form1
public MyApp()
{
     //USERCONTROL = your control with the CLickFromUserControl event
     this.USERCONTROL.CLickFromUserControl += new EventHandler(MyEventHandlerFunction_CLickFromUserControl);
}

public void MyEventHandlerFunction_CLickFromUserControl(object sender, EventArgs e)
{
         //add the value here
}

You can pass more parameters to the event :
this.CLickFromUserControl(new object(), new EventArgs(), param1, param2);

And then in the form :
public void MyEventHandlerFunction_CLickFromUserControl(object sender, EventArgs e, string param 1, string param2)
{
         //add the value here
}

OR
You can create properties on your user control :
public string Value {
  get { return textBox1.Text; }
  set { textBox1.Text = value; }
}

And then access it from the sender in the form's event.
You can check out how to build an event : msdn
Comment: Did you put : public event EventHandler CLickFromUserControl; in the user control ?
Comment: You can pass the property to the event ! I will update the answer for you.
Comment: Vs didn'T recognize "CLickFromUserControl " in Form1.  It is like undefined under this -> this.USERCONTROL.CLickFromUserControl += new EventHandler(MyEventHandlerFunction_CLickFromUserControl);
Comment: Works 100% ,, thanks alot . Small question plz. if now i have several components in my Custom Control. Now i am in Forms 1 and i want to know which control is clicked and maybe also read its Text Property!!how can i modify that?
Comment: What do you mean text property ? The text property of your user control ?
Answer: As @Jonsey and @Vinc P. suggested, you should use Event to do your job.
Basically, the work flow is:

User triggers a click event on the button;
Custom User Control gets the event, handle it, then pass it to its parent, which is the form;
Form gets the event and handle it.

To achieve the workflow, we can do the following:
In User Control
We need to define delegate and event for the form to register:
public delegate void ButtonClickEventDelegate(object sender, EventArgs e);
public event ButtonClickEventDelegate ButtonClick;

Next we need to register the Button Click Event Handler:
// in the custom user control constructor
public CustomUserControl()
{
    InitializeComponent();
    Button.Click += ButtonClickHandler;
}

Next we need to define the behavior of Button click in the user control:
private void ButtonClickHandler(object sender, EventArgs e)
{
    // do some handling if you have
    // now the important part, call the delegate function here, it will pass the handle to
    // the behavior defined in the main form:
    if (ButtonClick != null) ButtonClick(this, e);
}

Now we have finished the User Control part, next is about the Hosting Form.
The thing we need to do is simple: register and define the behavior of ButtonClick:
// in main form's constructor
public Form1()
{
    InitializeComponent();
    CustomUserControl.ButtonClick += UserControlButtonClickHandler();
}

private void UserControlButtonClickHandler(object sender, EventArgs e)
{
    // add the value here
}

Ok, things done :)


Find the function $f(x)$ whose graph passes through the point $(0,\frac{4}{3})$ and whose derivative is: $f'(x) = x \sqrt{16-x^2}$

Question: Section 5.2
Can somebody verify this solution for me?
Find the function $f(x)$ whose graph passes through the point $(0,\frac{4}{3})$ and whose derivative is:
$f'(x) = x \sqrt{16-x^2}$

So the idea here is we integrate $f'(x)$ to get $f(x)=Something +C$ Where $C$ is our constant of integration, and then we use the fact that $f$ must pass through the point $(0,\frac{4}{3})$ to figure out what $C$ is.
So first lets integrate $f'(x) = x \sqrt{16-x^2}$
$\int x \sqrt{16-x^2}dx$
Let $u=16-x^2$. Then $\frac{du}{dx}=-2x$ and $\frac{du}{-2x}=dx$. And so we have:
$\int x \sqrt{16-x^2}dx$
$=\int x \sqrt{u}\frac{du}{-2x}$
$=\frac{-1}{2} \int \sqrt{u}du$
$=\frac{-1}{2} \frac{u^{\frac{3}{2}}}{\frac{3}{2}}+C$
$=\frac{-1}{3} u^{\frac{3}{2}}+C$
$=\frac{-1}{3} (16-x^2)^{\frac{3}{2}}+C$
Okay. So by defintion $f(x)=\int f'(x)dx$. Thus $f(x) = \frac{-1}{3} (16-x^2)^{\frac{3}{2}}+C$. Lets plug in the point $(0,\frac{4}{3})$ and solve for $C$
$\frac{4}{3} = \frac{-1}{3} (16-0^2)^{\frac{3}{2}}+C$
$\rightarrow \frac{4}{3} = \frac{-1}{3} (16)^{\frac{3}{2}}+C$
$\rightarrow 4= -(16)^{\frac{3}{2}}$
$\rightarrow 4=-(4^3)+ \frac{C}{3}$
$\rightarrow 4=-64+ \frac{C}{3}$
$\rightarrow \frac{68}{3}=C$
Therefore, plugging this back into $f(x)$, we get:
$f(x) = \frac{-1}{3} (16-x^2)^{\frac{3}{2}}+\frac{68}{3}$
Unfortunately, the computer says the correct solution is:
$f(x) = \frac{-1}{3} (16-x^2)^{\frac{3}{2}}+\frac{68}{3}$
Can somebody find my error? Thanks!
Answer: Your mistake is introduced here:

$\rightarrow \frac{4}{3} = \frac{-1}{3} (16)^{\frac{3}{2}}+C$
$\rightarrow 4= -(16)^{\frac{3}{2}}+C$

What you did here is multiply by $3$ on both sides, I believe. You also went ahead and let the factor of $3$ be "absorbed" into the constant $C$ (after all, $C$ is a constant, and therefore so is $3C$). We do this a lot in more complicated integrals, so it's understandable you would do this.
However, the value of $C$ necessary to satisfy this problem is fixed - a very particular (indeed, just one) value of $C$ can work here, so we cannot let $3C$ be considered as just as nebulous a constant as $C$. It's like when you're solving $x-1=0$: if you have $3x$ instead, you'd get a very different solution.
But at least this means rectifying your solution is easy. From the second line forward, just replace $C$ with $3C$, and then an additional last step will let you conclude $C=68/3$.
Answer: The correction is : $\frac{4}{3}=\frac{-1}{3}(16)^{3/2} +C$ then $ 4=-1(16)^{3/2} +3C$


How to define a default function on a mongoose schema?

Question: After spending a few hours now on this issue i have resorted to SO.
I am running into promise pending errors on the following field I want to assign a default value for:
    friendlyId: {
        type: Number,
        unique: true,
        index: true,
        required: [true, 'This is an auto-calculated field and you should not set it.'],
        default: function() {
            return mongoose.model(collectionName).countDocuments({})
            .then(count => count + 1)
            .catch(error => logError(error));
        }
    },

Yield the same error:
Error: toggles validation failed: friendlyId: Cast to Number failed for value "Promise { <pending> }" (type Promise) at path "friendlyId" at ValidationError.inspect (C:\source\Togly\server\node_modules\mongoose\lib\error\validation.js:50:26)
I have played around with a view different varations:
    friendlyId: {
        type: Number,
        unique: true,
        index: true,
        required: [true, 'This is an auto-calculated field and you should not set it.'],
        default: async function () {
            let counter = await mongoose.model(collectionName).countDocuments({});
            return counter + 1;
        }

Is this even do-able? I just require the field to be populate with a default generated value on model.save().
Answer: I solved this a .pre save hook.
featureSchema.pre('save', async function(next) {
    var count = await mongoose.model(collectionName).countDocuments({})
    this.friendlyId = count + 1;
    next();
})

The big gotcha was, i was using => functions and then this keyword was not binding to the document being saved.


how do pointers in c work here?

Question: #include <stdio.h>

int my_array[] = {1,23,17,4,-5,100};
int *ptr;

int main(void)
{
    int i;
    ptr = &my_array[0];     /* point our pointer to the first
                                      element of the array */
    printf("\n\n");
    for (i = 0; i < 6; i++)
    {
      printf("my_array[%d] = %d   ",i,my_array[i]);   /*<-- A */
      printf("ptr + %d = %d\n",i, *(ptr + i));        /*<-- B */
      printf("ptr + %d = %d\n",i, *ptr++);
      printf("ptr + %d = %d\n",i, *(++ptr));
    }
    return 0;
}

ptr++ should print ptr value than increment in it while ++ptr do 1st increment in ptr than print the value of ptr..  but when i compile the code it give me same result and what is void pointer. what is the use of it.
Comment: I don't see a single significant bit of C++ in there...
Comment: FWIW, arrays can be implicitly converted to pointers to their first element: `ptr = my_array;` is exactly the same. And `*(ptr + i)` is long-hand for `p[i]`. And you end up accessing way beyond the array, resulting in undefined behavior.
Comment: printf("ptr + %d = %d\n",i, *ptr++);
      printf("ptr + %d = %d\n",i, *(++ptr));
Comment: it give the same result. *ptr++ and *(++ptr), it  increment 8 byte while int take 4 byte in memory.than why its increment to 8 bytes.. it give the same output but my question is here now about post and pre-increment.. by definition post and pre- increment. it should not be the result according to the definition of post and pre increment..
Comment: I am having a feeling that this will invoke UB.
Answer: These lines:
printf("ptr + %d = %d\n",i, *ptr++);
printf("ptr + %d = %d\n",i, *(++ptr));

modify the pointer value. So at each iteration, you are adding 2 offsets beyond your current position i. By the time your for loop hits the 6th iteration, you will have tried to print memory locations from the end of the array to somewhere 6(iterations)*2(offsets)*4(int size)=48 bytes beyond.
Comment: sorry, i did not get printf("ptr + %d = %d\n",i, *ptr++);
printf("ptr + %d = %d\n",i, *(++ptr));
Comment: printf("ptr + %d = %d\n",i, *ptr++);
printf("ptr + %d = %d\n",i, *(++ptr));  its output.. plz explain me in easy way.. i am new one in c or c++
Answer: The expression *ptr++ is parsed as *(ptr++), and is evaluated as follows:

Retrieve the current value of ptr
Dereference this pointer value to obtain an integer value
At some point before the next sequence point, add 1 sizeof *ptr to ptr.

It's roughly equivalent to writing
x = *ptr;
ptr = ptr + sizeof *ptr;

except that the side effect of updating ptr may happen at any time before the next sequence point (never assume that side effects are applied in a particular order); IOW, the sequence could be
tmp = ptr;
ptr = ptr + sizeof *ptr;
x = *tmp;

In this case, the sequence point is at the end of the printf function call.
The expression *(++ptr) is parsed as written, and is evaluated as follows:

Retrieve the current value of ptr + sizeof *ptr
Deference this pointer value to obtain an integer value
At some point before the next sequence point, add 1 sizeof *ptr to ptr

which is roughly equivalent to writing
tmp = ptr + sizeof *ptr;
x = *tmp;
ptr = ptr + sizeof *ptr;

where again, the side effect of updating the value of ptr can happen at any point before the next sequence point; IOW, the sequence could be
tmp = ptr + sizeof *ptr;
ptr = ptr + sizeof *ptr;
x = *tmp;

Here's a hypothetical memory map at program startup (assume 32-bit addresses and 16-bit ints):

Item        Address            0x00  0x01  0x02  0x03
----        --------           ----  ----  ----  ----
my_array    0x08000000         0x00  0x01  0x00  0x17
            0x08000004         0x00  0x11  0x00  0x04
            0x08000008         0xff  0xfb  0x00  0x64
ptr         0x0800000C         0x00  0x00  0x00  0x00
i           0x10008000         0x??  0x??  0x??  0x??

0x?? indicates a random byte value.  Since ptr is declared at file scope, it has static extent and is implicitly initialized to NULL.  Since i is declared auto, it is not initialized and contains random garbage.
After executing the statement ptr = &my_array[0], the value of ptr is 0x08000000.
When you execute the line
printf("ptr + %d = %d", i, *ptr++);

the expression *ptr++ dereferences the memory at address 0x08000000, which contains the value 1, so the output is
ptr + 0 = 1

The side effect of the ++ operator updates the value of ptr to 0x08000002.
When you execute the line
printf("ptr + %d = %d, i, *(++ptr));

the expression *(++ptr) deferences the memory at 0x08000004, not 0x08000002, since the expression ++ptr evaluates to ptr + sizeof *ptr.  So the output of this line is
ptr + 0 = 17

and the side effect of the ++ operator updates the value of ptr to 0x08000004.
So as you loop, your output will look like
my_array[0] = 1     /* ptr = 0x08000000 */
ptr + 0 = 1
ptr + 0 = 1         /* ptr = 0x08000002 after */
ptr + 0 = 17        /* ptr = 0x08000004 after */
my_array[1] = 23
ptr + 1 = 4
ptr + 1 = 4         /* ptr = 0x08000006 after */
ptr + 1 = -5        /* ptr = 0x08000008 after */

Answer: After a few times through that loop, you're going to start referencing memory beyond the end of the array; except for that, your program looks like it makes sense.
Comment: printf("ptr + %d = %d\n",i, *ptr++);
printf("ptr + %d = %d\n",i, *(++ptr));
Comment: in *ptr++ it should 1st print *ptr and than increment so ans should be 1 because *ptr =1 at position 0 than do increment and go to next position so next time i should get ans 23.. its not look like this when i run my code.
Answer: You are probably only look at the last part of the output because, when I compile it it shows each value of the array and then 0s after you have gone through the entire part of the code.
And as for void pointers:
They are also called Generic Pointers because you can assign them to any data type.
Comment: in first iteration it give me ans as my_array[0]=1 , ptr 0=1  and ptr 0=23 ptr 0=23 how this  printf("ptr + %d = %d\n",i, *ptr++);
printf("ptr + %d = %d\n",i, *(++ptr)); give ans as 23 in 1st iteration..
Answer: First of like Ignacio pointed out, this is all C code. Not c++. C++ is a superset of C.
1) I want to point out that your for loop is not safe. Cause your are incrementing the pointer twice inside the loop in each iteration. So, you will end up outside the allocated array at the 4th iteration.
2) Any C questions please refer to the excellent c faq and c++ faq. here is a relevante answer to your question. http://c-faq.com/ptrs/unopprec2.html .
   Its got to do with operator precedence. *ptr++ is equivalent to *(ptr++).
The output of your first iteration should be
1   1
1
17
void pointer is generic pointer. you cannot do arithmetic on it. it is just used to pass around location, crudely put. Also, importantly you cannot do arithmetic on it. because, you dont know what its pointing to.
Comment: to be very frank , i am not understanding its output..
printf("ptr + %d = %d\n",i, *ptr++); printf("ptr + %d = %d\n",i, *(++ptr));
in *ptr++ it should 1st print *ptr and than increment so ans should be 1 because *ptr =1 at position 0 than do increment and go to next position so next time i should get ans 23.. its not look like this when i run my code.

how do pointers in c work here?


Generate non-random data that follows a given correlation matrix

Question: Problem: I have to generate in R a synthetic dataset which contains three different variables that didn't follow any known distribution.
What I did until now: Grouping historical values of each distributions in Markov chains, I was able to generate synthetical data that are quite good for my purpose if I evaluate each distribution separately. However, if I try to compare the whole synthetic dataset correlation matrix or the mutual information with the real dataset ones, the results are really poor:

For the mutual information:

I already tried the Cholesky decomposition but the results were far (and even worse) from what I was able to achieve with the first version of the model. I also tried "to mix" the Markov Chains in order to select the next state of all the other distributions using the current state of one of them, but also in that case the results were not good enough.
Question: are there any strategies/statistical tools/models to solve this problem?
To graphically understand the state of my results, these are the plots of the original dataset (first figure) and of the synthetic one (second figure).
Comment: Please explain the *purpose* of this exercise.  That will help suggest appropriate solutions (from among the myriad possible ones).  *Why* are you generating synthetic data?
Comment: This is an exercise that I have to solve: my objective is to generate synthetic data from three different time series related with a fixed covariance / mutual information. The main problem is that these time series don't follow any kind of known distribution because they're related to machine-behaviour and, for the same reason, the values show a _time consistency_ that keeps me from simply using `rnorm` or similar.
Answer: I'm not totally sure what you try to achieve here. I also don't understand your notion of 'non-random' there. But I assume that you're trying to simulate a data of three variable that has the same joint distribution as some real data set, and you specifically want the correlation structure to be the same.
There are many ways to achieve this. If you have plenty of data (~100^3), you can do non-parametric density estimation to recover the joint density and do Monte-Carlo with the estimated density.
Another way is to restrict our attention to marginals and correlation, which would presumably work well with much less data. It would work as follows.

First estimate the marginal distribution of each variable.
Apply the corresponding quantile functions(the inverse of the distribution functions) to each variable. Denote them by $Q_x,Q_y,Q_z$.
Estimate the correlation matrix $\Sigma$ of $\Phi^{-1}(Q_x),\Phi^{-1}(Q_y),\Phi^{-1}(Q_z)$, where $\Phi$ is the distribution function of standard normal.
Now, we're ready to simulate data using Gaussian copula.
Generate a normal vector that follows $N(0,\Sigma)$. You can do it by multiplying Cholesky decomposition of $\Sigma$ to the independent normal vector. Denote them by $x_{sim},y_{sim},z_{sim}$.
Let $X_{sim}=Q_x^{-1}(\Phi(x_{sim}))$, and define $Y_{sim},Z_{sim}$ similarly. Then these are desired random variables that have the marginal distributions estimated in 1, and similar correlation structure as the original data.
Comment: I want to generate three synthetic distributions which correspond to three different physical quantities (eg: voltage, current and frequency) starting from real world data. I don't want to simply generate some data because these data needs to have also a _time consistency_ since, for example, if the voltage in t1 is 123, in t2 it can't be 400 or -20 but a value between 124 and 128. However, since the distributions are not known, I wasn't able to find a way to extract / estimate the marginal distribution.
Answer: I'm not exactly sure I understand what you're trying to do, but if your goal is simply to "Generate non-random data that follows a given correlation matrix," the approach you're taking appears more complicated than necessary. Consider the following.
Let $x \sim F$ be an $n$-dimensional real random vector with mean $\mu$ and positive-definite covariance $\Sigma$. Let $X\in\mathbb{R}^{m\times n}$ where each row is a draw from $F$. To impose a correlation matrix $\Omega \in \mathbb{R}^{n\times n}$ (i.e. make it so your data has correlation $\Omega$) while preserving the variances $\mathbf{diag}\Sigma$ and mean $\mu$, the following transformation may be applied to $X$:
$$\phi(X) = (X - \mathbf{1}\otimes \mu)\cdot\Sigma^{-1/2}\cdot\Omega^{1/2}\cdot\mathbf{diag}\Sigma^{1/2} + \mathbf{1}\otimes \mu$$
where $\mathbf{1}$ is an $n$-vector with $1$ in every entry and $\otimes$ is the standard outer-product. Then, the rows of $\phi(X)\in\mathbb{R}^{m\times n}$ have correlation $\Omega$, variances $\mathbf{diag}\Sigma$, and mean $\mu$. In your case, if you don't really have a preference for the mean and variance,  just set $(\mu, \Sigma) = (0, \mathbb{I})$.


JSF CDI Bean initialization on every page access

Question: I'm currently learning about JSF 2.0 and i dont know how to initialize something in my CDI bean everytime a jsf page is accessed ?
For example, let's say in my case, i have one CDI bean that is used in one JSF page. Somewhere inside the bean, i would like to query and  initialize something, or check authorization everytime the page is accessed (not everytime the action method is called).
I could put it into the bean's constructor, but that doesnt guarantee that the constructor would be called for every calls of the jsf page ? Or should i call using the #{myBean.initialize()} on the JSF page ?
Regards,
Albert Kam
Answer: If you declare the managed bean @RequestScoped, then it will be created for every HTTP request (yes, that's "everytime a JSF page is accessed" as you say in enduser's terms). You can just do the initialization job in the bean's constructor or @PostConstruct method. This has nothing to do with CDI.

check authorization everytime the page is accessed (not everytime the action method is called).

For that, a javax.servlet.Filter or very maybe a javax.faces.event.PhaseListener is a much better solution.
Answer: You could have a look at https://cwiki.apache.org/confluence/display/EXTCDI/JSF+Usage
Answer: If I am not mistaken, the constructor is called every time the page loads - as soon as you apply your bean to the jsf page, that object is constructed. Hence, must have a constructor without any parameters


How do I concatenate values from 2 fields into one field

Question: I'm trying to concatenate 2 fields (Name and Other names) into one variable. For example, if my name is Jeffrey and other name is Johnston the the result should be Jeffrey Johnston.
I have tried the following on PRE-TEXT-ITEM
BEGIN
    :LOAN.NAME := ':LOAN.NAME :LOAN.OTHER_NAMES';
END;

BEGIN
    :LOAN.NAME := :LOAN.NAME + ' ' + :LOAN.OTHER_NAMES;
END;

When trying the following code
BEGIN
    :LOAN.NAME := :LOAN.NAME + ' ' + :LOAN.OTHER_NAMES;
END;

I received this error message:

FRM-40735: PRE-TEXT-ITEM trigger raised unhandled exception ORA-06502
Comment: The Oracle symbol for the concatenation operator is `||` (two pipe symbols), not `+`. That should be pretty much all you need.
Comment: @mathguy thanks! This works great.
Answer: You should use ||
BEGIN
    :LOAN.NAME := :LOAN.NAME || ' ' || :LOAN.OTHER_NAMES;
END;


Wildcard Search in Views

Question: Is there a way to use Wildcards (like *) in Views filters. I do not want to give the user the dropdown with all the operators but use a wildcard.
For example, when he writes name it will use "ends with", on name "begins with", on name "contains" and so on. Is there something out there that can do that, or do I have to write it on my own?
Comment: You can try views query alter .. For more info http://drupal.stackexchange.com/questions/76651/how-to-use-hook-views-query-alter-to-modify-where-condition
Comment: Contains does exactly same thing... http://drupal.stackexchange.com/questions/88632/views-use-like-operator-in-exposed-search-filter-search-search-terms
Comment: It does not. it is always working with "like". But if I want to get only the "ends with" or "starts with" items, I just can't get them with "contains" but get all items that contain my search-word. Looks like I have to code that.
Answer: Rather than filtering on a certain field (and then exposing the filter), try adding a filter of the type 'Search terms'. That uses drupal's search module to search, which has much smarter ways of searching.
Comment: It seems like I can't use "search terms" in my commerce products view or I can't find it :(

Wildcard Search in Views


Compartilhamento de objeto JSON entre diferentes clusters

Question: Boa tarde,
Gostaria de saber como faço para compartilhar um objeto JSON entre diferentes clusters.
Atualmente tenho 3 clusters ouvindo três portas diferentes. E preciso que se tenha um objeto salvo na memória para que os três clusters possam acessar as informações deste objeto.
var express = require('express');
var net = require('net');
var Files = require('./lib/Files');

var app = express();
var port = 9000;

if(!myObject) {
    // Cria o objeto na memória para que os outros sockets possam acessar
}

// Cria o Servidor
net.createServer(function (socket) {
    if (config.STRLOG) {
        console.log("Dispositivo Conectado: " + socket.remoteAddress + ":" + socket.remotePort);
    }

    socket.on('data', function (data) {
        console.log(data);
    });

    socket.on('error', function (err) {
        console.log('> SOCKET ERROR');
        Files.SocketErrorLog(port, err);
        console.log('------------------');
    });
}).listen(port);

module.exports = app;

Comment: Esse ficheiro é estático? ou deve ser acedido e/ou modificado por servidores diferentes? Existe alguma base de dados?
Comment: A ideia é que quando instanciado o objeto, as informações sejam buscadas no banco. Após instanciado ele é colocado na memória para que os outros sockets possam acessá-lo.
Answer: Você pode utilizar o banco de dados REDIS para salvar seu json e seus clusters podem acessar os objetos de lá ex:
var express = require('express'),
redis = require('redis'),
cookieParser = require('cookie-parser'),
session = require('express-session'),
redisStore = require('connect-redis')(session),
logger = require('morgan'),
bodyParser = require('body-parser');

var client = redis.createClient(),//CREATE REDIS CLIENT
    app = express();

app.use(cookieParser('strongsecretcode'));
app.use(session(
    {
        secret: 'strongsecretcodehere',
        store: new redisStore({ host: 'localhost', port: 6379, client: client }),
        saveUninitialized: false, // don't create session until something stored,
        resave: false // don't save session if unmodified
    }
));

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

var router = express.Router();

router.get('/yoursession/set/:value', function(req, res) {
    req.session.user = req.params.value;
    res.send('session saved in Redis');
});

app.get('/yoursession/get/', function(req, res) {
    if(req.session.user)
        res.send(req.session.user);
    else
        res.send("no session value stored in Redis ");
});

app.use('/', router);
var server = app.listen(2001, function() {
    console.log('REDIS SESSION server is listening on port %d', server.address().port);
});

No livro: Aplicações web real-time com Node.js Página 29 explica como utilizar o redis em aplicações baseadas em clusters.
Comment: "O Redis guarda e busca, em sua base de dados, elementos chave-valor, de maneira extremamente rápida, pois mantém os dados em grande parte do tempo na memória, fazendo em curtos períodos a sincronização dos dados com disco rígido. Ele é considerado um NoSQL do chave-valor"

Trecho de: Caio Ribeiro Pereira. "Aplicações web real-time com Node.js."
Comment: A tua resposta é bem-vinda mas é importante traduzir as partes comentadas, indicar a fonte de onde copiaste esse código e dar uma explicação do código com conteúdo teu. Assim a resposta ficaria muito melhor :) Diz-me quando tiveres feito isso que dou `+1` com gosto!
Comment: Interessante! A velocidade de acesso a este banco de dados é o mesmo para o acesso de objetos direto na memória?


Change of basis between coordinate charts in $\mathbb RP^2$.

Question: I'm looking at the real projective plane $\mathbb RP^2$, with homogeneous coordinates $(x:y:z)$. I want to find the transition matrix between bases associated with different coordinate charts.
I've arrived at an answer, which I think is correct, but I'm not sure, and the answer doesn't seem to be in the form the questions suggests it should be.
Here's what I've done:
First, I split $\mathbb RP^2$ up into three charts: $x \neq 0$,
$y \neq 0$ and $z \neq 0$.
In the chart $x \neq 0$, define $\varphi_1 : \mathbb R^2 \to \mathbb RP^2$ where $\varphi_1(u_1,v_1) = (1:u_1:v_1)$ and
$$\varphi_1^{-1}(x:y:z) = \left(\frac{y}{x},\frac{z}{x}\right) $$
In the chart $y \neq 0$, define $\varphi_2 : \mathbb R^2 \to \mathbb RP^2$ where $\varphi_2(u_2,v_2) = (u_2:1:v_2)$ and
$$\varphi_2^{-1}(x:y:z) = \left(\frac{x}{y},\frac{z}{y}\right) $$
In the chart $z \neq 0$, define $\varphi_3 : \mathbb R^2 \to \mathbb RP^2$ where $\varphi_3(u_3,v_3) = (u_3:v_3:1)$ and
$$\varphi_3^{-1}(x:y:z) = \left(\frac{x}{z},\frac{y}{z}\right) $$
Next, I found the change of coordinates between the 2nd and the 3rd charts:
$$(u_3,v_3) = (\varphi_3^{-1} \circ \varphi_2)(u_2,v_2) = \left(\frac{u_2}{v_2},\frac{1}{v_2}\right)$$
I think this is how to express the coordinate vector field from the second chart in the third chart:
$$ {\bf e}_{u_2} = \frac{1}{v_2} \, {\bf e}_{u_3} \ \ \text{ and }
\ \ {\bf e}_{v_2} = -\frac{u_2}{v_2^2}\,{\bf e}_{u_3} -\frac{1}{v_2^2}\,{\bf e}_{v_3} $$
and I would be able to find the transition matrix. Is this correct?
Ultimately, I want to express the matrix in terms of functions of the homogeneous coordinates. However, I seem to have a matrix giving a map $T\mathbb R^2 \to T\mathbb R^2$, and I can't see where the $x$, $y$ and $z$s are going to come from. Any guidance would be appreciated.
Comment: This is the type of bounty-carrying problem that I always find confusing: what answer to give when everything written by the bounty-giver is correct? Your computations are correct, I have done them separately. What I do not understand, though, is why you would want $\Bbb d (\phi _3 ^{-1} \circ \phi _2)$ to be expressed in terms of coordinates on $\Bbb R P^2$, a thing that is geometrically impossible (because the domain and codomain of this change of coordinates only see the spaces of coordinates, not the manifold itself).
Comment: Surely the results are well-defined with respect to $(x:y:z)$, because they are  **homogeneous** expressions, and that is the only essential fact. Note, though, that the matrix entries are not elements of $\Bbb R \bf P ^2$ (which are points), but homogeneous rational functions of $x,y,z$.
Comment: @AlexM. Thanks for looking through my work. Your confusion is exactly my confusion. I can't see how to express $\mathrm{d}(\varphi_3^{-1}\circ\varphi_2)$ in terms of the coordinates on $\mathbb R\mathrm P^2$. But this is a question from a coursework assignment that I found on a university's website. (I'm not taking the course). It says "*Calculate the transition matrix between the bases associated with the 2nd and the 3rd charts. You should express the matrix entries in the final answer as functions of the homogeneous coordinates*."
Comment: The only meaningful thing that I can think about is to replace $(u_2, v_2)$ by $(\dfrac x y, \dfrac z y)$ in your result, but this is a mathematically mechanical procedure that does not bring any insight into the problem.
Comment: @AlexM. I think you might be right! I had thought about that, but the terms $1/v_2$, $-u_2/v_2^2$ and $-1/v_2^2$ aren't quotients of homogeneous polynomials. So I assumed they wouldn't be well-defined in homogeneous coordinates. I've just made the substitutions, and **they do** come out as well-defined in  terms of $(x:y:z)$. I'm not sure what the transition matrix would mean: a $2 \times 2$ matrix which takes $\mathrm T\mathbb R^2$ to $\mathrm T\mathbb R^2$, but its entries are elements $\mathbb R \mathrm P^2$?!
Answer: In order not to let this question without an answer, allow me to collect here what was discussed in the comments, for what it's worth.
First, your computations are correct, there is nothing to add or doubt about this.
Second, note that the word "coordinates" may be used with two meanings here. One meaning is the usual one in differential geometry (such as in "coordinate chart"). The second one stems from the existence of "homogeneous coordinates" on every manifold that embeds into some projective space; unlike the coordinates in the first sense, these latter ones exist globally on such a manifold.
Normally, the matrix entries of $\Bbb d (\varphi _3 ^{-1} \circ \varphi _2)$ will be functions of $u_2$ and $v_2$. Fortunately, these charts called "projectivizations" have inverses that are easy to write down and, furthermore, their components are homogeneous rational functions of $[x : y : z]$, namely $\dfrac x y$ and $\dfrac z y$ (this particular formula for $\varphi _2 ^{-1}$ is well defined because $[\alpha x : \alpha y : \alpha z]$ and $[x : y : z]$ produce the same fractions, by simplification). In formulae, $\varphi ^{-1} _2 (p) = (u_2 (p), v_2 (p)) = (\dfrac x y, \dfrac z y) (p)$, and thus the entries of $(u_2, v_2) \mapsto \Bbb d (\varphi _3 ^{-1} \circ \varphi _2) (u_2, v_2)$ will be homogeneous rational functions of $[x : y : z]$.


The job failed. Unable to determine if the owner(xyz\Administrator) of job "abcde" has server access

Question: I had created a SQL Server Job that throws the error that is mentioned in the question itself. I have searched in Google for same and everywhere its about giving rights/permission but not sure from where to give the same or how to give that?
My SQL Server 2008 Account info
Username: (local)
Authentication: Windows
No Password is needed
Thanks!
Answer: Try to set the job owner to sa user. For me it works all the time.
There is a known security problem when the SQL Server can't check the Windows users's credentials. Look here for some guideline about the issue.


Ideal equals the whole ring

Question:
Show that in the polynomial ring $S=K[x_1, ..., x_n]$, having an ideal $I = (g_1, ..., g_m)$ and ${g_1, ..., g_m}$ a Groebner bases of $I$, then $I = S$ if and only if one of the $g_i$ is a nonzero constant polynomial.

I tried using  using another result: under the same conditions, a polynomial $f \in S$ belongs to $I$ if and only if $f$ reduces to $0$ with respect to ${g_1, ..., g_m}$ but am not 100% sure if everything is correct.
I was also curious if there is another approach.
Comment: $1\in I\Rightarrow 1\in in(I)=(in(g_1),\dots,in(g_m))$
Comment: What is $\;S\;$ ??
Answer: This answer just expands on user26857's excellent hint.
Note that $I=S$ if and only if $1 \in I$.
One direction is trivial. If one of the $g_i$ is a constant, then clearly $I=S$.
Now, if $I=S$, then $1 \in I$. But this implies that $1 \in in(I)=\langle in(g_1),\cdots,in(g_m) \rangle$. All the generators are monomials (i.e. of the form $\vec x^\vec a$ for $\vec a \in \mathbb N_0^n$).

But the only way $1$ can be in an ideal generated by monomials is if $1$ is a scalar multiple of one of the generators. Thus $1 = k \cdot in(g_l)$ for some $l$. Now this implies that $g_l=k$ as well, since $in$ should pick out the highest term.

Ideal equals the whole ring


Let the random point (X,Y) be uniformly distributed on the square

Question: Let the random point $(X,Y)$ be uniformly distributed on the square
$D=\{(x,y):-1\leq x\leq 1,\ -1\leq y \leq 1\}$.
Find the distribution function and the probability distribution function of $Z=X +Y$
Answer: Hint: convolution.
Maybe Macro is right and the Wikipedia entry could be a little clearer.  Let's work through a bit of the problem together.  We see that in the (axis-aligned) square, knowing $X$ tells you nothing about $Y$ (and vice versa).  That is, they are independent.  So, you are looking for the sum of independent variables.  This is called a convolution.
So, let's try to figure out how a convolution should work.  We know that if $Z=0.2$, then one possibility is $X=-0.3, Y=0.5$; another is $X=0.1, Y=0.1$, and so on.  So, it might a bit of a leap here, to see that the density of $Z$ is:
$$f_Z(z) \propto \int_{-\infty}^{\infty}f_X(x)f_Y(z-x)dx$$
In other words, the density of $Z$ at $z$ is proportional to to the density of $X$ at $x$ times the density of $Y$ at $y$ where $y$ is the number that would have to be added to $x$ to get $z$: $z-x$.
We said proportional to because we still need the density of $Z$ to sum to one.
To complete the problem, try to simplify this integral.  What is the density of $X$?
Comment: Indeed, no (explicit) convolution or appeal to independence is necessary, though the latter is trivial. This can be done completely geometrically, if one prefers that approach.
Comment: @Macro: With homework questions, the accepted practice is to give hints, and not full answers.  The wikipedia entry describes the procedure as well, and even has a nice diagram of this problem.
Comment: Also, normally, I reserve downvotes for answers that are wrong, or misleading.  If you feel that more of a hint would be beneficial to the asker's education, my personal inclination would be to add your own answer.
Comment: This looks more like a comment than an answer
Comment: Neil's answer misses the main point: justify that $X$ and $Y$ are independent.
Comment: @Stéphane Why is any reference to independence needed at all?  It's not necessary to invoke independence in order to compute the distribution of $X+Y$--for that, you only need to know the distribution function for the bivariate uniform distribution on this square.  Neil, this consideration suggests that your discussion of independence may be more of a distraction than a useful hint.
Comment: @whuber Yes but I followed Neil's idea: he proposed to use the well-known convolution to derive the density of the sum of two independent r.v.; following this way, it is most interesting to focus on the independence rather than showing how to apply a formula
Comment: @NeilG, I know the common practice is to point in the right direction, not answer the whole question, for homework. But, I think "Hint: convolution." falls more under the category of comment than answer. My intention wasn't to be obnoxious or ruffle any feathers. (+1, btw)
Comment: @cardinal: good point.
Answer: For any random variables $X$ and $Y$ (not necessarily restricted to the unit square, and not necessarily independent) with joint density function $f_{X,Y}(x,y)$, and any real number $z$
we have that
$$P\{X + Y \leq z\} = \int_{x=-\infty}^{\infty} \int_{y = -\infty}^{z-x}
f_{X,Y}(x,y)\,\mathrm dy\,\mathrm dx$$
which integral can be easily set up by beginning students if they would
only bother to sketch the $x$-$y$ plane, draw on it the line $z = x+y$,
and think a bit about which of the two regions (above the line or below
the line) corresponds to the event $\{X+Y\leq z\}$.  Thus with $Z$
denoting $X+Y$, we can express the cumulative probability distribution
function (CDF) of $Z$ as
$$F_Z(z) = P\{Z \leq z\} = P\{X + Y \leq z\}
= \int_{x=-\infty}^{\infty} \int_{y = -\infty}^{z-x}
f_{X,Y}(x,y)\,\mathrm dy\,\mathrm dx$$
and so we can find the density function $f_Z(z)$ by differentiating
the CDF thus obtained. Those familiar with Leibniz's rule for
differentiating under the integral sign will be able to bypass this
two-step process and write
$$f_Z(z) = \int_{-\infty}^{\infty} f_{X,Y}(x,z-x)\,\mathrm dx$$
directly. Note the lack of a proportionality sign as in
NeilG's answer: this is an equality.
For the special case of $(X,Y)$ taking on values in a square
region as in the OP's problem, the computation of $F_Z(z)$
becomes an integration of the joint density over a right
triangular region or a pentagonal region that is the complement of a
right triangular region. For the even more special case of a uniform
density, even formal integration can be avoided since simple mensuration
formulas suffice. This latter is the geometric approach mentioned
by cardinal in his comments on NeilG's answer.


Placing a tilted rectangle behind image

Question: I am trying to do this in css and html:

What I got so far:

css:
#logo-shadow {
  width: 250px;
    height: 50px;
    transform: skew(20deg);
    position: absolute;
  top: 10px;
  left: 10px;
  padding: 5px;
  background-color: green;
  z-index: -1;
 }

#logo-container {
    position: relative;
}

fiddle: https://jsfiddle.net/hobqmxjp/

Despite not knowing how to get the right shape with css, the problem is, that I need the background rectangle to have the width according to the logo file and not a fix width/height. So it also works responsive.
How can I achieve these two things?
Also
Comment: Why don't you just make a svg out of the file, so you don't need to worry?
Comment: Make the container inline-block (or add an additional container wrapping image and shadow), so that its width will be that of the image. And then simply don't specify a width for your absolutely positioned show, but a `right` "coordinate" as well. (And the extra shadow div could of course be replaced by a pseudo element of the container to begin with.)
Answer: The following will scale with the image. I have changed skew to rotate as I think this is more likely to give the desired look.

#logo-shadow {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%; /* space of text including padding */
  height: 55%; /* desired height */
  transform: rotate(-2deg); /* rotation of the rectangle */
  background-color: gray;
  z-index: -1;
}

#logo-container {
  position: relative;
  display: inline-block;
  padding: 0 15px;
}

Answer:

 #logo-container {
  position: relative;
  padding: 0 10px;
  display: inline-block;
 }

 #logo-container img {
   width: 100%;
  }

  #logo-shadow {
   width: 100%;
   height: 50%;
   transform: skewY(-2deg);
   position: absolute;
   bottom: 0;
   left: 0;
   padding: 5px;
   background-color: green;
   z-index: -1;
  }

Comment: Can you [edit] your question to include an explanation of your code?
Answer: You just need to modify the css using the value fit-content with the width property of the container. This way, its size is based on the size of he biggest element which is at this point the image. Then, you can use % values to give a width and height to your #logo-shadow. You can also use relative units if you want to. Finally, you can use rotate() to rotate your shape.

#logo-shadow {
  width: 100%;
  height: 50%;
  transform: rotate(-1deg);
  position: absolute;
  bottom: 10px;
  left: 10px;
  background-color: gray;
  z-index: -1;
}

#logo-container {
  position: relative;
  width: fit-content;
}

Hope it will help.

Placing a tilted rectangle behind image


Variable with negative coefficient in Ridge Regression and positive correlation

Question: In my research, I have four independent variables (X1, X2, X3 and X4) and one response variable (Y). Upon checking the VIF values of the explanatory variables, I noticed that they have high multicollinearity, so I decided to use a Ridge Regression to make predictions about the variables, without, of course, using their P-values. When I plotted the Ridge Trace Plot and the chart with the regression coefficients from the Ridge Regression outcome, I noticed that X2 and X4 had negative effects on the model.
I wasn't surprised that this would happen with X4, but I didn't expect the same to happen with X2. I was even more puzzled when I performed a simple linear regression comparing Y and X2, and noticed a positive correlation.
So I decided to remove X3 from my study, since this variable had the highest VIF value (about 13), and I realized that everything made more sense, with X2 presenting a positive influence on the model.
Should I then delete X3 permanently? If I can include this variable in the model, how would I explain the fact that X2 has a negative effect on the model, and yet it has a positive correlation with Y?
Comment: Simpson's paradox? https://en.wikipedia.org/wiki/Simpson%27s_paradox
Comment: yes, I believe it applies. its just a bit harder to visualize. but see my answer which I believe captures simpsons paradox for your case
Comment: @psboonstra, but would Simpson's paradox apply to numerical variables?
Answer: We can ignore the ridge regression bit, and we can also suppose you have just two variables, $X_1$ and $X_3$ (these are the two most relevant to your question). Suppose that $E[Y|X_1,X_3] = \beta_0+\beta_1X_1+\beta_3X_3$. Then, iterating the expectation, $E[Y|X_1] = E_{X_3|X_1} E[Y|X_1,X_3] = \beta_0 + \beta_1X_1 + \beta_3 E[X_3|X_1]$. Now, further suppose that $E[X_3|X_1] = \gamma_0+\gamma_1X_1$. Then, $$E[Y|X_1] = \beta_0 + \beta_1X_1 + \beta_3 (\gamma_0+\gamma_1X_1)\\ = \beta_0 +  \beta_3 \gamma_0+(\beta_1+\gamma_1)X_1$$
So, if $\beta_1+\gamma_1 > 0$ and $\beta_1<0$, then you would expect the sort of findings that you are encountering in your data analysis. Moreover, the regression coefficients are functions of correlations: if the assumed model is correct, then $\gamma_1= \mathrm{cor}(X_1,X_3)\sqrt{\dfrac{\mathrm{var}(X_3)}{\mathrm{var}(X_1)}}$. So, the sign of $\gamma_1$ is determined by the sign of the correlation, and the magnitude of $\gamma_1$ is driven by the size of the correlation and the relative ratios of the variances of $X_3$ to $X_1$.
Comment: I believe I understood your explanation, thank you very much! In this case, how then could I investigate in _R_ this possible empiric variance in `X3`? If it's really happening, would it be interesting to develop the Ridge Regression without this variable?
Comment: I made some changes to my answer based on your comment. What I mean is look at the correlation between $X_1$ and $X_3$ and the variances of $X_1$ and $X_3$, i.e. `cor(X1,X3)`, `var(X1)`, and `var(X3)`. Re: your question about ridge regression, it's not a bad thing to do here. Are you using AIC or something to choose your tuning parameter?
Answer: Here's an image that explains psboonstra's idea of the situation being a case of the Yule-Simpson effect.
I simulated $X_1 \sim N(0,1)$ and $Y = X_1 + \epsilon$ with $\epsilon \sim N(0,1)$, and added an extra variable $X_2 = 5+\text{round}((Y+X_1)/2)$
set.seed(1)
n = 300
X1 = rnorm(n,0,1)
noise = rnorm(n,0,1)
Y = X1+noise
X2 = 5+round((X1+Y)/2)
plot(X1,Y, col = X2, pch = 20)
lm(Y~X1+X2)

In this case the variable $X_2$ is discrete such that one can see it still as a categorical variable as in common examples of the effect.
The Yule-Simpson effect is that the positive effect of $X_1$ on $Y$ reverses to a negative effect when you consider the "groups" $X_2$ as well.
With some imagination one can see the variable $X_2$ becoming continuous and giving a continuous range of groups.


How to determine vocal range required to sing a particular song from sheet music

Question: I need to list the required vocal ranges for a number of songs in order to find singers to perform in a show. Is there a way I can easily determine this from the sheet music? Thanks so much in advance :-)
Answer: The range (lowest to highest note) alone doesn't necessarily tell you the ideal vocal type for the song and key. You get help for that from the song's tessitura, or the range within which most of the notes fall.
A mezzo soprano might be comfortable with a phrase or two going up to, say, an A below high C, but if the song  has a lots of high notes, it may be better suited for a soprano, or might need to be transposed down to be comfortable for a mezzo, who might otherwise say something like "I can sing all the notes, but it lies too high for my voice." You could think of the tessitura as  a song's "center of gravity" in terms of pitch.
Answer: You look at the sheet music and find the lowest note and highest note for each voice in a song. That's the range for that song
Comment: @phoog The question specifically asks about the **vocal range** and not the type of voice.
Comment: I believe the question is not asking for the range of notes but for the kind of voice (soprano, mezzo soprano, tenor, baritone, etc.).  Two songs can have an identical range of notes but be suited to different kinds of voices.
Answer: One simply searches the part of the sheet music that one wishes to sing, then determines what the closest written note to the bottom of the paper is, then the highest for the top. Subtract the two to find the required range.
(You should be able to read the notes for this to work, obviously. Be careful of accidentals- C♭ to E♯ is a tritone when you hear it)
Also, if you don't feel confident with working with intervals, you can convert them all into half-steps and then put the difference as the range.


JOptionPane.showInputDialog()

Question: So I have the method JOptionPane.showInputDialog() which returns a String that is the value input by the user.
The Dialog box has an OK and a Cancel button.
How can I check so that I know whether the user clicked OK or Cancel?
Comment: there aren't another correct answer as posted by (@Крысa)
Answer: JOptionPane.showInputDialog() will return the string the user has entered if the user hits ok, and returns null otherwise. Therefore, you can just check to see if the resultant string is null. The javadocs on the method here are not terribly explicit about this, but where the return behavior is mentioned, it is indicated to be so.
Comment: @Yokhen yes, it does, you can always verify yourself by printing out the raw result from the call, as well
Comment: So if the user fills in the text field but clicks cancel, it returns null?
Comment: +1, for the first answer and for suggesting that the poster can easily test this.
Answer: From Java documentation:
Returns:
    user's input, or null meaning the user canceled the input

JOptionPane.showInputDialog()


Exchange Online and on-prem AD

Question: A few years ago, we moved all of our on-prem Exchange 2010 users to Exchange Online using a full hybrid migration. When have no on-prem Exchange servers anymore.
Many of the changes we make to mailboxes, etc., requires the use of the Exchange 2010 console (which doesn't support modern auth, and therefore doesn't work anymore) or editing AD attributes directly.
I have recently discovered that I can install the Exchange 2019 management tools and use the open source project Exchange Recipient Admin, which is nice.
But is there a way to move my source of authority for all things Exchange to Exchange Online and Azure AD, which still keeping Active Directory on-prem for everything else?
Answer:
Is there a way to move my source of authority for all things Exchange
to Exchange Online and Azure AD, which still keeping Active Directory
on-prem for everything else?

Not that I'm aware of. When using Azure AD Connect, the source of authority for all objects synched to Office 365 is the on premises AD. This is still something Microsoft is working on AFAIK. Their recent changes (at the link below) and with Steve Goodman's tool piggybacking off those is a step closer, but we're not quite there yet. While some Exchange things can be managed solely (and only) from the cloud side, like mailbox delegation, most everything else needs to be managed from on-prem.
https://docs.microsoft.com/en-gb/Exchange/manage-hybrid-exchange-recipients-with-management-tools

Exchange Online and on-prem AD


NHL Playoff Clean Sweep

Question: After reading How many times has a 4 game sweep occurred during the Stanley Cup finals?, I was curious if there has ever been a team who has not lost a game during the NHL playoff to win the cup.
Answer:
[Has there] ever been a team who has not lost a game during the NHL playoffs to win the [Stanley] cup?

Since 1939 (the first best-of-seven Stanley Cup finals), this has taken place only twice, albeit during the era in which there were only six teams in the league (and two playoff rounds).

1960 - Montreal Canadiens
1952 - Detroit Red Wings

Between 1926-1938, this took place five times:

1930 - Montreal Canadiens
1929 - Boston Bruins
1927 - Ottawa Senators
1926, 1935 - Montreal Maroons

Since 1939 (the first best-of-seven Stanley Cup finals), the following teams lost only once on their way to the Stanley Cup:

1948, 1949 - Toronto Maple Leafs
1944, 1946, 1968 †, 1976 ‡ - Montreal Canadiens

 † - 12 teams in the league (three playoff rounds)  ‡ - 18 teams in the league (three playoff rounds, division winners earned a bye to the quarterfinals)
Between 1926-1938, the following teams lost only once on their way to the Stanley Cup:

1936 - Detroit Red Wings
1934 - Chicago Black Hawks
1933 - New York Rangers
1932 - Toronto Maple Leafs
Comment: @akadian There are the [Edmonton Oilers of 1988](http://www.hockey-reference.com/playoffs/NHL_1988.html), who lost twice over four rounds. In general, however, nobody has come closer than that in four playoff rounds.
Comment: Damn, so Canadiens are the only ones with more than 2 rounds to ever come close? Makes me feel good as a fan (granted I did expect them to be part of this stat if they were) 2 rounds is less impressive, thanks for including only 1 loss with 3 rounds.

NHL Playoff Clean Sweep


Is it possible to train a neural network without backpropagation?

Question: Many neural network books and tutorials spend a lot of time on the backpropagation algorithm, which is essentially a tool to compute the gradient.
Let's assume we are building a model with ~10K parameters / weights. Is it possible to run the optimization using some gradient free optimization algorithms?
I think computing the numerical gradient would be too slow, but how about other methods such as Nelder-Mead, Simulated Annealing or a Genetic Algorithm?
All the algorithms would suffer from local minima, why obsessed with gradient?
Comment: With 3 upvoted answers, this does not seem too broad to be answerable to me.
Comment: Yeah, you don't have to worry much about Nelder-Mead getting stuck at a local minimum, because you'll be lucky if it gets anywhere useful.
Comment: @FranckDernoncourt I interpreted the other question as "why not use *global* optimization techniques to train neural networks?", whereas this one is more "why not use *derivative-free* optimzers ...".
Comment: Possible duplicate of [In neural nets, why use gradient methods rather than other metaheuristics?](http://stats.stackexchange.com/questions/207450/in-neural-nets-why-use-gradient-methods-rather-than-other-metaheuristics)
Comment: BTW, ultra L-BFGS, give that a whirl.  it might be good, but it's so obscure probably no one has even tried it on neural networks. See equation 2.9 on p. 12 (you need to read the preceding few pages to understand the formula, though) of http://www.maths.dundee.ac.uk/nasc/na-reports/NA149_RF.pdf (not called ultra BFGS in the paper), which then would need to get into an "L" (limited memory) version to be ultra L-BFGS, rather than ultra BFGS. The non-L version is laid out in the paper. Ultra BFGS is basically a souped-up ("hot rod") BFGS - can be faster, but might be a little wilder.
Comment: afaik, only feed-forward neural nets can be trained with backpropagation (because the partial derivatives are easy to compute from the chain rule).  If you look at more general neural nets (recurrent neural nets, spike coupled neural nets, etc) then you typically try another method (genetic algorithms are the one's I'm most familiar with).
Comment: @GeoMatt22 I see, vote retracted, I let the OP decides :)
Comment: As already mentioned, depending on the architecture it's possible to do away with back propagation. Deep belief Ns (DBN), cascade correlation NNs and Random Vector Functional Link (RVFL) NNs don't use back propagation at all.
Comment: Related https://stats.stackexchange.com/questions/559251/simulated-annealing-for-deep-learning-why-is-gradient-free-statistical-learning
Answer: The first two algorithms you mention (Nelder-Mead and Simulated Annealing) are generally considered pretty much obsolete in optimization circles, as there are much better alternatives which are both more reliable and less costly. Genetic algorithms covers a wide range, and some of these can be reasonable.
However, in the broader class of derivative-free optimization (DFO) algorithms, there are many which are significantly better than these "classics", as this has been an active area of research in recent decades. So, might some of these newer approaches be reasonable for deep learning?
A relatively recent paper comparing the state of the art is the following:

Rios, L. M., & Sahinidis, N. V. (2013) Derivative-free optimization: a review of algorithms and comparison of software implementations. Journal of Global Optimization.

This is a nice paper which has many interesting insights into recent techniques. For example, the results clearly show that the best local optimizers are all "model-based", using different forms of sequential quadratic programming (SQP).
However, as noted in their abstract "We find that the ability of all these solvers to obtain good solutions diminishes with increasing problem size." To give an idea of the numbers, for all problems the solvers were given a budget of 2500 function evaluations, and problem sizes were a maximum of ~300 parameters to optimize. Beyond O[10] parameters, very few of these optimizers performed very well, and even the best ones showed a noticable decay in performance as problem size was increased.
So for very high dimensional problems, DFO algorithms just are not competitive with derivative based ones. To give some perspective, PDE (partial differential equation)-based optimization is another area with very high dimensional problems (e.g. several parameter for each cell of a large 3D finite element grid). In this realm, the "adjoint method" is one of the most used methods. This is also a gradient-descent optimizer based on automatic differentiation of a forward model code.
The closest to a high-dimensional DFO optimizer is perhaps the Ensemble Kalman Filter, used for assimilating data into complex PDE simulations, e.g. weather models. Interestingly, this is essentially an SQP approach, but with a Bayesian-Gaussian interpretation (so the quadratic model is positive definite, i.e. no saddle points). But I do not think that the number of parameters or observations in these applications is comparable to what is seen in deep learning.
Side note (local minima): From the little I have read on deep learning, I think the consensus is that it is saddle points rather than local minima, which are most problematic for high dimensional NN-parameter spaces.
For example, the recent review in Nature says "Recent theoretical and empirical results strongly suggest that local minima are not a serious issue in general. Instead, the landscape is packed with a combinatorially large number of saddle points where the gradient is zero, and the surface curves up in most dimensions and curves down in the remainder."
A related concern is about local vs. global optimization (for example this question pointed out in the comments). While I do not do deep learning, in my experience overfitting is definitely a valid concern. In my opinion, global optimization methods are most suited for engineering design problems that do not strongly depend on "natural" data. In data assimilation problems, any current global minima could easily change upon addition of new data (caveat: My experience is concentrated in geoscience problems, where data is generally "sparse" relative to model capacity).
An interesting perspective is perhaps

O. Bousquet & L. Bottou (2008) The tradeoffs of large scale learning. NIPS.

which provides semi-theoretical arguments on why and when approximate optimization may be preferable in practice.
End note (meta-optimization): While gradient based techniques seem likely to be dominant for training networks, there may be a role for DFO in associated meta-optimization tasks.
One example would be hyper-parameter tuning. (Interestingly, the successful model-based DFO optimizers from Rios & Sahinidis could be seen as essentially solving a sequence of design-of-experiments/response-surface problems.)
Another example might be designing architectures, in terms of the set-up of layers (e.g. number, type, sequence, nodes/layer). In this discrete-optimization context genetic-style algorithms may be more appropriate. Note that here I am thinking of the case where connectivity is determined implicitly by these factors (e.g. fully-connected layers, convolutional layers, etc.). In other words the $\mathrm{O}[N^2]$ connectivity is $not$ meta-optimized explicitly. (The connection strength would fall under training, where e.g. sparsity could be promoted by $L_1$ regularization and/or ReLU activations ... these choices could be meta-optimized however.)
Comment: @seanv507 While the claim has been made by the major proponents, there are peer reviewed articles from machine learning's top conferences which evaluate those claims rigorously, e.g. https://arxiv.org/abs/1406.2572. By now, that claim is widely accepted in the wider ML community, mostly due to its superior theoretical arguments and empirical evidence. I don't think an ad hominem argument is adequate here.
Comment: The 'review' you quote is from the major proponents of neural nets; I would question the claim about local minima - a well known theoretical criticism of NNs is precisely that any complex model cannot be optimised by gradient descent because it will get stuck in local minima. It is not clear whether it is only the successes of nns that can be solved with backdrop and you do not hear about the failures.
Comment: @GeoMatt22 Contrastive divergence is a special approximation to the gradient of a special class of models, which RBMs fall under. It should be noted that RBMs are probabilistic models which imply a certain kind of distribution, for which the gradient of the maximum likelihood estimate is intractable. Neural networks are computational models, which can be used without any probabilistic starting point, e.g. via optimising a hinge loss. Long story short, CD is not a general mean to optimise neural networks.
Comment: I agree that DL theory is lacking. Still you have to acknowledge that articles like this one are advancing that. If you feel that the article is stating wrong results and the conclusions (such as "local minima are less of a problem than saddle points") are invalid, you have to do better than stating yet another ad hominem attack, this time aimed at the ML community as a whole.
Comment: Recent work shows that with random initialization, gradient descent converges to a local minimum (rather than a saddle point). Paper here: http://arxiv.org/abs/1602.04915 and blog post here: http://www.offconvex.org/2016/03/24/saddles-again/  On the other hand, there's a (less) recent hypothesis that in large neural networks, the local minima are about as good as the global, discussed here: http://stats.stackexchange.com/questions/203288/understanding-almost-all-local-minimum-have-very-similar-function-value-to-the
Comment: @DavidR thank you for the references, which I will read when I get a chance. My impression of the comments I had seen w.r.t. saddles vs. local minima was that the deep NN's have many "good" local minima, so the concern was stagnating on saddles on the way to *getting* to one of the minima? (I will read your references though ... my impression was from some LeCun talks online, so not very reliable :) )
Comment: @seanv507 fair enough for the quote in my side note. I did not intend to advocate any position, and quoted the proponents on purpose. The bulk of my answer is independent, all from decidedly *not* the machine learning community. Do you have examples of some area where optimization problems with > 100's of variables are regularly solved and the method of choice is *not* some form of gradient descent (essentialy with the *goal* of getting a local minima)?
Comment: I was only commenting on the side note. I don't have examples- I am arguing from the opposite point of view - that typically you need to break the problem down into components and optimise separately ( which is not what is done with NNs); I totally agree that gradient descent is the method of choice for NNS
Comment: Question for NN experts: From the Wikipedia description, [Contrastive Divergence](https://en.wikipedia.org/wiki/Restricted_Boltzmann_machine) seems like a *non-backprop* gradient-based training method (at least for RBMs). **Is this true?** (If so, I can amend my answer with a line to this effect.)
Comment: @bayerj I am fully aware of these sort of articles ( that one linked cowritten by Bengio), but they are of the form lets take some maths theory (eg random matrix theory) and then lets assume it also applies to NNs - the 'theory' is used as a metaphor: in the linked paper the key phrase  is 'the theory suggests...' - normally theories are used to prove things.  The problem is that the ML peers are frankly unable to evaluate the maths (or whether it is relevant).
Comment: @bayerj thank you for the CD clarification. As I understand it, CD applies only to RBMs, which are essentially a Markov Random Field on a Bipartite graph. If this is correct, then that topological constraint means RBMs are certainly not "deep". (Not sure if the CD approach would apply to more complex MRFs? In computer vision I think [graph-cuts](https://en.wikipedia.org/wiki/Graph_cuts_in_computer_vision) are popular for energy-based MRFs)
Comment: It looks like your idea of using genetic-style algorithms to optimize network architecture is a fairly mature field of study now: https://ai.googleblog.com/2018/03/using-evolutionary-automl-to-discover.html
Answer: Well, the original neural networks, before the backpropagation revolution in the 70s, were "trained" by hand. :)
That being said:
There is a "school" of machine learning called extreme learning machine that does not use backpropagation.
What they do do is to create a neural network with many, many, many nodes --with random weights-- and then train the last layer using minimum squares (like a linear regression). They then either prune the neural network afterwards or they apply regularization in the last step (like lasso) to avoid overfitting. I have seen this applied to neural networks with a single hidden layer only. There is no training, so it's super fast. I did some tests and surprisingly, these neural networks "trained" this way are quite accurate.
Most people, at least the ones I work with, treat this machine learning "school" with derision and they are an outcast group with their own conferences and so on, but I actually think it's kind of ingenuous.

One other point: within backpropagation, there are alternatives that are seldom mentioned like resilient backproagation, which are implemented in R in the neuralnet package, which only use the magnitude of the derivative. The algorithm is made of if-else conditions instead of linear algebra. They have some advantages over traditional backpropagation, namely you do not need to normalize your data because they do not suffer from the vanishing  gradient problem.
Comment: @MarkL.Stone I don't know anyone who has done backpropagation by first applying a linear regression to the latter layer. It sounds interesting though.
Comment: As far as I know, the controversy around ELMs is mostly due to ethical aspects, not implementation. Schmidt et al had already touched the subject in 1992, with their Feedforward Network with random weights.
Comment: Cab you do (most or all of) the spiel in your 4th paragraph, and then use the result as the starting point for a derivative based optimization to "fine tune" it.
Comment: AFAIK, extreme learning doesn't work with reinforcement learning problems, only for labeled sets. too bad! really clever approach, it is like GPU computing, kind of like providing a parallel input for all possible mahtmatical functions to the last layer and then the last layer would pick (and combine) the ones that work.
Comment: This is an interesting approach I didn't pay attention before. Thanks for sharing.
Answer: There are all sorts of local search algorithms you could use, backpropagation has just proved to be the most efficient for more complex tasks in general; there are circumstances where other local searches are better.
You could use random-start hill climbing on a neural network to find an ok solution quickly, but it wouldn't be feasible to find a near optimal solution.
Wikipedia (I know, not the greatest source, but still) says

For problems where finding the precise global optimum is less important than finding an acceptable local optimum in a fixed amount of time, simulated annealing may be preferable to alternatives such as gradient descent.

source
As for genetic algorithms, I would see Backpropagation vs Genetic Algorithm for Neural Network training
The main case I would make for backprop is that it is very widely used and has had a lot of great improvements. These images really show some of the incredible advancements to vanilla backpropagation.
I wouldn't think of backprop as one algorithm, but a class of algorithms.
I'd also like to add that for neural networks, 10k parameters is small beans. Another search would work great, but on a deep network with millions of parameters, it's hardly practical.
Answer: You can use pretty much any numerical optimization algorithm to optimize weights of a neural network. You can also use mixed continous-discrete optimization algorithms to optimize not only weights, but layout itself (number of layers, number of neurons in each layer, even type of the neuron).
However there's no optimization algorithm that do not suffer from "curse of dimensionality" and local optimas in some manner
Answer: You can also use another network to advise how the parameters should be updated.
There is the Decoupled Neural Interfaces (DNI) from Google Deepmind. Instead of using backpropagation, it uses another set of neural networks to predict how to update the parameters, which allows for parallel and asynchronous parameter update.
The paper shows that DNI increases the training speed and model capacity of RNNs, and gives comparable results for both RNNs and FFNNs on various tasks.

The paper also listed and compared many other non-backpropagation methods

Our synthetic gradient model is most analogous to a value function
  which is used for gradient ascent [2] or a value function used for
  bootstrapping. Most other works that aim to remove backpropagation do
  so with the goal of performing biologically plausible credit
  assignment, but this doesn't eliminate update locking between layers.
  E.g. target propagation [3, 15] removes the reliance on passing
  gradients between layers, by instead generating target activations
  which should be fitted to. However these targets must still be
  generated sequentially, propagating backwards through the network and
  layers are therefore still update- and backwardslocked. Other
  algorithms remove the backwards locking by allowing loss or rewards to
  be broadcast directly to each layer – e.g. REINFORCE [21] (considering
  all activations are actions), Kickback 1, and Policy Gradient
  Coagent Networks [20] – but still remain update locked since they
  require rewards to be generated by an output (or a global critic).
  While Real-Time Recurrent Learning [22] or approximations such as [17]
  may seem a promising way to remove update locking, these methods
  require maintaining the full (or approximate) gradient of the current
  state with respect to the parameters. This is inherently not scalable
  and also requires the optimiser to have global knowledge of the
  network state. In contrast, by framing the interaction between layers
  as a local communication problem with DNI, we remove the need for
  global knowledge of the learning system. Other works such as [4, 19]
  allow training of layers in parallel without backpropagation, but in
  practice are not scalable to more complex and generic network
  architectures.
Answer: As long as this is a  community question , I thought I would add another response.  "Back Propagation" is simply the gradient descent algorithm.  It involves using only the first derivative of the function for which one is trying to find the local minima or maxima.  There is another method called Newton's method or Newton-Raphson which involves calculating the Hessian and so uses second derivatives. It can succeed in instances in which gradient descent fails.  I am told by others more knowledgeable than me, and yes this is a second hand appeal to authority, that it is not used in neural nets because calculating all the second derivatives is too costly in terms of computation.


python tkinter listbox: adding items

Question: At program startup, I add some items to my listbox like this:
for widget in WidgetNames:
    listbox.insert(0, widget)

WidgetNames is obviously a list of some items, e.g. "Button" and "Canvas". The thing is, the listbox doesn't show the items that are added with above code. However,
for widget in WidgetNames:
    listbox.insert(0, widget)
    print(listbox.get(0))

prints "Button" and "Canvas", and
for widget in WidgetNames:
    listbox.insert(0, widget)
print(listbox.size())

prints 2, which obviously is the correct number of items it contains. All the listbox shows after items are added is an empty line. I've tried listbox.see(0) and listbox.index(0), but that didn't help.
Any ideas why the items aren't added properly?
Answer: The code you're showing is not the problem -- it must be some other code which you are not showing. Please try to reproduce your problem in as small a compass as possible and edit your answer to include that minimal code.  Here's a small script to show that the code you show is actually OK:
from Tkinter import *

master = Tk()
listbox = Listbox(master)
listbox.pack()

WidgetNames = ['Button', 'Canvas']
for widget in WidgetNames:
    listbox.insert(0, widget)

mainloop()

This code runs fine on my box (Ubuntu 10.4, Python 2.6) and, exactly as expected, shows the two items ('Canvas' first).  If this does not behave that way on your box, please edit your answer to provide the minutest details about said box;-).
Comment: @Rawing, well, glad to have helped (by showing it's not Tkinter's fault but yours).  To keep your code under control, break it into modules and functions of _far_ smaller sizes than 1800 lines!
Comment: ARGH!! Okay, it's not tkinter's fault, but mine. I accidentally deleted all items abit later in the program and added an empty one... Sorry :(
(Well, my cute little program has more than 1800 lines, so I keeping track of everything that happens is pretty tough :/ )

python tkinter listbox: adding items


How to search and copy information from two tables in SQLite on Java?

Question: Beginner here.
I have this problem: I have two tables in my database. Table Customers and table Parcels. The program should find a customers id number from table Customers, when the user types the customers name. Then copy the customers id to table Parcels under column "customer_id".
Here is what I have done, but NetBeans gives me the Index out of bound exception. I have also tried another way, but then the program thinks I am trying to access a column by the name of the users feed. (I have created the tables in another if clause) What should I do differently? Here is my code:
            } if (feed.equals("4")) {
                System.out.println("Add tracking number: ");
                String tn = input.nextLine();
                PreparedStatement a = db.prepareStatement("INSERT INTO Parcels(tracknumb,cust_id) VALUES(?,?)", Statement.RETURN_GENERATED_KEYS);
                a.setString(1, u);

                System.out.println("Add customer name: ");
                String nameid = input.nextLine();

                PreparedStatement y = db.prepareStatement("SELECT id FROM Customers WHERE name= ?");

                y.setString(2, nameid);
                ResultSet rl = y.executeQuery(nameid);
                if(rl.next()) {
                    int id = rl.getInt(nameid);

                    a.setInt(2, id);

Answer: I'm pretty sure the problem is within your SQL requests.
You're trying to insert ? into one of your values in the database, but keep in mind that ? is something unknown to the SQLite database.
If you're trying to insert an empty value, insert NULL. If you're trying to insert something from somewhere else, try {0}, and add , yourValue.
Try this out and see if it works.
Note: If you still want to use questions marks within your database, type \"?\" or '?'. This way, if you've configured your table to use strings or chars, this will work.


Como funciona o XOR para dois binários com mais de um dígito?

Question: Eu aprendi que o operador XOR funciona como OU Exclusivo, ou seja, o resultado final só é 1 quando apenas um dos operadores for igual a 1.
A tabela verdade ilustra bem isto:

A minha dúvida é: como funciona o operador XOR com números que não sejam de apenas 1 bit?
Exemplo: 110 XOR 011 deveria retornar qual resultado?
Em JavaScript (ou qualquer outra linguagem) consigo ver que o resultado deve ser 101, mas como chegar neste resultado?

var a = 6; // 110
var b = 3; // 011
var res = (a ^ b).toString(2);

log('a:', a.toString(2));
log('b:', b.toString(2));
log('res.:', res);

// Apenas pra melhor visualização
function log(label, valor) {
    var lb = padLeft(' ', 5, label);
    var val = padLeft('0', 3, valor, true);

    console.log(lb, val);
}

function padLeft(padChar, padSize, str, left) {
    const pad = padChar.repeat(padSize);
    if(left)
        return (pad + str).slice(-padSize);
    else
        return (str + pad).substring(0, padSize);
}
Answer: O sistema de numeração binário não funciona diferente do sistema decimal. Assim como a soma ou multiplicação funciona igual, os operadores lógicos também. Portanto o XOR é feito bit a bit, como você somaria um decimal que é feito algarismo por algarismo, com a vantagem que não tem "vai um" nos operadores lógicos, nem faria sentido porque nessa operação os dígitos não possuem relação de grandeza, como na aritmética que um resultado em uma grandeza pode afetar o resultado da outra. Então:
110  => 1  1  0 (pra visualizar melhor)
011  => 0  1  1
---     -------
101  => 1  0  1

Só o do meio deu 0 porque ele não é exclusivo. Teria dado 0 se fosse 0 e 0, como no OR normal.
Coloquei no GitHub para referência futura.
Answer: O XOR é um mod 2, portanto se fossem:
110
011

Seria igual:
(1 + 0) mod 2, (1 + 1) mod 2, (1 + 0) mod 2

Assumindo que , fosse concatenação, veja isso no WolfgramAlpha.

Quando se faz isto com duas letras, por exemplo, literalmente H e u, por exemplo:
<?php

echo 'H' ^ 'u';
// Resultado: "="

Teste isto.
Na verdade isso ocorre porque seus valores binários são utilizados, pode também achar a tabela aqui.
01001000 // str_pad(decbin(unpack('C', 'H')[1]), 8, 0, STR_PAD_LEFT);
01110101 // str_pad(decbin(unpack('C', 'u')[1]), 8, 0, STR_PAD_LEFT);

Aplicando a mesma formula acima teríamos:
00111101

Que possui em ASCII (assim com em UTF-8) o valor de =, lembrando que nem todo o ASCII pode sem "representado".
Portanto se fizemos:
echo pack('C', bindec('00111101'));
// Resultado: "="

É isto que é feito por trás dos panos, praticamente.

Tentarei refazer este exemplo em Javascript para ficar mais fácil de visualizar.


Negation for the formula

Question: Could someone verify my answer please? Thanks
Comment: No, its not correct. Correct is the last option.
Answer: Negating an 'there exists' or 'for all' generally consists of two steps: switch the quantifier, and negate the nested statement.
So, 'there exists $x$ such that $P(x)$' negates to 'for all $x$, not $P(x)$'; similarly, 'for all $x$, $P(x)$' negates to 'there exists $x$ such that not $P(x)$.
So in this case: the negation is "there exists $x\in\mathbb{Z}$ such that not ($p(x)$ and $q(x)$)".  We then use DeMorgan's laws to simplify: the statement "not ($p(x)$ and $q(x)$)" simplifies to "(not $p(x)$) or (not $q(x)$)".
So, the last answer is the correct one.

Negation for the formula


Forçar abrir Facebook/Twitter/Google no browser

Question: Criei um app para mobile que é acessado via browser (um Web app, um site comum).
Nele tenho um botão que abre via window.open() uma página no Facebook, porém quando clico no botão, ele chama o aplicativo do Facebook.
Eu preciso que a página abra no próprio navegador do celular.
já tentei como window.open(url, '_system', 'location=yes'), mas não funciona, acho que só serve para apps instalados.
Existe alguma solução?
Comment: Oi Paulo, é um Web app, um site.
Comment: Só queria abrir uma página do Facebook no próprio navegador do Android, mas quando clico no botão ele pergunta se quero abrir com o App do Facebook ou com o navegador. Queria abrir direto com o navegador, sem perguntar. Será que tem como?
Answer: Tente isto,
HTML:

Google

JavaScript:
document.getElementById("teste").addEventListener("click", function(evt) {
    var a = document.createElement('a');
    a.setAttribute("href", this.getAttribute("data-href"));
    a.setAttribute("target", "_blank");
    var dispatch = document.createEvent("HTMLEvents");
    dispatch.initEvent("click", true, true);
    a.dispatchEvent(dispatch);
}, false);

Qualquer coisa posta aí.


How to print Multiple div element with fixed width and height without changing this size?

Question: In My Company, There is some perforated paper. any section of these paper should be filled by employees, then cut and use as an information card.
I want write some code with ASP.net core, that write some information in this card, after some transaction.
I designed preview of this card in html page (each of cards display with a div that its height and width is fixed), but when I want print this page, sizes of div isn't correct, and is different of div sizes. This problem cause printed cards doesn't match with perforated papers.
Code of preview page: Preview.cshtml
@model InputList
@{
    Layout = "_PrintLayout";
}

                  @for(int i = 0; i < Model.Count(); i = i + 2)
                        {

                                         test

test test test

test test test test

                                    @if (i+1 < Model.Count())
                                    {

                                            test 2

 test2 test 2 test 2

test2 test 2

                                    }

                        }

>

                 print

style: app.css
.InputCard {
    height: 15.82920199423rem !important;
    width: 21.9456027648rem !important;
    margin-bottom: 0.37080004671497rem !important;
    max-height: 15.82920199423rem !important;
    max-width: 21.9456027648rem !important;
    border-radius: 2px !important;
    border: 1px solid !important;
    box-shadow: none !important;
    -webkit-box-shadow: none !important;
    -moz-box-shadow: none !important;
}

Can I print preview page with exact div size (in td block)?
Comment: `This problem cause printed cards doesn't match with perforated papers.` Can you capture a screenshot to explain more detail about the problem, it might be easier let us understand and help you fix it.
Answer: Since you are using the window.print() to print the web page, it will use the browser's print option to print the page. So which kind of browser you are using to print the document? Generally, when print the web page using a browser, we can set the document margins on the paper.
So, you can try to the browser print preview and the Margins settings to change the print result.
You can refer to the following screenshot, it using Edge browser:
Comment: I use google chrome browser.
Comment: Chrome browser also has the same setting, you can check it.
Comment: As mentioned, My problem is because of perforated paper, that need to set any card in a exact place in paper. and margin of paper doesn't help to solve my problem.
Answer: You should create a separate print css using media query types. This way you can define the input card to match for example A4 paper size without affecting the web sites layout.
https://www.w3schools.com/css/css3_mediaqueries.asp
@media print {
 .InputCard {
  height: 500px !important;
  width: 200px !important;
 }
}

Comment: Unfortunately, this solution did not work! :(


how can I get `parentSectionOrNullObject` Range in word? using Office javascript Api?

Question: I am using this but this is giveing error TypeError: Cannot read properties of undefined (reading 'isNullObject') when I am using breakpoint then this is give selection.parentSectionOrNullObject undefined.no idea why irs gives error.

 async function getSectionRange() {
     try {
         await Word.run(async (context) => {
             const selection = context.document.getSelection();
             selection.load('parentSectionOrNullObject');

             await context.sync();

             if (!selection.parentSectionOrNullObject.isNullObject) {
                 const parentSection = selection.parentSectionOrNullObject;
                 const sectionRange = parentSection.getRange("Whole");

                 context.load(sectionRange, 'text');

                 await context.sync();

                 console.log('Section Range Text:', sectionRange.text);
             } else {
                 console.log('The selection is not within a section.');
             }
         });
     } catch (error) {
         console.error('Error: ', error);
     }
 }
Answer: Your selection variable is a Range object. Range does not have a parentSectionOrNullObject property. That property is only found on the Body object. As I explained in my answer to your other question: how can I get Word.Section Range using office javascript api?, you need to get the parent body of selection first.


Can a generating set for a polynomial ideal have less elements than a minimal Gröbner base?

Question: Let $\mathbb{K}$ be a field and $I\subset \mathbb{K}[x_1, \dots, x_n]$ an ideal. Then minimal Gröbner bases for $I$ (with respect to same monomial order) have the same number of elements, which is proven in e.g. https://www.kent.ac.uk/smsas/personal/gdb/MA574/week6.pdf. Let's call this number $k$.
Is it then possible that there exists a (non-Gröbner) generating set for $I$  with less than $k$ elements?
Comment: Actually when you ask for a Grobner base you are asking much more than a generating set so it makes sense that the generating sets (as they have less restrictions) have fewer elements.
Comment: That is true! I did not think of that.
Answer: Let $I=\langle x^3-2xy, x^2y-2y^2+x \rangle$ be an ideal in $K[x,y]$ with  grlex and $y\prec x$.
Then a minimal Gröbner basis has $k=3$ elements, e.g.,
$$
G=\{ x^2,xy,y^2-\frac{1}{2}x \}.
$$
But $I$ is generated by $2$ elements.