Understanding iotop (DTT) output in Solaris

Question: When running DTT iotop on a write heavy Solaris 10 server, which runs multiple zones with MySQL daemons installed, I get the following output:

  UID    PID   PPID CMD              DEVICE  MAJ MIN D            BYTES
   70  26636      1 mysqld           sd1      10  64 R           360448
   70  25940      1 mysqld           sd1      10  64 R           530432
    0      5      0 zpool-rpool      sd1      10  64 W         17250816

What bothers me here is the fact that zpool-rpool takes up most of the io. What can I do to see which of the MySQL or other processes really takes up the IO - a more elaborate breakdown? If zpool-rpool represents "writes to ZFS", then iotop is really not helping me here... :)
Thanks!
Answer: You might find Brendan Gregg's recent blog series on filesystem latency useful. He shows a couple of scripts for investigating filesystem usage with the syscall provider (which should be more reliable for identifying the responsible processes than the io provider used by iotop).
For example, the syscall-read-zfs.d script shown in Part 4 could easily be modified to probe on writes and aggregate on pid rather than execname.
The output of this script may also be more useful than iotop because it shows the number of IOs and the distribution of IO latency per process. For a database, the latency of reads and synchronous writes are direct measures of performance pain - much easier to interpret than bytes per second.
If you have time, I also highly recommend watching his presentation at BayLISA for a hands-on demonstration of how he goes about investigating MySQL query performance issues.
Answer: If you want to measure which applications are reading/writing the most, you want to measure at the syscall level. At the device level it's only kernel threads doing their work.


Why str() and type() returns different type names for db.Column?

Question: I am learning the Flask SQLAlchemy.
I initialize the SQLAlchemy normally:
db= SQLAlchemy(app = app)

And define tables with class like this:
class Table1(db.Model):
    col1 = db.Column(db.Integer)

I checked the type of db.Column as below.
str(db.Column)
"<class 'sqlalchemy.sql.schema.Column'>"
type(db.Column)
<class 'sqlalchemy.sql.visitors.TraversibleType'>

Why the types are different?
ADD 1 - 12:17 PM 6/21/2022
Suppose we have:
class C:
   pass

I think below 2 pieces of code are equivalent, just different syntax:
class X(C):
   pass

And:
X = type('X', (C,), {})

But when it comes to the Enum like in this thread, some error happens:
from enum import Enum

class X1(Enum):  # <========= this is OK.
   pass

X2 = type('X2', (Enum,), {}) # <========= This is error

And the error is:
...
  File "/usr/lib/python3.8/enum.py", line 175, in __new__
    enum_members = {k: classdict[k] for k in classdict._member_names}
AttributeError: 'dict' object has no attribute '_member_names'

Not sure why.
Comment: Comparing the difference between the output of `str(object)` and `type(object)`, the answer should become obvious.  Though ShadowRanger already gave a full explanation already so best to read that.
Comment: For those that care about what is under the hood, [this thread, and the answer that I provided](https://stackoverflow.com/questions/72682119/why-typeenumtype-return-enummeta-in-python/) covers the relationship of classes to the `type` "function".
Comment: That's because `Enum` uses the [`__prepare__` classmethod](https://stackoverflow.com/questions/46827708/), where [it provides a custom `classdict` argument as a `_EnumDict`](https://github.com/python/cpython/blob/v3.8.13/Lib/enum.py#L143-L155). While `X2 = type('X2', (Enum,), _EnumDict())` may work in 3.8, it still bypassed the `__prepare__`, and won't work [after this change in 3.9.2](https://github.com/python/cpython/blob/v3.9.2/Lib/enum.py#L170), thus the full equivalent is `type('X2', (Enum,), EnumMeta.__prepare__('X2', (Enum,),))`. Note this one-liner cannot add attributes.
Comment: However, splitting out the `EnumDict` construction by taking the return value of `__prepare__` and then populate it with the desired attributes, e.g.  `clsdict = EnumMeta.__prepare__('Color', (Enum,),)`, and then `clsdict['RED'] = 1`, finally `Color = EnumMeta('Color', (Enum,), clsdict)`, now you can `Color(1)` which will produce an instance ``.
Comment: @metatoaster Thanks for the info. I found some weird thing based on the thread you mentioned. Could you take a look at my ADD 1? Thanks.
Comment: @metatoaster just correct a typo in ADD 1. Mistyped `Enum` to `object`.
Answer: Because db.Column is a class, which has a metaclass of TraversibleType. Even classes are instances of other classes (most are instances of type itself), but in this case it's using a custom metaclass.
str reports the class itself (Column), type reports the metaclass (which is then stringified automatically by the REPL).


How did 10gbe happen without increasing the number of wires in the cable?

Question: The ethernet cable speed jump from 100Mb/s to 1Gb/s was possible because all of the wires were put to use in a patch cable. The jump from 1Gb/s to 10Gb/s required no such increase. How did they do this?
Comment: https://en.wikipedia.org/wiki/10_Gigabit_Ethernet
Answer: See this page for a brief history of Ethernet. Your question infers that you're only concerned about four-pair twisted pair cable, in which case you can search for "8P8C" to see the many different iterations that used the same cable.
In short, the reason for the speed increases is more or less the same as why you can now hold in your hand orders of magnitude more computing power than a desktop computer was capable of ten years ago.


How to fix error starting Windows SBS 2003 - STOP: c0000218?

Question: I recently lost a hard drive RAID 5 array controller which caused total loss of the array. I am able to restore using our Shadow Protect backup. After the restore, when I start Windows 2003 server, I receive the following stop message:

STOP: c0000218 {Registry File Failure} The registry cannot load the
  hive (file): \SystemRoot\System32\Config\SOFTWARE or its log or
  alternate. It is corrupt, absent, or not writeable.

Any ideas on how to fix this issue?
Answer: Looks like the SOFTWARE Registry hive got corrupted; this is quite a bad scenario, usually requiring a restore from backup, an O.S. repair from the installation media or, as a last resort, a full reinstallation.
You can try starting the Windows Recovery Console from the O.S. media and then running a disk check (CHKDSK); maybe it's just a file system corruption issue.
You can find some other tips here: http://support.microsoft.com/kb/822705.
Answer: Can you load the SOFTWARE registry hive using a live CD? This will at least you know where to start looking.
Answer: Probably a disk error or controller driver issue.
Comment: Did you need to install dirvers for this controller when you installed Windows, at that point in the install when you press F6 to add SCSI drivers? Or did you boot a manufacturer's CD to install, like HP SmartStart?
Comment: That system was restored with all the same drivers as the original server.
Comment: how would you fix the driver issue with out being able to get into windows ?
Answer: If System Restore has been running you should be able to get a copy of the registry as detailed at KB307545. It is for XP, but should still be applicable.
Comment: Xorlev System restore in Windows 2003 Server is not a supported solution. It involves installing unsupported windows XP operating system files into Server 2003. This is not supported or recommend by Microsoft.
Comment: -1, System Restore exists only in Windows XP, not Windows Server 2003.
Comment: I ask that you revoke your downrate, it does exist in Server 2003. http://www.windowsreference.com/windows-server-2003/how-to-installenable-system-restore-on-win-server-2003/
Answer: First, did you restore to identical hardware? If not, did you use the Hardware Independant Restore feature of ShadowProtect?
Comment: Different hardware and yes we did use the HIR restore option.
Comment: When you did the HIR, you picked the new storage driver for the OS? I've done this a ton of times, and a few of them I have mistakenly chosen the wrong one. But since i had the image, there was the liberty to keep trying with it.
Answer: In our case the software registry file had a CRC error, leaving the Windows 2003 server in an endless boot loop with error number c0000218. I tried the repair file system (chkdsk) from Windows recovery console. No improvement.
I found http://xphelpandsupport.mvps.org/how_do_i_repair_a_stop.htm which has an awful lot of steps.
I tried this less complex solution.

Use the screw driver to remove the Samsung Evo 840 mSATA boot drive from the chassis. Plug that mSATA in an mSATA to USB converter. Fire up Windows XP in a VM. Plug the USB drive. The USB drive mounts without a hitch.
Then create c:\backup\corrupt-registry\ folder.
Copy all these 5 files {system, software, sam, security, default} from c:\windows\system32\config\ to the backup folder. (this is when the OS informs me about the CRC error: 1 file could not be copied)
Delete the 5 files from c:\windows\system32\config\
Copy the 5 files from c:\windows\repair\ to c:\windows\system32\config\
Safely remove the USB drive
Plug the mSATA back into the chassis
Power up the computer

Boot registry issue c0000218 is now fixed.


Symantec Backup Exec 12 not restoring Shared Folders

Question: I am trying to retore shared Folders. They do retore but not with the shared properties.
I have checked the option under general - Restore all files and directories.
Thanks
Vee
Comment: If Evan's answer is the right answer, please accept it.
Answer: I think you're saying that "Sharing Permissions" (and, possibly even the fact that the folders were shared) aren't being restored when you restore folders using Backup Exec on a Windows Server machine. Let me know if I'm wrong.
Unless you restore the machine's "System State" (ultimately to restore the registry) you're not going to get the share-related attributes back. The share-related attributes for a folder aren't stored in the filesystem, but rather are stored as registry values in HKLM\System\CurrentControlSet\Services\LanManServer\Shares. Restoring the folders won't restore this information into the registry of the server.
Comment: Hi Evan - Yes yo are right I meant to say Sharing Permissions as well as the actual share it self. When I go to shared folders after restoring, only the default shares appear.


Filtrar objetos dentro de um array com hash

Question: Eu estou criando um programinha em JS de controle de plantação e na função add(), quero adicionar um filtro para verificar se aquele hash (name) já existe, porém, não estou conseguindo:
let data = [];
let name = {};
data[name];

function menu(){
let option = Number(prompt('1- adicionar  2- apagar  3- exibir  0- sair'));
switch(option){
    case 1: add(); break;
    case 2: break;
    case 3: break;
    default: return 0;
}
}

function add(){
name = prompt('Nome:');
let day = prompt('Dia:');
let mouth = prompt('Mês:');
var exists = data.filter(function(obj){
    return 'name' in obj['name'] != name;
});
console.log(exists);
if (data[name]!=exists){
    alert(`${name} adicionado!`)
    data[name] = {name: name, date:{day:day, mouth:mouth}};
    menu();
}
else if(prompt(`${name} já existe! quer atualizar? S ou N`)!='S'){
    alert('voltando ao menu...');
    menu();
}
data[name] = {name: name, date:{day:day, mouth:mouth}};
}

menu();

Answer: Algumas coisas no seu código não estão corretas, tais como:

A forma como está a fazer a filtragem;
A verificação (if) se o novo item já existe;
A forma como está a adicionar ao array.

Alterei estes pontos no seu código para que funcione como pretende. Porém, ainda há espaço para melhoramentos no seu código.

let data = [];
let name = {};
data[name];

function menu(){
let option = Number(prompt('1- adicionar  2- apagar  3- exibir  0- sair'));
switch(option){
    case 1: add(); break;
    case 2: break;
    case 3: break;
    default: return 0;
}
}

function add(){
name = prompt('Nome:');
let day = prompt('Dia:');
let mouth = prompt('Mês:');
var exists = data.filter(function(obj){
    return obj['name'] === name;
}).length > 0;

if (!exists){
    alert(`${name} adicionado!`)
    data.push({name: name, date:{day:day, mouth:mouth}});
    menu();
}
else if(prompt(`${name} já existe! quer atualizar? S ou N`)!='S'){
    alert('voltando ao menu...');
    menu();
}
data[name] = {name: name, date:{day:day, mouth:mouth}};
}

menu();
Comment: muito obrigado cara, eu estou recém aprendendo esse conteúdo, vou dar uma analisada no código


Calculating Average After Parsing Integers

Question: This is for Java.
I just finished parsing each column from String to int. However, now I am having a tough time figuring out how to calculate each column and finding the average.
For example, this is what I have after I parsed each column:
int Q1 = Integer.parseInt(columns[1]);

This is what I tried to find the average, but was unsuccessful.
int Q1s = Q1;

        int i;
        int total = 0;

        for (i = 0; i < 6; i++) {
            total = total + Q1s;
        }

        double avg = total / Q1s;
        return avg;

I know how to find the average the normal way (Example: int Q1[] = {1,2,3};)
But it is not the same for parsing an array of integers.
Any hints on how to proceed would be greatly appreciated!
P.S. I don't want the answer...I just want a direction on where to go from here. That is why I didn't put the complete code that I currently have.
Comment: Well, `Q1s` never changes during your loop, so you're just multiplying it by 6.  If you left out some other code inside the loop, then we can't help you.  But it looks like the problem is: what happened to the other 5 values you read?  Did you do anything with them?
Comment: why >> double avg = total / Q1s;  and not>>  double avg = total / ; ?
Comment: do you start with two dimension array? you HAVE to put more code for us to see what you are doing
Answer: Your logic to calculate total is wrong. You are not adding all the elements, what you're doing is adding the first element n times.
Assuming Q1 is your array of Strings.
for(int i = 0; i < Q1.length; ++i)
{
 total += Integer.parseInt(Q1[i]);//this is what you want to do. Parse it here
}
double avg = (double) total / Q1.length;

Comment: It would still give the wrong answer without typecast. Int divided by int will give int.
Comment: I knew something was off, but couldn't pinpoint it. I'll try that now. Thanks!
Answer: double avg = total / Q1s;

Calculating average is wrong here. It should be
double avg = total / 6;

It should be divided by the total number of elements here you are
  using 6 as a constant.
Comment: This is wrong because you're doing integer division.
Answer: two things:
1) you should use:
  double avg = total / <ColumnCount>;

2) you need to casting, So
  double avg = ((double)total) / <ColumnCount>;


CanCanCan, Doorkeeper and RSpec

Question: When I try to write tests in RSpec I get an error
CanCan::AuthorizationNotPerformed:
   This action failed the check_authorization because it does not authorize_resource. Add skip_authorization_check to bypass this check.

Although CanCanCan is configured and the controller has
before_action: doorkeeper_authorize!

Tests pass only when I specify in the controller even
skip_authorization_check

Answer: It was necessary to add an action to the configuration of the CanCanCan authorization
Comment: I have the same problem though two my actions without access tokens (on 401) pass. Idk why. Does anyone know?
Comment: Anyway big thanks for clarifying problem, it helped!

CanCanCan, Doorkeeper and RSpec


Ctrl+F and Ctrl+H doesn't work in SQL management studio

Question: I cannot use the Ctrl+F and Ctrl+H in my SQL Management studio. Even if I use the menu items it doesn't work. Haven't installed anything new or changed anything recently. Any ideas?
Comment: He's able to type in StackOverflow though
Comment: Is your keyboard plugged in? Sounds silly, but you wouldn't believe the number of times my foot-whirling has knocked loose a cable or two. I think my app has gone nuts until I realise nothing else works either!
Comment: @ButterChicken, I may be FailBoy but certainly not StupidBoy. lol!

@Patrick, We share a surname so you rock ;)
Answer: Have you recently used multi-monitor? Is it possible that it is opening off-screen? I get this problem a lot when disconnecting my laptop from an external monitor...
(edit) There is a Window -> Reset Window Layout menu option in SSMS 2008, that should fix it.
Comment: I have a single monitor, and yet it's been happening recently. Saved my sanity, thanks!
Comment: Dude you are a champion of note! Thanks a million mate!
Comment: Thank You!!!! . Was so frustrated with this. It took me 15 min to decide to get on stackoverflow.
Answer: Perfect! multi-monitor causes this.. Just minimize VS. Put to the other monitor.
You'll see opened window when you Ctrl+F
Irina
Answer: I currently have this issue also, I am using two monitors but this appears not to be the cause. I say this as the instance of management studio that I have open has two queries - both are connected to the same Server and Instance. On one query, Ctrl+F works fine, on the other query it won't work at all.
I think this is in some way related to debugging - the query which does not allow Ctrl+F currently I mistakenly debugged (as opposed to running) and after this the find/find replace functionality does not work!


Canonical example of a cosheaf

Question: Sheaves can, like all modern mathematical constructions and abstractions, be counterintuitive beasts but, like all such constructions, a few examples can allow one to visualise them simply as a generalisation of a natural object (ie. the sets of local functions on a topological space).
I have recently become rather interested in cosheaves- a sort of natural iteration of the sheaf concept- apply the sheaf functor once, and it's contravariant, twice (being terribly careful about covers being preserved in a sensible way) and one gets a covariant construction.
My problem is that this is rather hard to anchor to anything really natural as 'sheaves over sheaves' seems rather difficult to visualise without tying ones brain in knots. So my question, in its broadest terms is 'what do cosheaves look like?' (are they for example, just $\mathcal{F}^{op}$ to some sheaf $\mathcal{F}$ by analogy with various other 'co-constructions'), but more realistically (assuming that I am making assumptions) it is "Can one find a canonical example of a cosheaf that is 'visualisable' in some sense?"
Answer: So you are probably requiring a more general sort of cosheaf -- I don't really know what you mean by "running the sheaf functor twice" -- but let me maybe provide a simpler description of cosheaves that might answer your question.
One definition of a cosheaf is simply a functor
$F:\mathrm{Open}(X)\to\mathcal{C}$
that sends colimits (unions) to colimits. By formality, we can say this is a sheaf valued in the opposite category
$F:\mathrm{Open}(X)^{op}\to\mathcal{C}^{op}.$
There are a couple of "canonical" examples of cosheaves. One is the cosheaf of compactly supported real valued functions on a space:
$U\rightsquigarrow \{f:U\to\mathbb{R}|\mathrm{supp}(f) \, \mathrm{cpt}\}$
Where the extension function is to extend by zero and partitions of unity should force the cosheaf axiom to hold.
More abstractly, using the colimit-preserving definition, any continuous map of spaces:
$f:X\to Y$
we can build a cosheaf of spaces on $Y$, by assigning to each open set $U\subset Y$
$U\rightsquigarrow f^{-1}(U) \qquad U\cup V \rightsquigarrow f^{-1}(U)\cup f^{-1}(V).$
Another, very closely related canonical example of a cosheaf, is to take $\pi_0(f^{-1}(U))$ and as long as $Y$ is locally connected, this will be a cosheaf. See Jon Woolf's paper The Fundamental Category of a Stratified Space and appendix B in there.
Finally, due to an observation of Bob MacPherson, if we think of a cell complex $X$ as a category with objects the cells $\sigma\in X$ with morphisms given by the face relation $\sigma\subseteq \bar{\tau}$, then a constructible sheaf is equivalent to a functor $F:X\to\mathrm{Vect}$ and a constructible cosheaf $F:X^{op}\to\mathrm{Vect}$. These gadgets are bona fide sheaves and cosheaves in the Alexandrov topology on the associated face relation poset, i.e. open sets are $U\subset X$ such that $x\in U$ $x\leq y$ implies $y\in U$.
Finally, I should say that some of my thesis work applies cosheaves to Morse theory, persistent homology and sensor networks, which should provide some more intuitive examples.
Comment: I had wanted more details on the first "canonical example" for quite some time ( $C(U)$ with what topology, in which category) which I did find [here](http://math.stackexchange.com/a/1515973/92038)
Comment: I know this is an old post, but could you say more about what you mean by "partitions of unity should force the cosheaf axiom to hold"? I feel like if I understood that then would understand what a cosheaf is. Currently I don't see how partitions of unity come into it at all, and I'm struggling to get any intuition for what the cosheaf axiom really says.

Canonical example of a cosheaf


Why does adding dropout layers work on validation set, but not on test set?

Question: I'm working on a convolutional neural network in TensorFlow and having trouble with the dropout layers. As recommended, I'm passing a keep_probability placeholder to the graph and setting the value to 0.5 during training, and 1.0 during validation and testing. When observing the training process, the results are good for the validation set. However, when I test the network after training, the network fails.
UPDATE: When I say that the network fails, I mean that the network no longer segments the images correctly. During validation the network gets an mIoU of around 80%, but when testing it falls down to around 40% and classifies all the pixels into just one of the classes. Before the dropout layers were added, both validation and test set got an mIoU of around 80%.
I do not understand why the network is failing on the test set when it works on the validation set?
I've added the code for training, testing and for the graph itself.
Code for training the network:
with tf.Graph().as_default():
   #Probablitity that the neuron's output will be kept during dropout
    keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")

    global_step = tf.Variable(0, trainable=False)

    images, labels = Inputs.datasetInputs(image_filenames, label_filenames, FLAGS.batch_size)
    val_images, val_labels = Inputs.datasetInputs(val_image_filenames, val_label_filenames, FLAGS.batch_size)

    train_data_node = tf.placeholder(tf.float32, shape=[FLAGS.batch_size, FLAGS.image_h, FLAGS.image_w, 3])
    train_labels_node = tf.placeholder(tf.int64, shape=[FLAGS.batch_size, FLAGS.image_h, FLAGS.image_w, 1])
    phase_train = tf.placeholder(tf.bool, name='phase_train')

    logits = model.inference(train_data_node, phase_train, FLAGS.batch_size, keep_probability) #tensor, nothing calculated yet
    loss = model.cal_loss(logits, train_labels_node)
    # Build a Graph that trains the model with one batch of examples and updates the model parameters.
     train_op = model.train(loss, global_step)

  with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    sess.run(tf.local_variables_initializer())

    """ Starting iterations to train the network """
    for step in range(startstep, startstep + FLAGS.max_steps):
        image_batch ,label_batch = sess.run(fetches=[images, labels])
        # since we still use mini-batches in eval, still set bn-layer phase_train = True
        feed_dict = {
          train_data_node: image_batch,
          train_labels_node: label_batch,
          phase_train: True,
          keep_probability: 0.5
        }

        _, loss_value = sess.run(fetches=[train_op, loss], feed_dict=feed_dict)

        if step % 10 == 0:
          num_examples_per_step = FLAGS.batch_size
          examples_per_sec = num_examples_per_step / duration
          sec_per_batch = float(duration)

          # eval current training batch pre-class accuracy
          pred = sess.run(fetches=logits, feed_dict=feed_dict)

          Utils.per_class_acc(pred, label_batch)

        if step % 100 == 0 or (step + 1) == FLAGS.max_steps:
          """ Validate training by running validation dataset """
          total_val_loss = 0.0
          hist = np.zeros((FLAGS.num_class, FLAGS.num_class))
          for test_step in range(TEST_ITER):
            val_images_batch, val_labels_batch = sess.run(fetches=[val_images, val_labels])
            feed_dict = {
              train_data_node: val_images_batch,
              train_labels_node: val_labels_batch,
              phase_train: True,
              keep_probability: 1.0 #During testing droput should be turned off -> 100% chance for keeping variable
            }
            _val_loss, _val_pred = sess.run(fetches=[loss, logits], feed_dict=feed_dict)

 (...)

Code for testing the network:
keep_probability = tf.placeholder(tf.float32, name="keep_probabilty")

  image_filenames, label_filenames = Inputs.get_filename_list(FLAGS.test_dir)
  test_data_node = tf.placeholder(tf.float32, shape=[testing_batch_size, FLAGS.image_h, FLAGS.image_w, FLAGS.image_c])  #360, 480, 3
  test_labels_node = tf.placeholder(tf.int64, shape=[FLAGS.test_batch_size, FLAGS.image_h, FLAGS.image_w, 1])

  phase_train = tf.placeholder(tf.bool, name='phase_train')

  logits = model.inference(test_data_node, phase_train, testing_batch_size, keep_probability)
  loss = model.cal_loss(logits, test_labels_node)
  pred = tf.argmax(logits, dimension=3)

  with tf.Session() as sess:
    # Load checkpoint
    saver.restore(sess, FLAGS.model_ckpt_dir)

    images, labels = Inputs.get_all_test_data(image_filenames, label_filenames)
    threads = tf.train.start_queue_runners(sess=sess)
    hist = np.zeros((FLAGS.num_class, FLAGS.num_class))
    step=0
    for image_batch, label_batch  in zip(images, labels):
      feed_dict = { #maps graph elements to values
        test_data_node: image_batch,
        test_labels_node: label_batch,
        phase_train: False,
        keep_probability: 1.0 #During testing droput should be turned off -> 100% chance for keeping variable
      }

      dense_prediction, im = sess.run(fetches=[logits, pred], feed_dict=feed_dict)
(...)

The graph:
def inference(images, phase_train, batch_size, keep_prob):
  conv1_1 = conv_layer_with_bn(images, [7, 7, images.get_shape().as_list()[3], 64], phase_train, name="conv1_1")
  conv1_2 = conv_layer_with_bn(conv1_1, [7, 7, 64, 64], phase_train, name="conv1_2")
  dropout1 = tf.layers.dropout(conv1_2, rate=(1-keep_prob), training=phase_train, name="dropout1")
  pool1, pool1_indices = tf.nn.max_pool_with_argmax(dropout1, ksize=[1, 2, 2, 1],
                                                    strides=[1, 2, 2, 1], padding='SAME', name='pool1')
  conv2_1 = conv_layer_with_bn(pool1, [7, 7, 64, 64], phase_train, name="conv2_1")
  conv2_2 = conv_layer_with_bn(conv2_1, [7, 7, 64, 64], phase_train, name="conv2_2")
  dropout2 = tf.layers.dropout(conv2_2, rate=(1-keep_prob), training=phase_train, name="dropout2")
  pool2, pool2_indices = tf.nn.max_pool_with_argmax(dropout2, ksize=[1, 2, 2, 1],
                                                    strides=[1, 2, 2, 1], padding='SAME', name='pool2')
  conv3_1 = conv_layer_with_bn(pool2, [7, 7, 64, 64], phase_train, name="conv3_1")
  conv3_2 = conv_layer_with_bn(conv3_1, [7, 7, 64, 64], phase_train, name="conv3_2")
  conv3_3 = conv_layer_with_bn(conv3_2, [7, 7, 64, 64], phase_train, name="conv3_3")
  dropout3 = tf.layers.dropout(conv3_3, rate=(1-keep_prob), training=phase_train, name="dropout3")
  pool3, pool3_indices = tf.nn.max_pool_with_argmax(dropout3, ksize=[1, 2, 2, 1],
                                                    strides=[1, 2, 2, 1], padding='SAME', name='pool3')
  conv4_1 = conv_layer_with_bn(pool3, [7, 7, 64, 64], phase_train, name="conv4_1")
  conv4_2 = conv_layer_with_bn(conv4_1, [7, 7, 64, 64], phase_train, name="conv4_2")
  conv4_3 = conv_layer_with_bn(conv4_2, [7, 7, 64, 64], phase_train, name="conv4_3")
  dropout4 = tf.layers.dropout(conv4_3, rate=(1-keep_prob), training=phase_train, name="dropout4")
  pool4, pool4_indices = tf.nn.max_pool_with_argmax(dropout4, ksize=[1, 2, 2, 1],
                                                    strides=[1, 2, 2, 1], padding='SAME', name='pool4')
  conv5_1 = conv_layer_with_bn(pool4, [7, 7, 64, 64], phase_train, name="conv5_1")
  conv5_2 = conv_layer_with_bn(conv5_1, [7, 7, 64, 64], phase_train, name="conv5_2")
  conv5_3 = conv_layer_with_bn(conv5_2, [7, 7, 64, 64], phase_train, name="conv5_3")
  dropout5 = tf.layers.dropout(conv5_3, rate=(1-keep_prob), training=phase_train, name="dropout5")
  pool5, pool5_indices = tf.nn.max_pool_with_argmax(dropout5, ksize=[1, 2, 2, 1],
                                                    strides=[1, 2, 2, 1], padding='SAME', name='pool5')
  """ End of encoder """

  """ Start decoder """
  dropout5_decode = tf.layers.dropout(pool5, rate=(1-keep_prob), training=phase_train, name="dropout5_decode")
  upsample5 = deconv_layer(dropout5_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h//16, FLAGS.image_w//16, 64], 2, "up5")
  conv_decode5_1 = conv_layer_with_bn(upsample5, [7, 7, 64, 64], phase_train, True, name="conv_decode5_1")
  conv_decode5_2 = conv_layer_with_bn(conv_decode5_1, [7, 7, 64, 64], phase_train, True, name="conv_decode5_2")
  conv_decode5_3 = conv_layer_with_bn(conv_decode5_2, [7, 7, 64, 64], phase_train, True, name="conv_decode5_3")

  dropout4_decode = tf.layers.dropout(conv_decode5_3, rate=(1-keep_prob), training=phase_train, name="dropout4_decode")
  upsample4 = deconv_layer(dropout4_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h//8, FLAGS.image_w//8, 64], 2, "up4")
  conv_decode4_1 = conv_layer_with_bn(upsample4, [7, 7, 64, 64], phase_train, True, name="conv_decode4_1")
  conv_decode4_2 = conv_layer_with_bn(conv_decode4_1, [7, 7, 64, 64], phase_train, True, name="conv_decode4_2")
  conv_decode4_3 = conv_layer_with_bn(conv_decode4_2, [7, 7, 64, 64], phase_train, True, name="conv_decode4_3")

  dropout3_decode = tf.layers.dropout(conv_decode4_3, rate=(1-keep_prob), training=phase_train, name="dropout3_decode")
  upsample3 = deconv_layer(dropout3_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h//4, FLAGS.image_w//4, 64], 2, "up3")
  conv_decode3_1 = conv_layer_with_bn(upsample3, [7, 7, 64, 64], phase_train, True, name="conv_decode3_1")
  conv_decode3_2 = conv_layer_with_bn(conv_decode3_1, [7, 7, 64, 64], phase_train, True, name="conv_decode3_2")
  conv_decode3_3 = conv_layer_with_bn(conv_decode3_2, [7, 7, 64, 64], phase_train, True, name="conv_decode3_3")

  dropout2_decode = tf.layers.dropout(conv_decode3_3, rate=(1-keep_prob), training=phase_train, name="dropout2_decode")
  upsample2= deconv_layer(dropout2_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h//2, FLAGS.image_w//2, 64], 2, "up2")
  conv_decode2_1 = conv_layer_with_bn(upsample2, [7, 7, 64, 64], phase_train, True, name="conv_decode2_1")
  conv_decode2_2 = conv_layer_with_bn(conv_decode2_1, [7, 7, 64, 64], phase_train, True, name="conv_decode2_2")

  dropout1_decode = tf.layers.dropout(conv_decode2_2, rate=(1-keep_prob), training=phase_train, name="dropout1_deconv")
  upsample1 = deconv_layer(dropout1_decode, [2, 2, 64, 64], [batch_size, FLAGS.image_h, FLAGS.image_w, 64], 2, "up1")
  conv_decode1_1 = conv_layer_with_bn(upsample1, [7, 7, 64, 64], phase_train, True, name="conv_decode1_1")
  conv_decode1_2 = conv_layer_with_bn(conv_decode1_1, [7, 7, 64, 64], phase_train, True, name="conv_decode1_2")
  """ End of decoder """

  """ Start Classify """
  # output predicted class number (2)
  with tf.variable_scope('conv_classifier') as scope:
    shape=[1, 1, 64, FLAGS.num_class]
    kernel = _variable_with_weight_decay('weights', shape=shape, initializer=tf.contrib.layers.variance_scaling_initializer(), #orthogonal_initializer()
                                           wd=None)

    conv = tf.nn.conv2d(conv_decode1_2, kernel, [1, 1, 1, 1], padding='SAME')
    biases = _variable_on_cpu('biases', [FLAGS.num_class], tf.constant_initializer(0.0))
    conv_classifier = tf.nn.bias_add(conv, biases, name=scope.name) #tf.nn.bias_add is an activation function. Simple add that specifies 1-D tensor bias
    #logit = conv_classifier = prediction
  return conv_classifier

Comment: We do not know what you mean by "the network fails", please clarify that point.
Comment: Why is it related to drop out ? Does it work without ?
Comment: Sorry about that! I'm segmenting images into two classes: "not bulding" and "building". When testing the network, all the pixels are classified as "not building". This does not happen during validation. Before I added the dropout layers the validation and test dataset gave similar results.
Comment: same issue. any ideas?
Comment: The idea behind drop out is to penalize your training model, by training it with keep_prob fraction of actual size. This makes sure that each weight parameter is sufficiently trained. We do not drop out during testing because we want to test the weights that we trained during training phase
Answer: I eventually figured out that my issue came from problems with batch normalization not being implemented correctly.
The issue was that I did not update the moving_mean and moving_variance   correctly. This is pointed out in the TensorFlow docs for batch_norm:

Note: when training, the moving_mean and moving_variance need to be
  updated. By default the update ops are placed in
  tf.GraphKeys.UPDATE_OPS, so they need to be added as a dependency to
  the train_op.

My code, therefore, looks like this:
def training(loss):

    global_step = tf.Variable(0, name='global_step', trainable=False)

    #This motif is needed to hook up the batch_norm updates to the training
    update_ops = tf.get_collection(tf.GraphKeys.UPDATE_OPS)
    with tf.control_dependencies(update_ops):
        if(FLAGS.optimizer == "SGD"):
            print("Running with SGD optimizer")
            optimizer = tf.train.GradientDescentOptimizer(0.1)
        elif(FLAGS.optimizer == "adam"):
            print("Running with adam optimizer")
            optimizer = tf.train.AdamOptimizer(0.001)
        elif(FLAGS.optimizer == "adagrad"):
            print("Running with adagrad optimizer")
            optimizer = tf.train.AdagradOptimizer(0.01)
        else:
            raise ValueError("optimizer was not recognized.")

        train_op = optimizer.minimize(loss=loss, global_step=global_step)
    return train_op, global_step

Full implementation of my network can be seen here: https://github.com/mathildor/TF-SegNet


Setting up dataset for Cox regression with time-dependent variables

Question: I posed a question about how to set up the code for this question here (Psychometrics: Survival analysis of help seeking behaviors) and @Fomite suggested that I pose a separate question about whether my data are set up correctly.
Background:
I'm studying people seeking help. Participants described contacts with between 1 and 3 "responders" (e.g., friends, the police) in order- for example, a participant could have contacted just responder 1, or responder 1, then responder 2, then responder 3. I'm trying to predict help-seeking dropout, meaning that, for example, a participant contacted responder 1 but did not go on to contact a second or third responder- that participant would have a dropout at responder 1. So unlike other survival analysis, the observations are responders rather than time points- but they're still ordered in time. The independent variables in my model include characteristics of the people seeking help (e.g., gender) and aspects of their interactions with the responders (e.g., whether they liked the interaction). The data are right-censored for those participants who said that they contacted more than three responders because they could not record more than three responders in the survey. There are two people who only reported on responder 3; those people are left-censored because data are missing for responders 1 and 2.
Current data setup:
The data are set up as a person-period dataset such that there is a line for each responder, which means that most participants have multiple lines. Responders are nested within participants. So a participant that contacted two responders would have two lines in the dataset; the participant-level data is the same in both lines and the responder-level data is different.
Here's what the data looks like now: https://flic.kr/p/s1fW6k
Variables:
Participant-level:
id is the ID number for the participant.
gender is the participant's gender; 1 = woman and 2 = man
Responder-level:
responder represents the responder number in the order that the participant contacted them. Possible values are 1, 2, and 3.
stoppedhelpseeking represents whether the participant stopped seeking help/dropped out after contacting that responder; 0 = no and 1 = yes. This is the event that I'm hoping to predict.
likedresponder represents whether the participant liked their interaction with the responder; 0 = no and 1 = yes
censor represents whether the participant did not report a dropout by responder 3. It is coded for their last recorded responder in the dataset.
My question:
@Fomite pointed out that, the way I've set up my data, each responder with a 0 value for stoppedhelpseeking will have a censored event time. This doesn't sound like what I want- what I'm hoping to predict is the point at which people drop out of help-seeking. Censoring should occur at the participant level, not the responder level. What's the right way to set this up to model things correctly?
Answer: Actually, the censoring of everything with stoppedhelpseeking=0 is what you want.
Having a subject with a time-dependent covariate is the same as having that subject leave the study when the covariate changes, and another subject with slightly different covariates enter the study at the same time. So you do want it to look like everything with stoppedhelpseeking=0 got censored. And then you want to claim that this person only entered that study when their previous version exited the study--so the person with responder 1 was in the study for the "time" interval (0, 1], the person with responder 2 was there for time interval (1, 2], etc. In this case your censor variable is actually irrelevant.
I don't know SAS, but in R, the way you would set up the response variable here is Surv(responder-1, responder, stoppedhelpseeking). For more info, see section 2 of this vignette. I imagine it's similar in other statistical software.
EDIT: based on @Fomite's answer to your other question, you can probably use something like entry=responder-1 to encode this?


Find intersection of three planes?

Question: Find the intersection of
$2x_1+x_2+x_3-3=0$, $2x_1+x_2+4x_3-6=0$, $2x_1+x_2-2=0$.
So I calculated the determinant of the three planes and it's equal to zero, then I calculated the cross product between the two first planes and it gave me $3x_1-6x_2$
The thorem in the course book says that if the determinant of the three planes is equal to zero, and cross product it's not zero, then there's two posibilities if $r_4-up_4-vq_4=0$ then the three planes intersect in a line $l$ and $l=\{(x_1,x_2,x_3):SX=up×q\}$ and S is a point in the line.
What is $r_4$, $up_4$ and $vq_4$ ? Thanks for your help.
Answer: These three planes are pairwise non-identical and non-parallel so they will meet in a point, or a line or they will form an open prism.
If you write it like  $Ax=B$ and if $\det|A|=0$, the planes will either meet in a line or the will form an open prism. If you find the edges of pairwise planes by finding cross products of their normals $\vec n_1, \vec n_2, \vec n_3$. If edges are identical/coincident the planes will meet in a line. If edges are non coplanar planes will form a prism.
But more simply here if you put (3) in (1) and (2), you get $x_3=1$  and $x_3=2$ contradiction (an inconsistency). So these three Eqs. are inconsistent and hence they form a prism. In a prism two plane meet leaving out one, so no solution (inconsistency).

Find intersection of three planes?


AJAX on Form Details

Question: I have a lot of API calls in a form, so in order to not call everything on load, I only want to load it when the user opens the details element.
But I am having trouble adding #ajax to the element. I can see a spinner and that it calls buildForm(), but it is not triggering the callback method. When I wrap the ajax part inside e.g. an checkbox, it works. Thus, I come to the conclusion that it is something with the details element.
How can I create an ajax callback, when the user opens the details element?
Non-working example with ajax on details:
public function buildForm(array $form, FormStateInterface $form_state) {
  [...]
  return [
    '#type' => 'details',
    '#title' => $title,
    '#open' => FALSE,
    '#ajax' => [
      'callback' => [$this, 'openAccordion'],
      'event' => 'click',
    ],
  ];
}

public function openAccordion(array $form, FormStateInterface $form_state) {
  $response = new AjaxResponse();
  return $response;
}

Working example where ajax is on a button (just an example):
return [
  '#type' => 'details',
  '#title' => $title,
  '#open' => FALSE,
  'widget' => [
    '#type' => 'button',
    '#value' => 'Load',
    '#ajax' => [
      'callback' => [$this, 'openAccordion'],
      'event' => 'click',
    ],
  ],
];

Comment: Is this code in your ajax callback?
Comment: Can we see a the full implementation minus irrelevant fields please?
Comment: I added a working example. As seen in the code, the only difference is that ajax is not on `details`.
Comment: How are you determining it is not triggering the callback? Is there a message in the Drupal log? Are there any messages in the JS console?
Comment: Im using xdebug and have set a breakpoint. No console logs and no Drupal logs. Also because of xdebug, I can see it reaches a breakpoint in buildForm after click but it is not hitting the callback method.
Answer: I am suspecting you cannot attach #ajax to non-input elements. As the display element does not take an input, it likely doesn't trigger #ajax. You can do the following as a fix.

Create a library:
details_element_ajax:
  css:
    theme:
      css/details_element_ajax.css
  js:
    js/details_element_ajax.js
dependencies:
  - core/once
  - core/jquery

Add a details element, and a submit button that triggers the ajax, and attach the library
$form['details'] = [
  '#open' => FALSE,
  '#markup' => '',
  '#attached' => ['library' => 'examplemodule/details_element_ajax',
];

$form['hidden_submit'] = [
  '#type' => 'submit',
  '#value' => $this->t('load_details),
  '#ajax' => [
    'callback' => [$this, 'openAccordion'],
    'event' => 'click',
  ],
];

Hide the submit button in the CSS file:
#edit_hidden_submit {
  display:none;
}

In the JS file, when the details element is opened, click the hidden submit button to trigger the ajax:
Drupal.behaviors.displayElementAjax = {
  attach: function(context) {
    const elements = $(context).find('#details_element_ajax').once('myfeature');
    elements.each(function () {
      $(this).click(function() {
        $("#edit_hidden_submit').click();
      });
    });
  }
};

AJAX on Form Details


Inserting DATETIME with ActiveRecord and MySQL

Question: I am having a problem with ActiveRecord inserting the DATETIME as per the documentation; "Active Record automatically timestamps create and update operations if the table has fields named created_at/created_on or updated_at/updated_on." I have a column named created_at but ActiveRecord is not inserting the DATETIME. My insert query looks as follows:
def add_record(server_id, backup_type)
  Record.create(:server_id => server_id, :backup_type => backup_type)
end

I know I could insert the time using something like
:created_at => "#{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"

The reason that I want to stay away from that is due to differences in time between the servers, both locally and geographically. I am wondering if there is a way to run the type in the same create method, such as:
def add_record(server_id, backup_type)
  Record.create(:server_id => server_id, :backup_type => backup_type, created_at => DATETIME())
end

Thank you.
Answer: You have a created_at column, but ActiveRecord is not creating a timestamp? What's the column type of your created_at column? Perhaps you could show us your migration?
Also, if you tell Rails to use UTC (default in 2.2, I think), the time values will be correct, regardless of different server locales.
And, lastly: You can just pass a time/date/datetime instance, you don't have to format it yourself. E.g. YourModel.create(:a_time_column => Time.now)
Comment: for some reason i was doing CallLog.callstart = Date.new.  that set the value to 0000-00-00 00:00:00. based on this answer, I changed it to CallLog.callstart = Time.now and I am feeling much better now.
Answer: If the created_at field type is TimeStamp then you can use the function CURRENT_TIMESTAMP() to set the value of the field.


Two-way relation in JPA

Question: I have the two entity classes, User and MyCharacter. User has a list of MyCharacters and each MyCharacter has a reference back to the User (owner). What I'd like to accomplish is, that I use the same join table for both relations, meaning, that the owner relation found in MyCharacter would automatically use the same join table as from User=>MyCharacter. This means that the getOwner() method in MyCharacter should work without me having to explicitly at some point call setOwner(user).
To clear this a bit more, here's my unit test which currently fails (last assert fails)

@Test
public void testTwoWayRelation() {
    User user = new User();
    MyCharacter character = new MyCharacter();
    List<MyCharacter> chars = new ArrayList<MyCharacter>();
    chars.add(character);
    user.setCharacters(chars);

    facade.store(user);
    assertNotNull(character.getId());

    character = facade.find(MyCharacter.class, character.getId());

    assertNotNull(character.getOwner());
}

My entity classes are listed below.

@Entity
@Table(name = "myuser")
public class User {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @OneToMany(cascade = { CascadeType.PERSIST })
    protected List<MyCharacter> characters;

    public User() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public List<MyCharacter> getCharacters() {
        return characters;
    }

    public void setCharacters(List<MyCharacter> characters) {
        this.characters = characters;
    }

}

@Entity
public class MyCharacter{
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Long id;

    @ManyToOne
    @JoinTable(name = "myuser_mycharacter", joinColumns = @JoinColumn(name = "characters_id"), inverseJoinColumns = { @JoinColumn(name = "user_id") })
    protected User owner;

    public MyCharacter() {

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public User getOwner() {
        return owner;
    }

    public void setOwner(User owner) {
        this.owner = owner;
    }
}

Answer: That's how we join two entities with jpa in our project:
    @Entity
    @Table(name = "Period")
    public class Period implements Serializable {
      private List<Delay> delays = new ArrayList<Delay>();

      @OneToMany(mappedBy="period") //name of the field in joined entity
      public List<Delay> getDelays() {
        return delays;
      }
    }

   @Entity
   @Table(name = "Delay")
   public class Delay implements Serializable {

    private Period period;

    @ManyToOne
    @JoinColumn(name = "PERIODID")
    public Period getPeriod() {
       return period;
    }
}

Answer: i'm not sure i understand your problem correctly, but you could try to set mappedBy on MyCharacter.owner:
@ManyToOne(mappedBy="characters")

Answer: Here is an article from Oracle which explains how to map such relations.
When you use this in Java, depending on your JPA framework, you will need to add the MyCharacter to the list in User and set the user field in MyCharacter or only one of the two (because the framework will manage the other side for you). I suggest to write a small test to figure out what works (and you should write test cases for all the way in which you use your objects, anyway).
When objects are loaded from the database, you won't need to do that since all frameworks handle this case correctly.

Two-way relation in JPA


Proposition 4.2.24 in Liu Qing's AG book

Question: In the book Algebraic Geometry and Arithmetic Curves, Qing Liu states the following proposition:

Proposition 4.2.24. Let $X$ be an algebraic variety over an algebraically closed field $k$ (i.e. $X\to \mathrm{Spec}(k)$ is of finite type). Then $\mathrm{Reg}(X)$ (regular locus) is an open subset.

The first step of his proof: since $U=\{x\in X:O_{X,x}\text{ is a domain}\}\subset X$ is open, and contains $\mathrm{Reg}(X)$, we may reduce to $X$ integral.
My question: why can we reduce to this case? $U$ is reduced but is it integral?
Comment: $U$ isn't integral, but we know that it has exactly one irreducible component going at each point. So its connected components are irreducible: as a consequence, its irreducible components are pairwise disjoint, finitely many and closed: so they are open, and we can consider each of them individually.
Comment: @Mindlack looks like an answer to me! Would you care to record it below?
Comment: @Mindlack Dear friend, why a connected component of $U$ is irreducible? See here: https://stacks.math.columbia.edu/tag/0568, $U=X=\mathrm{Spec}(A)$ is connected, reduced, but not irreducible.
Answer: I rephrase my comment for clarity, answering the OP's comment as well.
$U$ isn't integral, but we can find it has an "almost integral structure".
Indeed, for each $x \in U$, $O_{X,x}$ is a domain. With a slight back-and-forth with commutative algebra, it implies that there is only one irreducible component of $U$ going through any given point.
So the (finitely many) irreducible components of $U$ are pairwise disjoint and closed. As a consequence, they are open. Therefore, $U$ is a disjoint reunion of integral open subschemes.
Comment: Thanks for your answer. 1. Let $A$ be a ring whose localization at any prime ideal is a domain. Then given a prime $p$, there's only one minimal prime $q\subset p$, as $A_p$ has a unique minimal prime. 2. Let $X$ be a scheme whose stalks are integral, then its irreducible components are disjoint: if $Z_1,Z_2$ two irr components intersecting at $x$, we take an affine open $U$ around $x$, then $Z_i\cap U$ are irr components of $U$, hence equal. And $Z_i\cap U\subset Z_i$ is dense.

Proposition 4.2.24 in Liu Qing‘s AG book


How to update an array inside an array of objects with useState function component

Question: How to update array inside array use state function component
the array is inside the setTask() and how to use setTask() and add new_photo in photos
const new_photo = "testing2.png"; <------------ insert this value in photos

const [task, setTask] = useState([
    {
        task_id: 123,
        title: "sample",
        photos: ["testing1.png"] <------------ here
    }
])

Result should be like this:
[
    {
        task_id: 123,
        title: "sample",
        photos: ["testing1.png","testing2.png"]
    }
]

Comment: Unfortunately, your edit didn't add any of the information that was asked for in the first comment. As this is your first time asking a question, please see [ask] to get some details on asking questions - the main things that your question is missing is: 1. Your attempt at solving this yourself (this shows that you've tried something and that you're not asking other people to "do your job for you"), and 2. Further clarification about how you pick which object to update, eg: What happens when you have data that looks like: `[{ task_id: 123, ...}, { task_id: 456, ...} ]` (ie: multiple objects)
Comment: Hi vince - please can you share what you have tried so far and what isn't working with your current code? It also isn't very clear how you decide which object to add `"testing2.png"` when your array has multiple objects. Do you want it to be added to all objects in your array, or just one with a specific id...? It would be good if you can please [edit] your question to include all of this information
Comment: done sir, sorry its my first time asking question here
Comment: I understand that showing what you have tried might be difficult if you don't know where to start - If that's the case, you can share some links to other questions on here or other articles that you have tried but haven't worked for you. This shows that you've done some research to try and solve the issue yourself first, and also will allow the answerer to not repeat information you may already know
Answer:     const new_photo = "testing2.png"; // <------------ insert this value in photos

const [task, setTask] = useState([
    {
        task_id: 123,
        title: "sample",
        photos: ["testing1.png"] // <------------ here
    }
])

const arrayPush = (ID) => {
  setTask((element) => {
    element.forEach((e) => {
      if (e.task_id === ID) {
       e.photos.push(new_photo);
     }
    });
  });
console.log(task);
}

const arrayPush2 = (ID) => {
    let taskCopy = Array.from(task)
    taskCopy.forEach((element) => {
      if (element.task_id === ID) {
        element.photos.push(new_photo);
      }
    });
    setTask(taskCopy)
};

arrayPush(123)
arrayPush2(123)

Comment: That will create a copy of the task array `[]`, but not the objects inside of it, eg: `const task = [{x: 1}]; const copy = Array.from(task); copy[0].x++; console.log(task);` would show `[{x: 2}]`. For this particular case, we need to copy three levels, the outer array `[]`, the object `{}` and the inner array `[]` (ie: we need to deep copy, the object's we're changing as opposed to a shallow copy (which is what `Array.from()` does), so the above code still modifies the original `photos` arrary from the original state
Comment: This will modify the `task` state directly, which you shouldn't be doing in React (`let taskCopy = task` does not create a copy)
Comment: thank you it's my mistake answer is updated please check
Comment: `element` in the callback still refers to the state object, so changing it with `e.photos.push()` still modifies the objects within the array
Comment: `let taskCopy = Array.from(task)` using array prototype it will create copy right ?
Answer: setTask((oldTasks) => {
  const myTask = oldTasks.find(t => t.task_id === 123)
  return [
    ...oldTasks.filter(ot => ot.task_id != 123),
    {
      ...myTask,
      photos: [...myTask.photos, new_photo],
    }
  ]
})

Ref: spread operator and asynchronous state update
Comment: This will find the task, but it will add a new object to the state array rather than updating the one that is there (ie: this will create two objects in the state array rather than keeping just one)
Comment: @vince.villafuerte Are you sure it works? This will output `[ { "task_id": 123, "title": "sample", "photos": [ "testing1.png" ] }, { "task_id": 123, "title": "sample", "photos": [ "testing1.png", "testing2.png" ] } ]`, so now you have your old object, along with a new one (which doesn't match what you asked for in your question)
Comment: i get the idea on your codes sir, that i should use  ". . .myTask.photos"


How to create delete button in Api jQuery DataTables?

Question: How do I add the delete button using the jQuery DataTables api?
I was able to install the Datatable, but I can not implement the methods.
Can someone help me ?
https://github.com/yajra/laravel-datatables#jquery-datatables-api-for-laravel-45

this is my controller
 public function getIndex()
{
    return view('usuarios');
}

public function anyData()
{
    return datatables()->eloquent(mytable::query())->make(true);

}

this is my blade.php

        data_1
        data_2
        data_3/th>
        data_4
        data_5
        data_6
        data_7
        data_8

this is my ajax

Answer: The DataTables documentation shows an example on how to delete a row by clicking on an icon inside de row.
Also it seems like the
 element is not properly closed.


complex analysis definite integral

Question: Evaluate:
$$ \int_0 ^{+\infty} \frac {x^{a-1} dx} {1+x^3} $$ for $0 < a < 3$.
In the other problems in this section, we try to extend the bounds of the integral to $-\infty, +\infty$ so that it's a closed curve and we can solve for the residues.
However, I don't think we can do that here since it's not an even function.
the "$a$" in the numerator is giving me some problems.
Comment: Have you tried a wedge contour? Take a look at this for inspiration: https://math.stackexchange.com/questions/393875/contour-integral-int-infty-01zn-1dz
Comment: hm don't think we've learned that strategy yet. (I'm at section 4.3 of Marsden's basic complex analysis)
Answer: The first thing is that I will evaluate the more general integral
\begin{align*}
I_{a,n} = \int_0^{\infty} \frac{x^{a-1}}{1+x^n} \mathrm{d}x
= \frac{1}{n} \int_0^\infty \frac{u^{a/n-1}}{1+u}\mathrm{d}u
\end{align*}
with $u \mapsto x^n$. I actually evaluated the last integral in my soon to be published master thesis about Hankel forms and Dirichlet series.
The last integral is the same as the Mellin transform of $f(x) = 1/(1+x)$ that is
\begin{align*}
\mathcal{M}_f(s) = \int_0^{\infty} x^{s-1} f(x) \mathrm{d}x = \frac{\pi}{\sin \pi s}
\end{align*}
We will compute this Mellin transform using complex analysis, and then you can use the first line to obtain the value of your integral. Consider the branch of $z^{s-1}/(z+1)$ defined on the slit plane $C\backslash[0,\infty)$ by
\begin{align*}
    f(z) = \frac{r^{s-1}e^{i(s-1)\theta} }{z+1}\,,
\end{align*}
where $z = re^{i\theta}$, and $\theta \in (0,2\pi)$. For small $\varepsilon$ and $R > 1$, we consider the keyhole domain $C$,

consisting of $z$ in the slit plane $\mathbb{C}\backslash[0,\infty)$ satisfying $\varepsilon < |z| < R$, see the figure above. Since $f$ has a simple pole at $ =-1$, Cauchy's residue theorem yields
\begin{align}
    \int_C f(z) = 2\pi i \operatorname{Res}\limits_{z = -1}f(z) = -2\pi i e^{\pi i s}\,. \tag{0}
\end{align}
The integral over $C$ breaks into the sum of $4$ integrals.
\begin{align}
\begin{split}
      \int_{C} f(z) \mathrm{d}z
    & = \int_{\varepsilon}^R \frac{x^{s-1}}{x+1} \mathrm{d}x
      + \int_{C_R} \frac{z^{s-1}}{z+1} \mathrm{d}z \\
    & + \int_R^{\varepsilon} \frac{e^{2\pi i(s-1)}x^{s-1}}{1+x} \mathrm{d}x
      + \int_{C_\varepsilon} \frac{z^{s-1}}{z+1} \mathrm{d}z \,,
\end{split}
\tag{1}
\end{align}
and for the integrals over $C_R$ and $C_\varepsilon$ we obtain the following estimates
\begin{align}
        \left|
            \int_{C_R} \frac{z^{s-1}}{z+1} \mathrm{d}z
        \right|
    & \leq \frac{R^{s-1}}{R-1} 2\pi R = O(R^{s}) \,, \tag{2} \\
        \left|
            \int_{C_\varepsilon} \frac{z^{s-1}}{z+1} \mathrm{d}z\,.
        \right|
    & \leq \frac{\varepsilon^{s-1}}{1 - \varepsilon} 2\pi \varepsilon = O(\varepsilon^s) \tag{3}
\end{align}
Letting $R \to \infty$ and $\varepsilon \to 0$ we see that ($2$) and ($3$) vanishes. That is since it is assumed that $0 < s < 1$ in order to assure that the Mellin transform converges. Thus for $\varepsilon \to 0$ and $R \to \infty$ equation ($1$) becomes
\begin{align*}
    \Bigl( 1 - e^{2\pi i(s-1)}\Bigr)\int_0^{\infty} \frac{x^{s-1}}{x+1} \mathrm{d}x
    = -2\pi i e^{\pi i s}\,,
\end{align*}
where equation ($0$) was also applied. Thus,
\begin{align*}
      \mathcal{M}_g(s)
    = \int_0^{\infty} \frac{x^{s-1}}{1+x} \mathrm{d}x
    = \pi \frac{2i}{e^{\pi i s-2\pi i} - e^{-\pi i s}}
    = \frac{\pi}{\sin(\pi s)}\,,
\end{align*}
and we are done.
Answer: It is more practical to use the Laplace transform. For any $a\in(0,3)$ the given integral is convergent and equals (by substituting $x=z^{1/3}$)
$$ \frac{1}{3}\int_{0}^{+\infty}z^{\frac{a}{3}}\frac{dz}{z(z+1)}=\frac{1}{3}\Gamma\left(1+\frac{a}{3}\right)\int_{0}^{+\infty}s^{-1-\frac{a}{3}}(1-e^{-s})\,ds =\color{red}{\frac{\pi}{3 \sin\frac{\pi a}{3}}}$$
by the reflection formula for the $\Gamma$ function.
Comment: Can you add some more details from the first to the second line? How exactly are you taking the Laplace transform of the integral.
Comment: @N3buchadnezzar: I am simply exploiting $$\mathcal{L}(z^{a/3}) = s^{-1-a/3}\Gamma\left(1+\frac{a}{3}\right)$$ and $$\mathcal{L}^{-1}\left(\frac{1}{z(z+1)}\right)=1-e^{-s}.$$
Comment: Together with $$\int_{0}^{+\infty}f(z)\,g(z)\,dz = \int_{0}^{+\infty}(\mathcal{L}f)(s)\,(\mathcal{L}^{-1}g)(s)\,ds.$$
Answer: $\newcommand{\bbx}[1]{\,\bbox[8px,border:1px groove navy]{\displaystyle{#1}}\,}
 \newcommand{\braces}[1]{\left\lbrace\,{#1}\,\right\rbrace}
 \newcommand{\bracks}[1]{\left\lbrack\,{#1}\,\right\rbrack}
 \newcommand{\dd}{\mathrm{d}}
 \newcommand{\ds}[1]{\displaystyle{#1}}
 \newcommand{\expo}[1]{\,\mathrm{e}^{#1}\,}
 \newcommand{\ic}{\mathrm{i}}
 \newcommand{\mc}[1]{\mathcal{#1}}
 \newcommand{\mrm}[1]{\mathrm{#1}}
 \newcommand{\pars}[1]{\left(\,{#1}\,\right)}
 \newcommand{\partiald}[3][]{\frac{\partial^{#1} #2}{\partial #3^{#1}}}
 \newcommand{\root}[2][]{\,\sqrt[#1]{\,{#2}\,}\,}
 \newcommand{\totald}[3][]{\frac{\mathrm{d}^{#1} #2}{\mathrm{d} #3^{#1}}}
 \newcommand{\verts}[1]{\left\vert\,{#1}\,\right\vert}$
\begin{align}
\int_{0}^{\infty}{x^{a - 1} \over x^{3} + 1}\,\dd x & =
{1 \over 3}\int_{0}^{\infty}{x^{a/3 - 1} \over x + 1}\,\dd x =
{1 \over 3}\int_{1}^{\infty}{\pars{x - 1}^{a/3 - 1} \over x}\,\dd x
\\[5mm] & =
{1 \over 3}\int_{1}^{0}{\pars{1/x - 1}^{a/3 - 1} \over 1/x}
\,\pars{-\,{1 \over x^{2}}}\dd x =
{1 \over 3}\int_{0}^{1}x^{-a/3}\pars{1 - x}^{a/3 - 1}\,\dd x
\\[5mm] & =
{1 \over 3}\,{\Gamma\pars{1 - a/3}\Gamma\pars{a/3} \over \Gamma\pars{1}} =
{1 \over 3}\,{\pi \over \sin\pars{\pi a/3}}
\\[5mm] & =
\bbx{\ds{{1 \over 3}\,\pi\csc\pars{{\pi \over 3}\,a}}}\,,\qquad
\Re\pars{a} \in \pars{0,3}
\end{align}

complex analysis definite integral


ASP.NET Storing Contextual Data During A Request

Question: I am writing an ASP.NET application that initializes some contextual data based on stuff sent through the Request object. How should I be storing this so that it is only visible to objects dealing with the request?
Essentially I have an HttpModule that looks at the request, and does something based on the incoming data:
public void OnBeginRequest(object sender, EventArgs e){
    if((sender as HttpApplication).Request.Url.Host == "something"){
        // Store some extra information here
    }
}

And then I want to retrieve the data later on in the pipeline, in a view
<%: somehowGetRequestSpecificData.MyProperty %>

How and where should I be storing that stuff?
Answer: IMO - HTTPContext.Items is the best way to do this.
Ref.: https://web.archive.org/web/20201202215202/https://www.4guysfromrolla.com/articles/060904-1.aspx
HTH.
Comment: Thanks. Are there any other options?


How is the standard deviation of VAE's constructed?

Question: I am trying to build a Variational Autoencoder. I was looking at various codes online and found most of them in some way or another copy Francois Chollet (Google researchers) code.
Now my main question with this code is this part:

First, here's our encoder network, mapping inputs to our latent
  distribution parameters:
x = Input(batch_shape=(batch_size, original_dim))
h = Dense(intermediate_dim, activation='relu')(x)
z_mean = Dense(latent_dim)(h)
z_log_sigma = Dense(latent_dim)(h)

We can use these parameters to sample new similar points from the
  latent space:
def sampling(args):
    z_mean, z_log_sigma = args
    epsilon = K.random_normal(shape=(batch_size, latent_dim),
                              mean=0., std=epsilon_std)
    return z_mean + K.exp(z_log_sigma) * epsilon

# note that "output_shape" isn't necessary with the TensorFlow backend
# so you could write `Lambda(sampling)([z_mean, z_log_sigma])`
z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_sigma])

As you can clearly see the $log(\sigma) = \mu$. Where did this assumption come from? How is it possible that we generate a random normal distribution with mean $\mu$ and standard deviation $\sigma$ like this?
Answer: The mean and log-variance are both encoded in the same way, but they have their own connections leading up to them from the encoding part $h$. Further on in the example, you will see that both z_mean and z_log_sigma are concatenated into a single layer.
I'm not too familiar with python, but in the same example given for the R interface to Keras, you can see this quite literally, as the function is called layer_concatenate.

z <- layer_concatenate(list(z_mean, z_log_var)) %>%
  layer_lambda(sampling)

(layer_lambda takes this concatenated input and evaluates the function sampling.)
Perhaps this explicit concatenation is not necessary in the python version due to differences in how R and python work.
Comment: Good question! I searched the paper and couldn't find and explanation by the authors, but I assume it is because $\exp(x) > 0 \,\forall x$, and $\exp(\log(x)) = x$, so it is a way to ensure the variance is always positive. It might also just be for numerical stability, or convenience elsewhere in their derivations.
Comment: Yeah..I read it somewhere and yes i actually was not familiar with keras.. But can you tell something why is it not variance but log(variance)?


Primes in a concave sequence

Question: Are there infinitely many consecutive tuples of primes $(p_{n-1},p_n,p_{n+1})$ satisfying $$\frac{1}{2}(p_{n-1}+p_{n+1})\le p_n$$
I don't know how to do this problem. Maybe we use the prime number theorem? Thanks for all the help. My friend has found by checking that probably this guess is true. But no rigorous proof of it yet.
Answer: Yes there are.  For suppose not.  Then
$$p_{n+1}-p_n\le p_n-p_{n-1}$$
has only finitely many solutions.  That is, from some point on we have always
$$p_{n+1}-p_n>p_n-p_{n-1}\ .$$
So
$$p_{n+1}>p_n+1\ ,\quad p_{n+2}>p_n+1+2\ ,\quad p_{n+3}>p_n+1+2+3$$
and so on.  This means, essentially, that $p_k$ is at least a constant times $k^2$.  But this is not true, for it is a consequence of the Prime Number Theorem that $p_k$ is asymptotically $k\log k$.

Primes in a concave sequence


Hosting a server-side site?

Question: I've been thinking lately that hosting a site with server-side user input must be extremely expensive/difficult. Take something like facebook for instance. They probably have petabytes of information stored on their servers, generated by users. Isn't this expensive? Can someone explain how this is done?
Comment: Try Google for size.  They've got most of the Web on their servers, past and present, and oodles of indexes to everything.  They've had massive research projects on how to run data centers as cheaply as possibly, including detailed studies on temperature vs. reliability and equipment lifetimes.
Comment: @DarkLightA : What do you mean by server-side user input?
Comment: @Abimaran: I think he just means various data submitted by users (like status updates on Facebook).
Comment: That's not server side input, actually, the users don't have the access in the server side in a typical web site.
Comment: So, it's an intranet site basically ? It's the same thing as a "web" site except users come from the local network, but from the server's point of view, it's the same thing. And as for the high storage and scalability, that's another thing, I'm sure someone will post a great answer on that matter.
Comment: To add to @David Thornley's comment, Google even went so far as to create a new file system (http://en.wikipedia.org/wiki/Google_File_System)
Comment: We agree on PSE that this is belongs to SF. This is nothing to do with programming. Please notice if we are wrong. We trying to do out best.
Answer: Yes its expensive, there are many strategies (mostly around scaling OUT, as opposed to scaling UP)
If you want to understand the architecture behind such sites, you may be able to find about them on this blog:  highscalability
I believe there was an article on Facebook
Edit:
There are numerous articles on Facebook on that site.
Answer: NoSQL databases may give you some insight on how they are doing this.  David Thornley makes a great point about the Google hardware. Their biggest accomplishment may be their ability to manage tens of thousands of servers efficiently.
Some business models that offer free subscriptions have the most issues. If your next 1000 customers are paying a monthly fee, it's a lot easier to come up with the cash to buy more servers.

Hosting a server-side site?


What is the ideal?

Question: Can you please explain to me why in  $\mathbb{Z}/n\mathbb{Z}$,  $n\mathbb{Z}$ is the ideal of $\mathbb{Z}$. Can you please explain with some examples. I'm having trouble with what is ideal? Appreciate any help?
P.S.
I've read a lot on the net about quotient rings but all are difficult to me to comprehend.
Comment: Are you having trouble with the fact that $n\mathbb Z$ is an ideal, or why all ideals in $\mathbb Z$ are of that form?
Comment: First write down the definition of an ideal $I$ in a ring $R$, and then check the axioms for $R=\mathbb{Z}$ and $I=n\mathbb{Z}$. In order to help you, you should say where exactly you get stuck there.
Comment: Can you please, explain with simple language what is the ideal is?
Answer: The basic idea of an ideal is that it "acts as a stand-in for zero". In a ring (any ring) we have the following properties of $0$:

$0 + 0 = 0$
$a\cdot 0 = 0 \cdot a = 0$, for any $a$.

so an ideal is a subset $I$ of a ring $R$ for which:

$I + I = I$ (this means $I$ is closed under addition)
$RI = IR = I$ (this is called the absorption property).

Because some rings (in some texts) may not contain a multiplicative identity, one often sees (1.) above replaced by the condition:
1a. $(I,+)$ is a subgroup of $(R,+)$, or the equivalent: $I$ is closed under subtraction.
Ideals are an example of what are called "kernels". These take different forms in different structures:
In groups, kernels are normal subgroups.
In abelian groups, kernels are subgroups.
In rings, kernels are ideals.
In vector spaces, kernels are null spaces.
Ring-homomorphisms are essentially "operation-preserving maps" that preserve sums and products:
$f: R \to S$ with $f(r + r') = f(r) + f(r')$ and $f(rr') = f(r)f(r')$.
It turns out the kernel of $f$:
$\text{ker }f = \{r \in R: f(r) = 0_S\}$ is such an ideal:
If $r,r' \in \text{ker }f$ then $f(r) = f(r') = 0_S$, and since $f$ preserves sums:
$f(r + r') = f(r) + f(r') = 0_S + 0_S = 0_S$, so that $r + r' \in \text{ker }f$
Similarly, for any $a \in R$ and $r \in \text{ker }f$, we have:
$f(ar) = f(a)f(r) = f(a)0_S = 0_S$, so that $ar \in \text{ker }f$. An analogous result holds for $ra$.
It turns out that ALL ideals arise in this way. To see this, given an ideal $I$, and a ring $R$, we somehow need to come up with another ring $S$ and a homomorphism $f$ with:
$f: R \to S$ and $\text{ker }f = I$.
This is how we can do this:
Call two elements $r_1,r_2 \in R$, "$I$-equivalent", if  $r_1 - r_2 \in I$.
It may not be obvious this is actually an equivalence relation on $R$, but we can show it like so:
$r$ is $I$-equivalent to itself (reflexiveness of $I$-equivalence):
$r - r = 0_R \in I$ (since any subgroup of $(R,+)$ contains the additive identity $0_R)$.
If $r$ is $I$-equivalent to $r'$, then $r'$ is $I$-equivalent to $r$ (symmetry of $I$-equivalence):
If $r - r' \in I$, then since $(I,+)$ is a subgroup of $(R,+)$, we have:
$-(r - r') = r' - r \in I$.
If $r$ is $I$-equivalent to $r'$ and $r'$ is $I$-equivalent to $r''$, then $r$ is $I$-equivalent to $r''$ (transitivity of $I$-equivalence).
If we have $r - r' \in I$ and $r' - r'' \in I$, then since $I$ is closed under addition:
$r - r'' = (r - r') + (r' - r'') \in I$.
We call the SET of $I$-equivalence classes, the QUOTIENT SET $R/I$. It turns out that this can be made into a ring, like so:
Writing $[r]$ for the $I$-equivalence class of $r$, we set:
$[r] + [r'] = [r+r']$, and $[r][r'] = [rr']$.
WARNING!!! Although this seems "reasonable", we have to check that these definitions "make sense", that is, that they only depend on the equivalence classes, and not the individual elements of these classes. Let's do that now:
We need first to check that if $[r] = [a]$ and $[r'] = [b]$, that $[r + r'] = [a + b]$. So what we start with is:
$r - a \in I$ and $r' - b \in I$, and we need to show that $(r+r') - (a + b) \in I$.
But $(r + r') - (a + b) = (r - a) + (r' - b)$, which is the sum of two elements of $I$, and thus is in $I$, by closure.
We need to do the same for multiplication: given $r - a \in I$ and $r' - b \in I$, we need to show $rr' - ab \in I$.
This is a bit trickier:
$rr' - ab = rr' - rb + rb - ab = r(r' - b) + (r - a)b$. Now (using property 2), we have each term of this sum is in $I$, and thus the sum is.
This, then, makes the set of $I$-equivalences classes of $R$, a ring, the QUOTIENT RING of $R$ by $I$, $R/I$. We can then take $S = R/I$, and define our homomorphism $f: R \to R/I$ by $f(r) = [r]$. Then $\text{ker }f = [0_R] = I$.

Now, this may seem a bit "out-there". So let's use a very simple example:
We start with the ring of integers, $\Bbb Z$. We can, of course, form the subset $I = 2\Bbb Z$ of all even integers. I leave it to you to show that this is, in fact, an ideal (this comes down to showing the set of all even integers is a group under integer addition, and that multiplying an even integer by any other integer, still gives you an even integer).
Next, convince yourself that the only way $a - b$ can be even, is if BOTH $a,b$ are odd, or both are even. Thus we have just two $I$-equivalence classes, here:
$[0] = $ the even integers (this acts as the "zero of parity")
$[1] = $ the odd integers
and our ring operations are just the familiar rules:
odd + odd = even
even + odd = odd + even = odd
even + even = even
odd $\cdot$ odd = odd
odd $\cdot$ even = even $\cdot$ odd = even
even $\cdot$ even = even.
Answer: Some intuition:
When we do group theory, we are highly interested in subgroups as the main structure within a group, and especially normal subgroups. In rings, looking merely at sub-rings doesn't capture the structure very well, because it's too restrictive. Instead we study ideals.
Like normal subgroups, ideals arise naturally via kernels of homomorphisms. An ideal is the kernel of a ring homomorphism. As a result, the Isomorphism Theorems from group theory also apply to ring theory, using rings and ideals instead of groups and normal subgroups.
There's another, related context in which ideals appear, and that is in studying divisibility. Ideals were originally created to extend unique prime factorization to rings. In some rings, like $\mathbb{Z}[\sqrt{-5}]$, our normal conception of factoring falls apart. $$6=2\cdot 3=(1+\sqrt{-5})(1-\sqrt{-5})$$ but all of the above numbers don't break up more. Mathematicians went looking for "ideal numbers" that behaved, in terms of divisibility, the way they wanted them to. Dedekind realized that you could define easily, instead of ideal numbers, the sets that they divide.
The concept of "being divided by" follows two important rules:

If some number (ideal or not) divides $a$ and $b$, it divided $a+b$.
If some number (ideal or not) divides $a$, it divdes $\lambda \times a$ for all $\lambda$.

So in the above example, it turns out that there is some "ideal number" $\alpha$ that divides both $2$ and $1+\sqrt{-5}$ and a second "ideal number" $\beta$ that divides both $3$ and $1+\sqrt{-5}$. The "ideal factorization" of $6$ turns out to be $\alpha^2\beta^2=6$ But since defining and proving existence of these "ideal numbers" is difficult and possibly philosophically problematic, it's much easier to talk about the ideals that they generate, i.e. their multiples
For every number, $z$ in a ring $R$, we can define the set of multiples of $z$ easily. This is called a principle ideal. We can also use the above two rules to produce ideals that are not just the multiples of any number (called non-principle ideals). These are the ideals that correspond to the multiples of "ideal numbers".
Formally:
An ideal, $I$, of a ring $R$ is a nonempty subset such that
1) $\forall x\in I, \forall r\in R, rx,xr\in I$
2) $\forall x,y\in I, x-y\in I$
In the case of $n\mathbb{Z}:=\{nz|z\in\mathbb{Z}\}$ we see that $(nz)r=n(zr)$ and $(nz_1)-(nz_2)=n(z_1-z_2)$ so it satisfies these properties (and $0\in n\mathbb{Z}$ so it's nonempty)
Comment: Can you please, explain to me with more simple language, due to that fact that I've encountered as yours explanation in the papers and books ?
Comment: What about some examples?
Comment: @elgolondrino is this better

What is the ideal?


Prove that $f(x,y)=g(x+y,xy)$.

Question: Let $f\in \mathbb R[x,y]$ such that $f(x,y)=f(y,x)$. Prove that $f(x,y)=g(x+y,xy)$.
My try.
$f\in \Bbb R[x,y]$ and $f(x,y)=f(y,x)$ .If I take for example $f(x,y)=a_{00}+a_{01}y+a_{11}xy+a_{10}x+a_{13}xy^3$ then only the coefficients which are symmetric w.r.t $x $ and $y$ will remain in the expression of $f$ ,all other terms will cancel out because $f(x,y)=f(y,x)$.
But How can I write down the equation of $f$ from here?
Please provide some hints.
Comment: I think it's worth mentioning that this is a special case of the [fundamental theorem of symmetric polynomials](https://en.wikipedia.org/wiki/Elementary_symmetric_polynomial#The_fundamental_theorem_of_symmetric_polynomials). In particular $g$ does not only exist but is unique.
Comment: What is g? Do you mean some polynomial g in the variables $x+y$ and $xy$?
Answer: Write $f = f_{0} + f_{1} + \dots + f_{n}$, where each $f_{i}$ is homogenous of degree $i$ and symmetric.
You want to prove that each $f_{i}(x, y)$ is a polynomial in $x + y, x y$.
This is obvious for $f_{0}$ and $f_{1}$. We do also the case $i = 2$, as an example of the induction step that follows. We have
\begin{align}
f_{2}
&=
a_{2} x^{2} + a_{1} x y + a_{2} y^{2}
\\&= a_{2} (x^{2} + 2 x y + y^{2}) + (a_{1} - 2 a_{2}) x y
\\&=
a_{2} (x + y)^{2} + (a_{1} - 2 a_{2}) x y.
\end{align}
In general, proceeding by induction on $i \ge 2$, let
$$
f_{i} = a_{i} x^{i} + a_{i-1} x^{i-1} y + \dots a_{i-1} x y^{i-1} + a_{i} y^{i}
$$
for some $a_{j}$. Then there are $b_{j} = a_{j} - a_{j} \binom{i}{j}$ such that
\begin{align}
f_{i}
&=
 a_{i} (x + y)^{i} + b_{i-1} x^{i-1} y + \dots b_{i-1} x y^{i-1}
\\&=
a_{i} (x + y)^{i} + x y (b_{i-1} x^{i-2} + b_{i-2} x^{i-3} y + \dots + b_{i-2} x y^{i-3}  + b_{i-1} y^{i-2}),
\end{align}
and now by induction
$$
b_{i-1} x^{i-2} + b_{i-2} x^{i-3} y + \dots + b_{i-2} x y^{i-3}  +b_{i-1} y^{i-2}
$$
is a polynomial in $x + y, x y$. The point is that as the $a_{j}$ are symmetric, that is, $a_{i-j} = a_{j}$, so are the $b_{j}$, as $b_{i-j} = a_{i - j} - a_{i-j} \binom{i}{i-j} = a_{j} - a_{j} \binom{i}{j} = b_{j}$.
Comment: how to calculate $b_j=a_j-a_j(i,j)$;I can't understand that
Comment: $\binom{i}{j}$ denotes the binomial coefficient.
Answer: Set x+y=A and xy=G
we get

The resultant is a function since it is not one-many (f(x,y)=f(y,x)) and not many-many , since we have transformed a set of ordered pairs into another.
Comment: I can't decipher the first two equations, and as a consequence not understand why they hold. I would be much better if you used MathJax.
Answer: Let $f\in \mathbb R[x,y]$ such that $f(x,y)=f(y,x)$. To prove that $f(x,y)=g(x+y,xy)$.
Let $$f(x,y)=a_{0,0}x^0y^0+a_{0,1}x^0y^1+a_{0,2}x^0y^2+a_{1,0}x^1y^0+a_{1,1}x^1y^1+a_{1,2}x^1y^2+a_{2,0}x^2y^0+a_{2,1}x^2y^1+a_{2,2}x^2y^2$$
We then have
$$f(y,x)=a_{0,0}y^0x^0+a_{0,1}y^0x^1+a_{0,2}y^0x^2+a_{1,0}y^1x^0+a_{1,1}y^1x^1+a_{1,2}y^1x^2+a_{2,0}y^2x^0+a_{2,1}y^2x^1+a_{2,2}x^2y^2$$
As $f(x,y)=f(y,x)$ we have $a_{0,1}=a_{1,0}, a_{2,0}=a_{0,2},a_{1,2}=a_{2,1}$.. So, we have
$$f(x,y)=a_{0,0}x^0y^0+a_{0,1}x^0y^1+a_{0,2}x^0y^2+a_{0,1}x^1y^0+a_{1,1}x^1y^1+a_{1,2}x^1y^2+a_{0,2}x^2y^0+a_{1,2}x^2y^1+a_{2,2}x^2y^2=a_{0,0}x^0y^0+a_{0,1}(x^0y^1+x^1y^0)+a_{0,2}(x^0y^2+x^2y^0)+a_{1,1}x^1y^1+a_{1,2}(x^1y^2+x^2y^1)+a_{2,2}x^2y^2=a +bxy+c(xy)^2+d(x+y)+e((x+y)^2-2xy)+fxy(x+y)$$

Prove that $f(x,y)=g(x+y,xy)$.


No ARIMA, No GARCH, which model?

Question: I am trying to fit a model for a data set.
The acf and pacf, after differentiating the data are:

The acf shows that the returns appeared to be random. According to them seems like the ARIMA model is not a good fit for the data.
I square the observations and run a new acf plot on this new data set of squared returns, waiting to find a GARCH pattern, but again the  acf appeared to be random:

Which model do you think it would be convenient to use to fit the data?
DATA
<code>10346 13698 13894 19854 28066 26620 27066 16658  9221 13578 11483 10390 11126 13487
15851 16116 24102 30892 25081 14067 10433 15591  8639 10345 10639 15796 14507 21289
25444 26149 23612 19671 12447 13535 10667 11255  8442 11546 15958 21058 28088 23827
30707 19653 12791 13463 11465 12326 12277 12769 18341 19140 24590 28277 22694 15489
11070 11002 11579  9834  9364 15128 15147 18499 25134 32116 24475 21952 10272 15404
13079 10633 10761 13714 16073 23335 29822 26800 31489 19780 12238 15318  9646 11786
10906 13056 17599 22524 25057 28809 27880 19912 12319 18240 11934 10290 11304 16092
15911 24671 31081 27716 25388 22665 10603 14409 10736  9651 12533 17546 16863 23598
25867 31774 24216 20448 12548 15129 11687 11581
</code>
Answer: Transformations like drugs can be bad for you. The only time an ARCH/GARCH is needed is if the error variance changes stochastically over time. Here is a useful model with actual fit and forecast . The forecasts are here  . A plot of the residuals suggests randomness  which is confirmed by the acf of the residuals  . All models are wrong but this one seems useful. The combination of ARIMA structure and 5 seasonal dummies renders the error process to be white noise thus effectively decomposing the observation set to signal and noise.
The reason your software failed to identify the appropriate model was that it'S model identification approach is limited. Simple solutions can often work for simple problems. When data is not simple ( as in nearly always ! ) you may need more sophisticated model identification procedures. The ACF/PACF of the original series is of little value when there are deterministic components such as seasonal dummies/level shifts/pulses/local time trends in the data.
Comment: For the record, the following statement is incorrect: *The only time an ARCH/GARCH is needed is if the error variance changes stochastically over time.* ARCH/GARCH models imply deterministic conditional variance and that is one major way in how they differ from stochastic volatility (SV) models. To see this, take a look at the ARCH/GARCH equation for the conditional variance: it does not have an error term at the end. The current period's conditional variance is completely determined by past shocks/innovations in ARCH models and also by past conditional variances in GARCH models.

No ARIMA, No GARCH, which model?


Python 3: Why am I getting an AttributeError?

Question: I'm running Python 3 and I'm getting the following error:
AttributeError: 'AssemblyParser' object has no attribute 'hasMoreCommands'

Here is the code that is raising the error:
import sys
from Parser import AssemblyParser
from Code import Code

parser = AssemblyParser(sys.argv[1])
translator = Code()

out_file = str(sys.argv[1]).split(".")
out_file = str(out_file[:1]) + ".hack"

with open(out_file, 'w', encoding='utf-8') as f:
    while parser.hasMoreCommands():
        parser.advance()
        if parser.commandType() == "A_COMMAND":
            dec_num = parser.symbol()
            binary = "{0:b}".format(dec_num)
        elif parser.commandType() == "C_COMMAND":
            default_bits = "111"
            comp_bits += translator.comp(parser.comp())
            dest_bits += translator.dest(parser.dest())
            jump_bits += translator.jump(parser.jump())
            binary = default_bits + comp_bits + dest_bits + jump_bits
        assert len(binary) == 16
        f.write(binary)

Here is my Parser.py file:
class AssemblyParser:
"""
Encapsulates access to the input code. Reads an assembly language command,
parses it, and provides convenient access to the command's components (fields and symbols).
In addition, removes all whitespace and comments.
"""

def __init__(self, input_file):
    self.current_command = ""
    self.next_command = ""
    with open(input_file,"r+", encoding='utf-8') as f:
        for l in f:
            line = "".join(l.split()) # Remove whitespace from the line
            line = line.split('//') # Removes any comments from the line
            clean_line = line[0]
            if clean_line.strip(): # Removes any blank lines
                f.write(clean_line)
        next_command = f.readline()

def __hasMoreCommands__(self):
    if self.next_command:
        return true
    return false

def __advance__(self):
    with open(input_file, encoding='utf-8') as f:
        self.current_command = self.next_command
        self.next_command = f.readline()

def __commandType__(self):
    char_1 = self.current_command[:1]
    if char_1 == "@":
        return "A_COMMAND"
    elif char_1 == "(":
        return "L_COMMAND"
    else:
        return "C_COMMAND"

def __symbol__(self):
    assert self.commandType() == ("A_COMMAND" or "L_COMMAND")

    if self.commandType() == "A_COMMAND":
        symbol = str(symbol[1:])
    else:
        symbol = str(symbol[1:len(symbol)-1])
    return str(symbol)

def __dest__(self):
    assert self.commandType() == "C_COMMAND"

    if "=" in self.current_command:
        temp = self.current_command.split("=")
        return str(temp[:1])
    else:
        return ""

def __comp__(self):
    assert self.commandType() == "C_COMMAND"

    temp = self.current_command

    if "=" in temp:
        temp = temp.split("=")
        temp = str(temp[1:])
    if ";" in temp:
        temp = temp.split(";")
        temp = str(temp[:1])
    return temp

def __jump__(self):
    assert self.commandType() == "C_COMMAND"

    if ";" in self.current_command:
        temp = self.current_command.split(";")
        return str(temp[1:])
    else:
        return ""

I really don't know why I'm getting this error, I've looked at the import documentation, but I'm getting more and more confused. I'm fairly new to Python. Can anyone explain this error?
Thanks.
Comment: `hasMoreCommands` vs `__hasMoreCommands__`
Answer: Well. There seems to be no function in Parser module with name hasMoreCommand. The function in there starts with underscore and end eith underscore.
Answer: Two leading and trailing underscores are used to identify "magic" attributes. You can't use that to create your own, as they only reference pre-existing methods.
The following is what you probably want:
hasMoreCommands():

If you have multiple classes with this function, use name mangling instead:
_hasMoreCommands():

See: https://stackoverflow.com/a/8689983/2030480
And: http://www.rafekettler.com/magicmethods.html


Base migration class?

Question: What's the procedure for including a base migration class that some of my migrations extend? I have some common functionality across multiple migrations class and would not like to repeat the code in all classes.
My question is how do I "register" my base class with Migrate? I need it to get autoloaded. Perhaps if I just include it in the module's list of files?
Answer: Take a look at the example migration for beer and wine that ships with Migrate in the 'migrate_example' folder.
You will need to register your your migration and handler classes explicitly with hook_migrate_api(). See 'migrate_example.migrate.inc' inside 'migrate_example'.
However, you don't need to explicitly register abstract intermediate classes that hold common info for other migrations you're implementing. See the abstract class 'BasicExampleMigration', inside 'beer.inc' as an example.
Comment: That's right. You wouldn't register your base class with *Migrate*, per se, because that base class doesn't represent an actual Migration to be run. But you would want to register the file containing your base class with Drupal's class autoloader. That's done in the module.info file, with a "files[] = filename.php" line.

Base migration class?


Set user agent using Selenium 2

Question: I am trying to web automate an application which behaves differently if accessed from a mobile device. Does anyone know the best way to achieve this using Selenium 2?
In an ideal world I would like to find a way to just configure the user agent so that we can easily test lots of permutations.
Answer: Looking here, it shows this code to set the user agent string in Firefox:
FirefoxProfile profile = new FirefoxProfile();
profile.addAdditionalPreference("general.useragent.override", "some UA string");
WebDriver driver = new FirefoxDriver(profile);

Converting to Ruby, it would look like this:
require 'selenium-webdriver'

profile = Selenium::WebDriver::Firefox::Profile.new
profile['general.useragent.override'] = 'some UA string'

driver = Selenium::WebDriver.for :firefox, :profile => profile

Adding a line to the end of that to navigate to http://whatsmyuseragent.com indicates that it works as expected.
However, Selenium 2 comes with drivers for both iPhones and Android applications. I haven't tried them yet, but it looks like they both run either in the simulators or in the real device. Is there a reason that these wouldn't work for you? They might give a better view of how things are really going to look on the device.
Comment: do you know how change orientation ?

Set user agent using Selenium 2


Lead Guitar - Stay In Single Key

Question: I have played guitar for a while and am confused by this... I know scales up and down the neck quite well, and know the fretboard and notes quite well. When I play lead, I am just now getting to where I express myself without sounding like a scale player. Do this by changing scale forms and move across the neck. Or, play Lydian mode which puts accent across the tune on the I and V chords.
I was looking for some expression ideas and have seen where some suggest playing the lead all in the same key, but pick your notes wisely as the chords change. Then others suggest to change the key of the scale that you are playing as the chords change. I must admit that changing scale key every time a chord changes, even in a simple I-IV-V progression seems fairly difficult. Any thoughts? Should I practice changing scale key with chord changes or should I just play a scale in the key that the song is composed of?
Kind of confused on this one...
Answer: It's dependent on several things. For blues songs, especially if they're 3 chord wonders, then changing for each chord does work well. Of course whatever you do, there will be notes common to 2 of the chords anyway.
Let's say you're IN C, but ON F. In a standard type of song (whatever that is...) You'll still be IN C ON F. So all the C scale notes still fit. The difference is the Bb.So using F Lydian avoids that. So you're still actually playing the C scale notes. Obviously Bb can be, and sometimes is used, but you just need to be careful where it goes. Use of ears here!
In jazz, going with each chord is the way, and that's down to, in a lot of cases, the fact that the chord structure changes offten, and the chords themselves are not diatonic. Thus, IN C, but ON, say, E7#9, the notes from C scale aren't as useful as those from E, with the option of G# and Fx, for example.
It's a useful exercise to be able to switch scales quickly. A simple idea is to play 4 to the bar, and have someone tell you the next chord/key just before the next bar. When that arrives, change to the nearest note in the new key, and then play 3 more, and so on. Might work better slower and 2 bars each at the beginning. Stick to majors initially. It ain't easy, but it's good. You could do the same with arpeggios; this will strengthen the chord shape knowledge. No sliding up several frets to the new bar!
Comment: Changing scale with each new chord will more often than not get you into trouble when trying to follow a jazz progression. A more practical approach is to think in "tonal centers". The example of the E7#9 chord in the key of C is actually a good one. That chord would usually be heard as the dominant of Am, so you could very well play the C major scale / A natural minor scale over it. Just the note A becomes an avoid note (and could, but need not, be replaced by a G#).
Answer: It depends on the chord progression. If the chord progression implies a key change then you'd also change the key of your solo melody. In all other cases, you wouldn't change the key of your melody.
You gave a I-IV-V progression as an example. If you were to use the IV-major scale over the IV chord, you would make that chord sound like a new I chord, and not like the IV chord of the current key. The same is true for the V chord: by playing the V-major scale over it, it wouldn't sound like a dominant chord of the actual key; however, it is usually intended as a dominant chord leading back to the I chord.
Things are different if you have a modal piece like So What by Miles Davis. In the solo section you have D dorian (Dm7) before changing to Eb dorian (Ebm7). The Ebm7 chord does not function in the key of D minor, but it is something new. This means that in your solo you would change scales from D dorian to Eb dorian, simply reflecting what is happening harmonically.
Another different case is the blues. A basic I-IV-V blues is based on dominant seventh chords. There is no single chord scale for those 3 chords. Note that this is different from my first example because there the 3 chords are all from the same major scale with the same root as the I chord (unlike in the blues, only the V chord could be a dominant seventh chord, not the others). In the blues, the chord scale for each of the 3 chords is the mixolydian scale with the respective roots. Nevertheless, it is important and helpful to realize that those 3 mixolydian scales share many common notes. The mixolydian scale of the IV chord is obtained by changing only one single note of the mixolydian scale of the I chord. The same is true for the mixolydian scale of the V chord.
In sum, on order to be able to choose appropriate scales, you need to understand the harmony and the implied key (and, possibly, key changes) of the underlying progression, and you want to reflect all this in your solo. If your solo does not reflect the harmonic function of the current chord (if there is any) then the result will not be very musical.
Comment: @Tim: That's right, but I was talking about the chord scales of the three dominant chord, and these are the mixolydian scales with the respective roots of the 3 chords. These chord scales do not contain any blue notes. Both blues scales you mentioned are a subset of the mixolydian scale plus one (major blues) or two blue notes (minor blues).
Comment: In blues, as likely as not, the minor blues scale notes will be used, and even the major blues scale notes, sometimes along with the Mixolydian.

Lead Guitar - Stay In Single Key


let N be Vitali set and $A=\{(x,a_0)∈\mathbb{R^2}|x∈N \} $($a_0$ is a fixed real number).Now which of following options is true?

Question: let  $\mathcal{N} $ be Vitali set and $A=\{(x,a_0) \in \mathbb{R^2} \mid x \in \mathcal{N} \} $ ($a_0 $ is a fixed real number).Now which of following options is true ?
1.$A$ is  measurable set in $\mathbb{R^2}$
2.$A$ is a closed set in $\mathbb{R^2}$ for the  Euclidean distance.
3.$A$ is  measurable set but not closed.
4.$A$ is  measurable set with $\infty$ measure.
We know $A$ is not closed because we know $G \times H$ is closed iff $G$ is closed and $H$ is closed.
Comment: https://math.stackexchange.com/questions/1143277/lebesgue-measure-of-non-measurable-set-times-the-null-set?rq=1
Answer: What is the Lebesgue measure of $B= \mathbb{R} \times\{ a_0\}$ ? And think of what it means for a measure to be complete.
Comment: So $A \subset B $ and $m(B) =0 \times \infty =0 $ so $m(A) =0 $ then "1" is true.
Comment: @amirbahadory not only 1, pick another too


standard method to get notfication from database on change/insertion

Question: I am curently trying to make a chat application aimed at 1000-1500 users. What currently happens is once my webpage loads I make an ajax request every second to check if there is anything new in the database. I want to know if this is the standard practise or if there is a more efficient way to be notified by the server somehow when an insertion occurs.
Answer: Use WebSockets. Or at the very least AJAX polling. Firing a request every second from 1500 clients will most likely kill your server.
Look at http://socket.io/ if you are open to introduce something new to your stack. But there are PHP websocket solutions out there if you are limited to PHP.
Comment: Even if I use socket.io i will still be required to query my database ateast once a second per user to check for message updates. Would that also lead to problems as well?
Comment: The idea is to structure the application in another way then. You don't need to store the messages in the DB. Or at least you don't need to constantly read from it. Just push messages over WS directly to users when they arrive.
Answer: Your approach is a standard method called Polling. Based on the number of clients this should be perfectly fine for a server with up-to-date hard-ware (do HEAD requests via AJAX that indicate the status via HTTP status code).
The other alternative - as pointed out by Jan - is called Pushing.

Pros: Involves a lot less requests to the server.
Cons: Requires technology that may or may not be provided by your client's browser.

In case you'll opt for the second approach, take a look into Server-Sent Events (W3C draft).

This specification defines an API for opening an HTTP connection for receiving push notifications from a server in the form of DOM events. The API is designed such that it can be extended to work with other push notification schemes such as Push SMS.