Apache: RewriteCond %{QUERY_STRING} issue

Question: I wished to block requests like http://anything.com/something.php?hack_attempt=select *  from users.
For this I do in .htaccess
RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]

The problem is that this rule hits also http://anything.com/update.php
As I know %{QUERY_STRING} should contain only get params string after ? , but it hits the URI.
Can anyone advice where the problem can be?
UPDATE: full rule
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{QUERY_STRING} ^.*(localhost|loopback|127\.0\.0\.1).*                                [NC,OR]
 #RewriteCond %{QUERY_STRING} ^.*(\.|\*|;|<|>|'|"|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*                 [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*/ban_url/                                                           [NC,OR]
 #RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]
 RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|insert|cast|set|declare|drop).* [NC,OR]
 RewriteCond %{QUERY_STRING} ^.*\?.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]

 RewriteRule ^(.*)$ - [R=400,L]
</IfModule>

if I uncomment
RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]

then Apache will block www.anything.com/update.php
but it should block only www.anything.com/something.php?param=update
UPDATE 2: full conf
<IfModule mod_rewrite.c>
 RewriteEngine On
 RewriteCond %{QUERY_STRING} ^.*(localhost|loopback|127\.0\.0\.1).*                                [NC,OR]
 #RewriteCond %{QUERY_STRING} ^.*(\.|\*|;|<|>|'|"|\)|%0A|%0D|%22|%27|%3C|%3E|%00).*                 [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*/ban_url/                                                           [NC,OR]
 #RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]
 RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|insert|cast|set|declare|drop).* [NC,OR]
 RewriteCond %{QUERY_STRING} ^.*\?.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]

 RewriteRule ^(.*)$ - [R=400,L]

 RewriteCond %{REQUEST_URI} ^.*wp-*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*www\.zip*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*backup\.zip*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*public_html\.zip*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*\.tar\.gz*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*administrator*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*admin\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*admin/index\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*elrekt\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*_adminer*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*accesson*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*agentui*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*trackback*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*wp-login*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*router\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*AspCms_AdminAdd*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*public/js/wind*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*include/calendar/calendar-cn*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*app-ads*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*vendor/phpunit/*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*utility/*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*blackhat*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*data/admin/allowurl*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*js/varien*                                                           [NC,OR] #magento
 RewriteCond %{REQUEST_URI} ^.*js/mage*                                                           [NC,OR] #magento
 RewriteCond %{REQUEST_URI} ^.*magento_version*                                                           [NC,OR] #magento
 RewriteCond %{REQUEST_URI} ^.*db_z\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*functions\.php*                                                           [NC,OR]
 RewriteCond %{REQUEST_URI} ^.*lottery-index*                                                           [NC]

 RewriteRule ^(.*)$ - [R=400,L]

</IfModule>

this conf file is applied in site conf like
<VirtualHost *:80>
    Include /var/www/url_blacklist.conf
        ServerName ...
        DocumentRoot ...
        ErrorLog ...
</VirtualHost>

Comment: _"UPDATE: full rule"_ - is this your full rewrite configuration now, or are you doing other stuff as well? What you say is happening, can't be explained by what you have shown so far, and a test tool like https://htaccess.madewithlove.be agrees.
Comment: Please post your `RewriteRule` and be more specific about the problem you are having.
Comment: @AmitVerma updated
Comment: @MykolaMykolayovichDolynskyi, is the string `hack_attempt=` is a sample one of a real string in your query string(if not then please do mention whatever could be there with samples)? Kindly confirm once.
Comment: @RavinderSingh13 not real, I just want to search for any forbidden words which are only in get params (after ? sign), but in real it also finds them in URI also. I can not use sample as I don`t know which kind of attack robots will deploy next time
Comment: @CBroe full rule, but not full .htaccess. Anyhow as I explained there is commented line **RewriteCond %{QUERY_STRING} ^.*(md5|benchmark|union|select|insert|cast|set|declare|drop|update).* [NC]** if I uncomment it rule hits URL **www.anything.com/update.php** this should not be, it must hit **www.anything.com/anythingelse.php?somethin=update** and tool you provided the link to (thanks) confirms it should work like I say but in fact behaves like QUERY_STRING is REQUEST_URI+QUERY_STRING
Comment: Well show us the full .htaccess then – something else inside of it appears to be interfering with what you are trying to do here.
Comment: @BCore like what? Updated post above
Answer: I am assuming that you want to block/forbid all those requests which have select * in their query string along with their uri doesn't have update.php requested, if this is the case could you please try following once. These conditions are written as per requested conditions only, you could try to test it alone and then could try to merge them with your existing conditions too.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/update\.php [NC]
RewriteCond %{QUERY_STRING} select \* [NC]
RewriteRule ^ - [R=301,F]

Comment: @MykolaMykolayovichDolynskyi, sorry usually conditions wouldn't have many spaces so I haven't scrolled right to it.
Comment: like that, I wish to redirect to 301 any request which have SQL expressions only in get params. At same time I need to allow ULRs like /update.php and so on (but QUERY_STRING hits URL also, I thought it should only include get params only)
Comment: @MykolaMykolayovichDolynskyi, looks like your conditions like `RewriteCond %{REQUEST_URI} ^.*wp-* ` then `RewriteCond %{REQUEST_URI} ^.*www\.zip*` having an issue here, you did not put `OR` condition there so all are considered as an AND condition which can't be the case by seeing your regex(which isn't looking ok to me since everything is checking that it `^` starts from), kindly try to put `OR` condition once there?
Comment: the [NC,OR] seems to be everywhere, may be have not scrolled code frame to right?


File upload functionality on node display page

Question: I want to provide an upload button on a node display page. By clicking a button, it should be possible to upload files. Here is what I'm thinking of:

For this I am changing the node.tpl file.
 <?php //code added
     if($node->title=='Course Materials'){?>
        <form action="" name="uploadform" >
            <input type="submit" value="upload" name="upload"/>
        </form>
    <?php  }
    if(isset($_REQUEST['upload'])){ ?>
    <form  method="post" action="" enctype="multipart/form-data">
        <input type="file" name="file"  />
        <input type="submit" name="submit"  />
    </form>
     <?php //end
     }?>

And to save the files:
  if(isset($_REQUEST['submit'])){
    if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){

   //$file = $_FILES['file']['tmp_name'];
   move_uploaded_file($_FILES["file"]["tmp_name"], "C:/xampp/htdocs/drupal6upload/sites/default/files/".$_FILES["file"]["name"]);
    $node = node_load($node->nid);

        // Build the file object
        $file_obj = new stdClass();
        $file_obj->filename = $_FILES['file']['name'];
        **$file_obj->filepath = "C:/xampp/htdocs/drupal6upload/sites/default/files/".$_FILES["file"]["name"];**
        $file_obj->filesize = $_FILES['file']['size'];
        $file_obj->uid = 1;
        $file_obj->status = FILE_STATUS_TEMPORARY;
        $file_obj->timestamp = time();
        $file_obj->list = 1;
        $file_obj->new = true;

        // Save file to files table
        drupal_write_record('files', $file_obj);

        // change file status to permanent
        file_set_status($file_obj,1);

        $node->field_test1[] = (array)$file_obj;
        node_save($node);

        drupal_goto('node/'.$node->nid);
    }
}

But I am facing some problem to set the filepath and file_copy instead of move_uploaded_file.
Any suggestions?
Answer: I am able to do it by creating custom module.
    function upload_course_form($form_id) {
    $form = array();
    $form['#attributes'] = array('enctype' => "multipart/form-data");
    $form['course_materials'] = array(
            '#type' => 'file',
            '#title' => t('Course Materials:'),
            '#size' => 60,
            );
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
        '#weight' => 10
    );
    return $form;
}

  function upload_course_form_validate($form, &$form_state){
  $field = 'course_materials';
  $directory = file_directory_path();
  if (file_check_directory($directory, FILE_CREATE_DIRECTORY, $field)) {
    $validators = array('file_validate_extensions' => array('pdf xls html doc txt docx'));
    if ($file = file_save_upload($field, $validators, $directory)) {
      file_set_status($file, FILE_STATUS_PERMANENT);
      $form_state['storage']['file'] = $file;

    }
  }
}

function upload_course_form_submit(&$form, &$form_state){
print_r($form_state['storage']['file']); //see the upload file details
//and add the code to save the node.

}


Drupal Views Export

Question: I added a data export to the view and also with glossary mode enabled..
When I filter the view by alphabet and then click on export then it not generating excel file.
Comment: Above issue is solved I removed the glossary option set in the data export display of the view.But now I am having another problem When I go to any alpha page then try to export .. it is generating empty file.. I want to export the whole view...
Answer: This you can do by just installing the Views data export module and could play around the data that you want to export.
This would provide a module to do your desired work.

This module is designed to provide a way to export large amounts of data from views. It provides a display plugin that can rendered progressively in a batch. Style plugins are included that support exporting in the following types:

CSV
Microsoft XLS
Microsoft DOC
Basic TXT
XML.
Comment: Hi Nitesh,I am using the data export to generate the excel file. But as I am using the contextual filters it is generating the empty file when I filter. I think the problem is with path path/%/mentor..I have just given some path.. I dont no it is correct..And how to set the path...
Comment: I found this link some having same issue with exposed filters http://drupal.org/node/1322816. I have set the parent sort and now the path I dont no it is correct
Comment: @BLPraveen You can try by applying the [submitted patch](http://drupal.org/node/1322816#comment-6756186) as one of the user has mentioned that it solved the issue for him. If the issue solves your problem as well, then you should report it back to the issue queue with status _reviewed & tested by community_.
Comment: hi develkar,I made some changes so that the export file will show only the filter views. And it was working to me..I found this link which was helpful to setup the data export to filter data based on contextual filters http://drupal.org/node/1322816 .. But here I need to enable the glossary option in the data export to work properly
Comment: But the other way did not work to me to show all the views even when contextual filters is applied.
Answer: are u getting any data in the view .
If yes then you could export it to csv or excel and get the desired file.
If no data is being viewed the n you need to check the view once again.
Comment: Actually I fixed the problem by making the data export filter the result based on the contextual filters .. but the problem is I cannot get the full view results in the export even when view is filtered

Drupal Views Export


Plancks Formula for Blackbody spectrum

Question: I am trying to write a simple python code for a plot of intensity vs wavelength for a given temperature, T=200K.
So far I have this...
import scipy as sp
import math
import matplotlib.pyplot as plt
import numpy as np
pi = np.pi
h = 6.626e-34
c = 3.0e+8
k = 1.38e-23

def planck(wav, T):
    a = 2.0*h*pi*c**2
    b = h*c/(wav*k*T)
    intensity = a/ ( (wav**5)*(math.e**b - 1.0) )
    return intensity

How to define wavelength wav and thus produce the plot of Planck's Formula?
Comment: Usually the wavelength is just even spaced vector, you can use `np.arange` or `np.linspace`. Remember the indent in your planck function.
Comment: Presumably you want a range of wavelengths. Do you know what range? Look into [`range`](http://docs.python.org/2.7/library/functions.html#range).
Answer: Here's a basic plot.  To plot using plt.plot(x, y, fmt) you need two arrays x and y of the same size, where x is the x coordinate of each point to plot and y is the y coordinate, and fmt is a string describing how to plot the numbers.
So all you need to do is create an evenly spaced array of wavelengths (an np.array which I named wavelengths).  This can be done with arange(start, end, spacing) which will create an array from start to end (not inclusive) spaced at spacing apart.
Then compute the intensity using your function at each of those points in the array (which will be stored in another np.array), and then call plt.plot to plot them.  Note numpy let's you do mathematical operations on arrays quickly in a vectorized form which will be computationally efficient.
import matplotlib.pyplot as plt
import numpy as np

h = 6.626e-34
c = 3.0e+8
k = 1.38e-23

def planck(wav, T):
    a = 2.0*h*c**2
    b = h*c/(wav*k*T)
    intensity = a/ ( (wav**5) * (np.exp(b) - 1.0) )
    return intensity

# generate x-axis in increments from 1nm to 3 micrometer in 1 nm increments
# starting at 1 nm to avoid wav = 0, which would result in division by zero.
wavelengths = np.arange(1e-9, 3e-6, 1e-9)

# intensity at 4000K, 5000K, 6000K, 7000K
intensity4000 = planck(wavelengths, 4000.)
intensity5000 = planck(wavelengths, 5000.)
intensity6000 = planck(wavelengths, 6000.)
intensity7000 = planck(wavelengths, 7000.)

plt.plot(wavelengths*1e9, intensity4000, 'r-')
# plot intensity4000 versus wavelength in nm as a red line
plt.plot(wavelengths*1e9, intensity5000, 'g-') # 5000K green line
plt.plot(wavelengths*1e9, intensity6000, 'b-') # 6000K blue line
plt.plot(wavelengths*1e9, intensity7000, 'k-') # 7000K black line

# show the plot
plt.show()

And you see:

You probably will want to clean up the axes labels, add a legend, plot the intensity at multiple temperatures on the same plot, among other things.  Consult the relevant matplotlib documentation.
Comment: why `math.e**b` instead of just `np.exp(b)`
Comment: @DevonDahon - Matplotlib 2.1 with the deprecation was from October 2017.  This comment predates that by three years.  Will edit though.  I believe just need to remove call to `hold()` as it's the default behavior.
Comment: Because I took the function from `user3421850`'s question and they used `math.e**b`.
Comment: @Zhenya - Also now that I look at it, their formula was incorrect, since they have an extra pi in the formula for `a`.  (Note h is Planck's constant, not h-bar (Planck's reduced constant ħ = h/2pi).)
Comment: Re units: it's anyway better to always use dimensionless units rather than (arbitrary) SI units. Write the formula on a piece of paper, use substitutions (here, x = \hbar * \omega / k T), express both rhs and lhs in terms of dimensionless functions of dimensionless variables, plot those (and use axis labels for stating the exact variables).
Comment: @Zhenya - Why are you commenting to me about this?  The `planck` equation was taken from OP's question only trivially modified initially (add indentation and remove parenthesis on return statement).  There are many situations it makes more sense to plot in SI units; e.g., when engineering or when you have familiarity with wavelengths (e.g., visible is ~400nm (violet) - 700nm (red)).  Furthermore you should never drop or unnecessarily add dimensionless constants like pi as you can never add them back in through dimensional analysis (bundling with another variable is fine).
Comment: @Zhenya - The original question wanted intensity vs wavelength not frequency, so your substitution is something quite different.  Note its B_\nu d\nu = - B_\lambda d\lambda, so the Blackbody formula plotted against frequency and wavelength will for example have peaks at different points (peak frequency != c/peak wavelength). Finally, T  varies independently of wavelength (and frequency) and wavelength (and frequency) appear in other parts of the formula, so it doesn't make sense to bundle them together. (Doing so will make ugly/bizarre units.) But, feel free to make a plot of your own.
Comment: `plt.hold(True)` functionality was [deprecated](https://github.com/matplotlib/matplotlib/issues/12337) in 2.1, and intentionally removed in 3.0. It will return the following error: `AttributeError: module 'matplotlib.pyplot' has no attribute 'hold'`.
Answer: You may also want to use the RADIS library, which allows you to plot the Planck function against wavelengths, or against frequency / wavenumber, if needed !
from radis import sPlanck

sPlanck(wavelength_min=135, wavelength_max=3000, T=4000).plot()
sPlanck(wavelength_min=135, wavelength_max=3000, T=5000).plot(nfig='same')
sPlanck(wavelength_min=135, wavelength_max=3000, T=6000).plot(nfig='same')
sPlanck(wavelength_min=135, wavelength_max=3000, T=7000).plot(nfig='same')

Plancks Formula for Blackbody spectrum


How many times can a file be compressed?

Question: I was thinking about compression, and it seems like there would have to be some sort of limit to the compression that could be applied to it, otherwise it'd be a single byte.
So my question is, how many times can I compress a file before:

It does not get any smaller?
The file becomes corrupt?

Are these two points the same or different?
Where does the point of diminishing returns appear?
How can these points be found?
I'm not talking about any specific algorithm or particular file, just in general.
Comment: I have heard of a compression algorithm, that if run over and over again eventually reduced the file size to 1 byte. Using it I managed to store every file ever created in just one zip file - and it was smaller than 1KB! Some people say the algorithm was a bit lossy. I say, however, that the space savings more than compensated for the slight loss of precision. ;)
Comment: How many roads must a man walk down?
Comment: you should clarify if you ask for lossless, lossy, or both, data compression.
Comment: @JeffreyKemp Could you be talking about Matt Mahoney's BARF compressor? http://mattmahoney.net/dc/barf.html
Comment: @jchevali looks like they have come a long way in compression technology!
Comment: @JeffreyKemp if you are referring to BARF compressor then I would like to mention that the compressor is a cheat. It doesn't actually compress the data but rather starts removing bytes from actual data and place them in `File Name` hence reducing the file size but the truth is its increasing the file name 3 times more than the original file.
Comment: About 80% of the time when I decompress a double-compressed video it fails to load. Annoying. Just get a good compressor and leave it alone.
Answer: For lossless compression, the only way you can know how many times you can gain by recompressing a file is by trying. It's going to depend on the compression algorithm and the file you're compressing.
Two files can never compress to the same output, so you can't go down to one byte. How could one byte represent all the files you could decompress to?
The reason that the second compression sometimes works is that a compression algorithm can't do omniscient perfect compression. There's a trade-off between the work it has to do and the time it takes to do it. Your file is being changed from all data to a combination of data about your data and the data itself.
Example
Take run-length encoding (probably the simplest useful compression) as an example.
04 04 04 04 43 43 43 43 51 52 11 bytes
That series of bytes could be compressed as:
[4] 04 [4] 43 [-2] 51 52 7 bytes (I'm putting meta data in brackets)
Where the positive number in brackets is a repeat count and the negative number in brackets is a command to emit the next -n characters as they are found.
In this case we could try one more compression:
[3] 04 [-4] 43 fe 51 52 7 bytes (fe is your -2 seen as two's complement data)
We gained nothing, and we'll start growing on the next iteration:
[-7] 03 04 fc 43 fe 51 52 8 bytes
We'll grow by one byte per iteration for a while, but it will actually get worse. One byte can only hold negative numbers to -128. We'll start growing by two bytes when the file surpasses 128 bytes in length. The growth will get still worse as the file gets bigger.
There's a headwind blowing against the compression program--the meta data. And also, for real compressors, the header tacked on to the beginning of the file. That means that eventually the file will start growing with each additional compression.

RLE is a starting point. If you want to learn more, look at LZ77 (which looks back into the file to find patterns) and LZ78 (which builds a dictionary). Compressors like zip often try multiple algorithms and use the best one.
Here are some cases I can think of where multiple compression has worked.

I worked at an Amiga magazine that shipped with a disk. Naturally, we packed the disk to the gills. One of the tools we used let you pack an executable so that when it was run, it decompressed and ran itself. Because the decompression algorithm had to be in every executable, it had to be small and simple. We often got extra gains by compressing twice. The decompression was done in RAM. Since reading a floppy was slow, we often got a speed increase as well!
Microsoft supported RLE compression on bmp files. Also, many word processors did RLE encoding. RLE files are almost always significantly compressible by a better compressor.
A lot of the games I worked on used a small, fast LZ77 decompressor. If you compress a large rectangle of pixels (especially if it has a lot of background color, or if it's an animation), you can very often compress twice with good results. (The reason? You only have so many bits to specify the lookback distance and the length, So a single large repeated pattern is encoded in several pieces, and those pieces are highly compressible.)
Answer: Generally the limit is one compression. Some algorithms results in a higher compression ratio, and using a poor algorithm followed by a good algorithm will often result in improvements. But using the good algorithm in the first place is the proper thing to do.
There is a theoretical limit to how much a given set of data can be compressed. To learn more about this you will have to study information theory.
Comment: Regarding theoretical limit: yes, a good place to start is with the work of Claude Shannon. However, it doesn't say how a given compression algorithm will compress the data, and predicting the _number_ of compression steps with profit is pretty hopeless.
Comment: "good" is subjective. yes using lzma2 on lzo has "improvements", but one might need lzo for extremely compatible and fast compression etc.
Answer: In general for most algorithms, compressing more than once isn't useful. There's a special case though.
If you have a large number of duplicate files, the zip format will zip each independently, and you can then zip the first zip file to remove duplicate zip information. Specifically, for 7 identical Excel files sized at 108kb, zipping them with 7-zip results in a 120kb archive. Zipping again results in an 18kb archive. Going past that you get diminishing returns.
Comment: Good example. I worked on a few videogames where double-compression was used. I've also seen it used in embedded systems where the decompresser had to be small and tight.
Answer: Suppose we have a file N bits long, and we want to compress it losslessly, so that we can recover the original file.  There are 2^N possible files N bits long, and so our compression algorithm has to change one of these files to one of 2^N possible others.  However, we can't express 2^N different files in less than N bits.
Therefore, if we can take some files and compress them, we have to have some files that length under compression, to balance out the ones that shorten.
This means that a compression algorithm can only compress certain files, and it actually has to lengthen some.  This means that, on the average, compressing a random file can't shorten it, but might lengthen it.
Practical compression algorithms work because we don't usually use random files.  Most of the files we use have some sort of structure or other properties, whether they're text or program executables or meaningful images.  By using a good compression algorithm, we can dramatically shorten files of the types we normally use.
However, the compressed file is not one of those types.  If the compression algorithm is good, most of the structure and redundancy have been squeezed out, and what's left looks pretty much like randomness.
No compression algorithm, as we've seen, can effectively compress a random file, and that applies to a random-looking file also.  Therefore, trying to re-compress a compressed file won't shorten it significantly, and might well lengthen it some.
So, the normal number of times a compression algorithm can be profitably run is one.
Corruption only happens when we're talking about lossy compression.  For example, you can't necessarily recover an image precisely from a JPEG file.  This means that a JPEG compressor can reliably shorten an image file, but only at the cost of not being able to recover it exactly.  We're often willing to do this for images, but not for text, and particularly not executable files.
In this case, there is no stage at which corruption begins.  It starts when you begin to compress it, and gets worse as you compress it more.  That's why good image-processing programs let you specify how much compression you want when you make a JPEG:  so you can balance quality of image against file size.  You find the stopping point by considering the cost of file size (which is more important for net connections than storage, in general) versus the cost of reduced quality.  There's no obvious right answer.
Answer: Usually compressing once is good enough if the algorithm is good.
In fact, compressing multiple times could lead to an increase in the size
Your two points are different.

Compression done repeatedly and achieving no improvement in size reduction
is an expected theoretical condition
Repeated compression causing corruption
is likely to be an error in the implementation (or maybe the algorithm itself)

Now lets look at some exceptions or variations,

Encryption may be applied repeatedly without reduction in size
(in fact at times increase in size) for the purpose of increased security
Image, video or audio files increasingly compressed
will loose data (effectively be 'corrupted' in a sense)
Comment: I think it should be noted that image, video, and audio files would only be 'corrupted' and lose date if a lossy compression (such as mp3, divx, etc.) is used.  If the compression is lossless, then the output of the compression is effectively the same data, only recorded in a different number of bytes.
Comment: It's a good idea to apply compression before encryption, because encryption usually disrupts the patterns that (most) compression algorithms use to do their magic.
Comment: @Totty, your point is well taken. A good example for audio is FLAC against MP3.
Comment: @dar7yl, you are right. It is also a good idea to TAR first and then compress to get better patterns across the complete data (rather than individual file compresses).
Answer: You can compress infinite times. However, the second and further compressions usually will only produce a file larger than the previous one. So there is no point in compressing more than once.
Comment: You would need infinite storage, though. You'd use up the universe.
Answer: Compression (I'm thinking lossless) basically means expressing something more concisely. For example
111111111111111

could be more consisely expressed as
15 X '1'

This is called run-length encoding. Another method that a computer can use is to find a pattern that is regularly repeated in a file.
There is clearly a limit to how much these techniques can be used, for example run-length encoding is not going to be effect on
15 X '1'

since there are no repeating patterns. Similarly if the pattern replacement methods converts long patterns to 3 char ones, reapplying it will have little effect, because the only remaining repeating patterns will be 3-length or shorter. Generally applying compression to a already compressed file makes it slightly bigger, because of various overheads. Applying good compression to a poorly compressed file is usually less effective than applying just the good compression.
Answer: You can compress a file as many times as you like.  But for most compression algorithms the resulting compression from the second time on will be negligible.
Comment: It might get smaller, it might stay the same, and depending on the algorithm, I think you might see the file size increase just a bit.
Answer:
How many times can I compress a file before it does not get any smaller?

In general, not even one. Whatever compression algorithm you use, there must always exists a file that does not get compressed at all, otherwise you could always compress repeatedly until you reach 1 byte, by your same argument.

How many times can I compress a file before it becomes corrupt?

If the program you use to compress the file does its job, the file will never corrupt (of course I am thinking to lossless compression).
Comment: He, don't stop at 1 byte, continue until you have 1 bit!
Comment: in fact AT LEAST HALF of all files will become larger, or remain the same size with any compression algorithm.

However, this says nothing about USEFUL files, which usually contain non-random data, and thus is usually compressible
Answer: It is a very good question. You can view to file from different point of view. Maybe you know a priori that this file contain arithmetic series.
Lets view to it as datastream of "bytes", "symbols", or "samples".
Some answers can give to you "information theory" and "mathematical statistics"
Please check monography of that researchers for full-deep understanding:
A. Kolmogorov
S. Kullback
С. Shannon
N. Wiener
One of the main concept in information theory is entropy.
If you have a stream of "bytes"....Entropy of that bytes doesn't depend on values of your "bytes", or "samples"...
If was defined only by frequencies with which bytes retrive different values.
Maximum entropy has place to be for full random datastream.
Minimum entropy, which equal to zero, has place to be for case when your "bytes" has identical value.

It does not get any smaller?

So the entropy is minimum number of bits per your "byte", which you need to use when writing information to the disk. Of course it is so if you use god's algorithm. Real life compression lossless heuristic algorithms are not so.

The file becomes corrupt?

I dont understand sense of the question. You can write no bits to the disk and you will write a corrupted file to the disk with size equal to 0 bits. Of course it is corrupted, but his size is zero bits.
Answer: Here is the ultimate compression algorithm (in Python) which by repeated use will compress any string of digits down to size 0 (it's left as an exercise to the reader how to apply this to a string of bytes).

def compress(digitString):
    if digitString=="":
        raise "already as small as possible"
    currentLen=len(digitString)
    if digitString=="0"*currentLen:
        return "9"*(currentLen-1)
    n=str(long(digitString)-1); #convert to number and decrement
    newLen=len(n);
    return ("0"*(currentLen-newLen))+n; # add zeros to keep same length

#test it
x="12";
while not x=="":
    print x;
    x=compress(x)

The program outputs 12 11 10 09 08 07 06 05 04 03 02 01 00 9 8 7 6 5 4 3 2 1 0 then empty string. It doesn't compress the string at each pass but it will with enough passes compress any digit string down to a zero length string. Make sure you write down how many times you send it through the compressor otherwise you won't be able to get it back.
Comment: It is pretty funny, it's really just a reverse iterable counter with a level of obfuscation.
Comment: I usually hold back myself from down-voting. But this answer forces me to.
Answer: I would like to state that the limit of compression itself hasn't really been adapted to tis fullest limit. Since each pixel or written language is in black or write outline. One could write a program that can decompile into what it was, say a book, flawlessly, but could compress the pixel pattern and words into a better system of compression. Meaning It would probably take a lot longer to compress, but as a system file gets larget gigs or terra bytes, the repeated letters of P and R and q and the black and white deviations could be compressed expotentially into a complex automated formula. THe mhcien doesn't need the data to make sense, it just can make a game making a highly compressed pattern. This in turn then allows us the humans to create a customized compression reading engine. Meaning now we have real compression power. Design an entire engine that can restore the information on the user side. The engine has its own language that is optimal, no spaces, just fillign black and white pixel boxes of the smallest set or even writing its own patternaic language. Nad thus it can at the same time for the mostoptiaml performace, give out a unique cipher or decompression formula when its down, and thus the file is optimally compressed and has a password that is unique for the engine to decompress it later. The machine can do amost limitlesset of iterations to compress the  file further. Its like having a open book and putting all the written stories of humanity currently on to one A4 sheet. I don't know but it is another theory. So what happens is split volume, because the formula to decrompress would have its own size, evne the naming of the folder and or icon information has a size so one could go further to put every form of data a a string of information. hmm..
Answer: It all depends on the algorithm. In other words the question can be how many times a file can be compressed using this algorithm first, then this one next...
Answer: Example of a more advanced compression technique using "a double table, or cross matrix"
Also elimiates extrenous unnessacry symbols in algorithm
[PREVIOUS EXAMPLE]
Take run-length encoding (probably the simplest useful compression) as an example.
04 04 04 04 43 43 43 43 51 52 11 bytes
That series of bytes could be compressed as:
[4] 04 [4] 43 [-2] 51 52 7 bytes (I'm putting meta data in brackets)
[TURNS INTO]
04.43.51.52      VALUES
4.4.**-2         COMPRESSION
Further Compression Using Additonal Symbols as substitute values
04.A.B.C         VALUES
4.4.**-2         COMPRESSION
Answer: In theory, we will never know, it is a never-ending thing:

In computer science and mathematics, the term full employment theorem
  has been used to refer to a theorem showing that no algorithm can
  optimally perform a particular task done by some class of
  professionals. The name arises because such a theorem ensures that
  there is endless scope to keep discovering new techniques to improve
  the way at least some specific task is done. For example, the full
  employment theorem for compiler writers states that there is no such
  thing as a provably perfect size-optimizing compiler, as such a proof
  for the compiler would have to detect non-terminating computations and
  reduce them to a one-instruction infinite loop. Thus, the existence of
  a provably perfect size-optimizing compiler would imply a solution to
  the halting problem, which cannot exist, making the proof itself an
  undecidable problem.

(source)


Modeling a Poisson RV with an unknown number of structural changes

Question: I have some count data for user-interaction over the past 365 days. I have reason to believe that several events have occurred which change the rate at which users are interacting. The model is as follow:
Assumptions

Daily count data is (locally) drawn from a Poisson distribution with parameter lambda
There are between 0 and n<365 structural changes, i.e. lambda changes
These changes can occur at any time during the 365 day period

Desired Answers

How many times did a likely structural change occur?
When did these changes happen?

I would like to instantiate this model with tensorflow_probability. The model described near the end of this chapter seems like a good starting place. However the number of structural changes is hard-coded to 1. How can I expand this model to handle an unknown number of changes?
EDIT
This is a modified version of the code mentioned above. It allows for an arbitrary number of switchpoints. Inspired by Dave Moore's answer below, I've allowed tau to have "out-of-bounds" elements by multiplying by 2. Stylistically, I'm concerned about the computation for indices, as I think it's a bit confusing to understand what is happening. However, I can't think of a better way to do it. Functionally, I'm worried about the effect that the out-of-bounds values may have on the log probability.
def joint_log_prob(count_data, taus, lambdas, max_switches):
  rate = np.array(1./count_data_.mean(), np.float32)
  lambdas_prior = tfd.Exponential(rate)
  taus_prior = tfd.Uniform()

  A = tf.gather(
        taus * ndays,
        indices=tf.stack([tf.fill([ndays], i) for i in range(max_switches-1)])
  )
  B = tf.to_float(tf.range(ndays))
  indices = tf.reduce_sum(tf.to_int32(tf.less(A, B)), axis=0)
  lambda_ = tf.gather(lambdas, indices)
  count_data_prior = tfd.Poisson(lambda_)

  return (
      tf.reduce_sum(count_data_prior.log_prob(count_data))
    + tf.reduce_sum(taus_prior.log_prob(taus))
    + tf.reduce_sum(lambdas_prior.log_prob(lambdas))
  )

Answer: You're exactly right that models with an random number of latent variables are tricky to write in most existing tools, including TFP, because they require the shape of the inference computation to change dynamically during inference: the set of things to infer is itself one of the quantities you're doing inference over. That doesn't (easily) fit into standard computation graph frameworks.
The usual solution is to rewrite the model to use a fixed number of variables. For example, if you think there will be at most 20 structural changes, you could write a model with 20 changepoints, where some are allowed to be 'dummy' changepoints (e.g., occurring outside the length of the observed signal), so that only a subset are actually active.
A related approach is to treat the structural uncertainty as a problem of Bayesian model selection: instead of fitting a model with uncertain structure, you consider several models, each with different number of variables, and compare them using the marginal likelihood.
Unfortunately I don't know of any concrete examples applying these approaches specifically to changepoint models -- maybe others will have better references?
Comment: I like the dummy changepoint idea. So the Poisson parameter would be built up as slices of a fixed-length vector, but not every slice would be included. That definitely gives me a direction to go in--thank you!


Repository pattern implementation with ninject

Question: In my project, I trying to implement repository pattern and unit of work.
I found some web site to describe how to implement it such as:
http://www.codeproject.com/Articles/688929/Repository-Pattern-and-Unit-of
http://www.codeproject.com/Articles/561584/Repository-Pattern-with-Entity-Framework-using
I was wondering, why is not generic Unit of Work and Repositories Framework? then try several search on internet and I found it,
http://genericunitofworkandrepositories.codeplex.com/
This framework is first code but my project is model first therefore is not work correctly?
Could you please suggest me model first framework like this?
My project is a internet web site with one database, If there is plausible reason I can change model first approach to code first approach.
Thanks for you time.
Comment: coolblue2000, One of the primary reasons for implementing the repository pattern is to minimize the surface area of your our ORM technology e.g. EF, nHibernate etc. Compartmentalization of where your CRUD is happening in your application, so instead of having CRUD happening throughout your presentation layer you have them the repository layer, where they can also reused as well. This also provides abstraction, which in return if you were to ever switch ORM technologies, you would be mitigating how much refactoring you would need to do. Read up on this for many more reasons for this pattern.
Comment: Entity Framework is already a repository pattern so you do not need to wrap it up in another.
Comment: @coolblue2000 thanks for your response, but if EF is already repo pattern, why developed http://genericunitofworkandrepositories.codeplex.com/ for EF6?
Comment: No idea. EF6 is also Mockable so there is no need to wrap it up at all. The entities themselves are the repositories (in the repository pattern), these have calls to update, get and save data.
Answer: We've abstracted all the interfaces in our latest release into Repository.Pattern project https://genericunitofworkandrepositories.codeplex.com/SourceControl/latest#main/Source/Repository.Pattern, in plans to implement nHibernate provider. You are more than welcome to start implementing these interfaces, based on bandwidth at the moment, I cannot commit to any dates as of yet.


Discrepancies of Percent Encoding for URLs

Question: After viewing this previous SO question regarding percent encoding, I'm curious as to which styles of encodings are correct - the Wikipedia article on percent encoding alludes to using + instead of %20 for spaces, while still having an application/x-www-urlencoded content type.
This leads me to think the + vs. %20 behavior depends on which part of the URL is being encoded. What differences are preferred for path segments vs. query strings? Details and references for this specification would be greatly appreciated.

Note: I assume that non-alphanumeric characters will be encoded via UTF-8, in that each octet for a character becomes a %XX string. Correct me if I am wrong here (for instance latin-1 instead of utf-8), but I am more interested in the differences between the encodings of different parts of a URL.
Answer:
This leads me to think the + vs. %20 behavior depends on which part of the URL is being encoded.

Not only does it depend on the particular URL component, but it also depends on the circumstances in which that component is populated with data.
The use of '+' for encoding space characters is specific to the application/x-www-form-urlencoded format, which applies to webform data that is being submitted in an HTTP request.  It does not apply to a URL itself.
The application/x-www-form-urlencoded format is formally defined by W3C in the HTML specifications.  Here is the definition from HTML 4.01:
Section 17.13.3 Processing form data, Step four: Submit the encoded form data set

This specification does not specify all valid submission methods or content types that may be used with forms. However, HTML 4 user agents must support the established conventions in the following cases:
• If the  method is "get" and the action is an HTTP URI, the user agent takes the value of action, appends a `?' to it, then appends the form data set, encoded using the "application/x-www-form-urlencoded" content type. The user agent then traverses the link to this URI. In this scenario, form data are restricted to ASCII codes.
• If the  method is "post" and the action is an HTTP URI, the user agent conducts an HTTP "post" transaction using the value of the action attribute and a message created according to the  content type specified by the enctype attribute.

Section 17.13.4 Form content types, application/x-www-form-urlencoded

This is the default content type. Forms submitted with this content type must be encoded as follows:
1.Control names and values are escaped. Space characters are replaced by '+', and then reserved characters are escaped as described in  [RFC1738], section 2.2: Non-alphanumeric characters are replaced by '%HH', a percent sign and two hexadecimal digits representing the ASCII code of the character. Line breaks are represented as "CR LF" pairs (i.e., '%0D%0A').
2.The control names/values are listed in the order they appear in the document. The name is separated from the value by '=' and name/value pairs are separated from each other by '&'.

The corresponding HTML5 definitions (Section 220.127.116.11 Form submission algorithm and Section 184.108.40.206 URL-encoded form data) are way more refined and detailed, but for purposes of this discussion, the jist is roughly the same.
So, in the situation where the webform data is submitted via an HTTP GET request instead of a POST request, the webform data is encoded using application/x-www-form-urlencoded and placed as-is in the URL query component.
Per RFC 3986: Uniform Resource Identifier (URI): Generic Syntax:

URI producing applications should percent-encode data octets that correspond to characters in the reserved set unless these characters are specifically allowed by the URI scheme to represent data in that component.

'+' is a reserved character:
reserved    = gen-delims / sub-delims

gen-delims  = ":" / "/" / "?" / "#" / "[" / "]" / "@"

sub-delims  = "!" / "$" / "&" / "'" / "(" / ")"
            / "*" / "+" / "," / ";" / "="

The query component explicitly allows unencoded '+' characters, as it allows characters from sub-delims:
unreserved  = ALPHA / DIGIT / "-" / "." / "_" / "~"

pct-encoded = "%" HEXDIG HEXDIG

pchar       = unreserved / pct-encoded / sub-delims / ":" / "@"

query       = *( pchar / "/" / "?" )

So, in the context of a webform submission, spaces are encoded using '+' prior to then being put as-is into the query component.  This is allowed by the URL syntax, since the encoded form of application/x-www-form-urlencoded is compatible with the definition of the query component.
So, for example: http://server/script?field=hello+world
However, outside of a webform submission, putting a space character directly into the query component requires the use of pct-encoded, since ' ' is not included in either unreserved or sub-delims, and is not explicitly allowed by the query definition.
So, for example: http://server/script?hello%20world
Similar rules also apply to the path component, due to its use of pchar:
  path          = path-abempty    ; begins with "/" or is empty
                / path-absolute   ; begins with "/" but not "//"
                / path-noscheme   ; begins with a non-colon segment
                / path-rootless   ; begins with a segment
                / path-empty      ; zero characters

  path-abempty  = *( "/" segment )
  path-absolute = "/" [ segment-nz *( "/" segment ) ]
  path-noscheme = segment-nz-nc *( "/" segment )
  path-rootless = segment-nz *( "/" segment )
  path-empty    = 0<pchar>
  segment       = *pchar
  segment-nz    = 1*pchar
  segment-nz-nc = 1*( unreserved / pct-encoded / sub-delims / "@" )
                ; non-zero-length segment without any colon ":"

So, although path does allow for unencoded sub-delims characters, a '+' character gets treated as-is, not as an encoded space.  application/x-www-form-urlencoded is not used with the path component, so a space character has to be encoded as %20 due to the definitions of pchar and segment-nz-nc.
Now, regarding the charset used to encode characters -
For a webform submission, that charset is dictated by rules defined in the webform encoding algorithm (more so in HTML5 than HTML4) used to prepare the webform data prior to inserting it into the URL.  In a nutshell, the HTML can specify an accept-charset attribute or hidden _charset_ field directly in the <form> itself, otherwise the charset is typically the charset used by the parent HTML.
However, outside of a webform submission, there is no formal standard for which charset is used to encode non-ascii characters in a URL component (the IRI syntax, on the other hand, requires UTF-8 especially when converting an IRI into an URI/URL). Outside of IRI, it is up to particular URI schemes to dictate their charsets (the HTTP scheme does not), otherwise the server decides which charset it wants to use. Most schemes/servers use UTF-8 nowadays, but there are still some servers/schemes that use other charsets, typically based on the server's locale (Latin1, Shift-JIS, etc).  There have been attempts to add charset reporting directly in the URL and/or in HTTP (such as Deterministic URI Encoding
), but those are not commonly used.
Comment: A URL processor will never treat a `+` as a space, only `%20`. *After* the query has been decoded, a *subsequent* `application/x-www-form-urlencoded` processor on the decoded query will then treat `+` has a space.
Comment: Just to make sure, if a `%20` exists in a query string, then all `+` are considered a normal plus sign. If there isn't one, then it's treated as a space?


Logic Pro X: create summing track stack of already existing track stacks

Question: I would like to create a track stack of track stacks. When I attempt to create, it says that I can only create a "folder" track stack and not a "summing" track stack.
My questions:

How comes? Why is this a thing, does anybody know the reason behind this?
How do I work around this issue?
Comment: I don't know Logic, but I guess there must be an option for a "bus track" or "fx track" to achieve that.
Comment: I think it's not possible to make summing into summing,you can use VCA faders or try you question on Logic Pro Help forum
Comment: Hey, thank you for the comments you all, I appreciate that
Answer:
How comes? Why is this a thing, does anybody know the reason behind this?

No idea.

How do I work around this issue?

Just use buses and auxiliary channels.


Points on which function is invertible?

Question: $f: \mathbb R ^{2}\mapsto \mathbb R ^{2}$
$f(x,y)\mapsto((x-y)^{2}+1,  x-y^{3}-2)$
For which points is this function invertible?
I calculated the Jacobian matrix, but what should I do next to get specific points?
$J=\begin{pmatrix} 2x-2y & -2x+2y\\ 1 & -3y^{2}   \end{pmatrix}  $
Comment: The last entry in $J$ should be $-3y^2$
Comment: By invertible I assume you mean locally invertible?
Comment: @zhw yes i mean that
Answer: The function will be invertible as long as the determinant of its Jacobian is not 0.  Wasn't that the reason you found the Jacobian in the first place?
Comment: With two unknowns, the equation gives a _relation_ between the coordinates of the points:  there is an infinite number of such points.
Comment: Yes. But with two unknowns, I cant solve equation.
Answer: $\det(J) = (2x-2y)(-3y^2) + (2x -2y) = (2x-2y)(-3y^2+1) = 0$
The function is not invertable when $x= y$ or when $y = +-1/√3$
Of course, this is accepting that $J$ is correct.
Comment: In general, not quite true; think about $f(x,y) = (x^3, y)$.

Points on which function is invertible?


Let $a,b>0$ and $a+b\leqslant 2$. Prove that $\left(\frac{a}{b}\right)^{b} +\left(\frac{b}{a}\right)^{a} \leqslant 2.$

Question: Let $\displaystyle a,b>0$ and $\displaystyle a+b\leqslant 2$. Prove that $$\displaystyle \left(\frac{a}{b}\right)^{b} +\left(\frac{b}{a}\right)^{a} \leqslant 2.$$
(reduce fractions is not a smart idea I think)
Comment: Welcome to Mathematics SE. Take a tour: https://math.stackexchange.com/tour.  You'll find that simple "Here's the statement of my question, solve it for me" posts will be poorly received. What is better is for you to add context by stating what you understand about the problem, what you've tried so far, etc.; both to show you are part of the learning experience and to help us guide you to the appropriate help. You can consult this link for further guidance: https://math.meta.stackexchange.com/q/9959 .
Comment: It is interesting. You may edit your question including some information (e.g. source, attempt). Perhaps someone will vote to reopen it.
Answer: $(\frac{a}{b})^b+(\frac{b}{a})^a=e^{b\ln(\frac{a}{b})}+e^{a\ln(\frac{b}{a})}$. Let $x=\ln(\frac{a}{b})$, then $e^{bx}+e^{-ax}=\frac{e^{(a+b)x}+1}{e^{ax}}<\frac{e^{2x}+1}{e^{ax}}$ Since $a>0$ , $b>0$ and $a+b<2$, we have $a<2$. From that we deduce, $(2-a)x<0$ and then $e^{(2-a)x}<1$. Hope this helps.
Comment: Typo:$x=ln(\frac{a}{b})$


How to manipulate a Solidity struct with Solidity?

Question: I'm writing a smart contract where I'm creating an object (struct) in one function, and then I want to alter the properties in a different function. One of the struct's properties is added in a mapping and that's how I track them, but even though I have created a function to get the properties of the struct from the mapping, I still cant quite figure out how to change them.
This is the struct
struct Component {

    uint compID;
    string compManufacturer;
    string compDetails;
    address owner;
    address[] pastOwners;
    bool transfer;
}

This is an example of a function where I try to change the values in the struct.
function transfer (address factory, uint id) public {

    address compad;
    address[] compowners;
    bool boolean;

    (,,, compad, compowners, boolean) = getComponent(id);

    require(component.transfer == true);

    component.owner = factory;
    pastOwners = pastOwners.push(factory);
}

The function getComponent returns everything that is in the struct and here I initialize some local variables to store the ones that I need. However, I'm not sure what I do afterwards works, because this way the local variables seem useless, and if I change the local variables the changes won't go through in the actual object. Please correct me if I'm wrong. Any help appreciated.
Answer: Assuming you have a mapping such as:
mapping(uint => Component) public components;

You can manipulate like this:
components[id].compDetails = "comp details";


How to keep secrets as part of configmap in kubernetes?

Question: I have a config map which creates a config file to my application.
The structure is as below.
apiVersion: v1
kind: ConfigMap
metadata:
  name: database-configmap
data:
  config: |
   dbport=5432
   dcname=
   dbssl=false
   locktime=300
   domainuser=
   dattserverhost=localhost
   conntype=ON-PREM
   dbinstance=
   dattusessl=false
   dbpwd=VrjvBguQ
   iisport=80
   docountupgrade=false
   doreportupgrade=false
   dbname=dattdatabase
   dattuseiis=false
   dbtype=POSTGRESQL
   dbusername=postg
   dbserver=tgres.database.azure.com
   domainpwd=

Complete file will be dumped to a properties file so the application would use that.
Is there anyway to save some properties (like dbusername,dbpassword) in it as a kubernetes secret and when the deployment pods are created,  these secrets and configmap will be merged to the properties file?
Please suggest.
Answer: Short answer: don't.
Secrets and ConfigMaps are different objects. Secrets are obfuscated with a Base64 encoding and should be used for confidential data while ConfigMaps are used for non-confidential data.
You should make separate objects for confidential and non-confidential data and than use them in your Deployment. You can modify your Deployment to use both Secrets and ConfigMaps, for example:
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
  name: envtest
spec:
  replicas: 1
  template:
    metadata:
      labels:
        name: envtest
    spec:
      containers:
      - name: envtest
        image: gcr.io/<PROJECT_ID>/envtest
        ports:
        - containerPort: 3000
        env:
        - name: LANGUAGE
          valueFrom:
            configMapKeyRef:
              name: language
              key: LANGUAGE
        - name: API_KEY
          valueFrom:
            secretKeyRef:
              name: apikey
              key: API_KEY

There are also different ways of using both Secrets and ConfigMaps. For more details, see the sources below:

Secrets

ConfigMaps

Kubernetes ConfigMaps and Secrets

An Introduction to Kubernetes Secrets and ConfigMaps
Comment: Yes, and this topic was already covered as you shouldn't try to do it that way. That -1 for your question is not from me but it shows that you are asking for something that you should not do. The second issue is: how to configure a `deployment` to use both `secrets and `configmaps` and to get a good answer for that you should ask a separate question showing your configs and explaining the use case instead of mixing it all together here as this is not how SO and SF are supposed to work. Trust me and take my honest advice :)
Comment: the application configuration is saved to the properties file inside the container.Can we update the part of the configuration inside it with configmap and remaining part with secrets. We just need the username and password part of database to be set like that
Comment: Yes, this is a good approach.
Comment: can you suggest how to do that, because i wasn't able to merge to a single file both secrets and configmap
Comment: Hello @uday. It would be better if you post a separate question for that. Show your configs, what you have tried and what would you like to achieve. That way it's more clear for the community and we don't mix multiple topics/tasks in one question.
Comment: But that is the main topic of the question. to keep secrets as part of the configmap
Comment: The above configmap sample only database password(dbpwd) part we will keep in secrets, everything is in configmap only. So, how to link this in the configmap final file


Mysql Query data between date on joined table

Question: Am trying to query data from two joined tables with a date as criteria using the query below but it brings nothing, can someone please show me what I did wrong.
SELECT Journal_T.GL_ID as Akaunti
      ,coa_t.gl_name_vc
      ,SUM(Amount_NU) as YTD
FROM  Journal_T
      JOIN coa_t
         ON journal_t.gl_id = coa_t.gl_id
WHERE CAST(date_dt AS DATE) BETWEEN '25/08/2017' AND '25/08/2017'
      AND bs_category_vc='Rev'
GROUP BY coa_t.gl_name_vc
      ,Journal_T.GL_ID

Comment: are you sure on such cast as date? is high dependent on locale (country) active on mysql server
Comment: Can you post some data, specifically what date_dt looks like. Preferably on a record you know should appear in the list
Comment: This sample date_dt 25/08/2017 03:08:11
Comment: @JacekCz I cast to remove timestamp part of date
Comment: So just tried this and CAST AS DATE returns Null. You should use STR_TO_DATE as @vladatr has suggested
Comment: @twoleggedhorse Yes sure I removed CAST too.
Comment: This work for me: select Journal_T.GL_ID as Akaunti,coa_t.gl_name_vc,sum(Amount_NU) as YTD
from Journal_T JOIN coa_t on journal_t.gl_id = coa_t.gl_id
where date(date_dt) BETWEEN '2017-08-25' AND '2017-08-25' AND bs_category_vc='Rev'
GROUP by coa_t.gl_name_vc,Journal_T.GL_ID
Answer: ISO 8601 Date format would be '2017-08-26'. Try this query to see default Date format on your server:
select STR_TO_DATE("26/08/2017", '%d/%m/%Y')

Then you can convert it, for example to '%Y-%m-%d' format:
DATE_FORMAT(STR_TO_DATE("26/08/2017", '%d/%m/%Y'), '%Y-%m-%d')

Comment: @vladatr Thanks, I changed how I store date in database to Y-m-d  and it worked.
Answer: I think you can try out this query :
SELECT Journal_T.GL_ID as Akaunti,coa_t.gl_name_vc,sum(Amount_NU) as YTD
FROM Journal_T
LEFT JOIN coa_t on journal_t.gl_id = coa_t.gl_id
WHERE DATE_FORMAT(date_dt,'%d-%m-%Y') BETWEEN '25/08/2017' AND '25/08/2017'
AND bs_category_vc='Rev'
GROUP by coa_t.gl_name_vc,Journal_T.GL_ID
Comment: this didn't work for me


Clarifying notation for the Kullback-Leibler divergence in terms of expectations

Question: We know that for discrete variables
\begin{equation}
D(p(x),q(x))=\mathbb{E}_{p}\left(\log\frac{p(x)}{q(x)}\right)
\end{equation}
where $p(x)$ and $q(x)$ are probability mass functions. Can this be extended to
\begin{equation}
D(p(x,y,z),q(x,y,z))=\mathbb{E}_{p}\left(\log\frac{p(x,y,z)}{q(x,y,z)}\right)
\end{equation}
?
where now $p(x,y,z)$ and $q(x,y,z)$ are joint probability mass functions. Is the notation correct?
Comment: Yes, that's correct. The way you've written KL divergence using expectations is valid for any pair of distributions defined on the same space (not limited to discrete values; also works for reals, vectors, etc.). Notation looks fine to me, as long as you specify that $D(\cdot, \cdot)$ means KL divergence. Everyone writes things a little bit differently.
Comment: Yes, [and see here](https://stats.stackexchange.com/questions/188903/intuition-on-the-kullback-leibler-kl-divergence/189758#189758)  for an explanation and interpretation of that expectation.
Comment: @user20160: do you want to post your comment(s) as an answer?
[Better to have a short answer than no answer at
all.](https://stats.meta.stackexchange.com/a/5326/)
Anyone who has a better answer can post it.
Answer:
Yes, that's correct. The way you've written KL divergence using expectations is valid for any pair of distributions defined on the same space (not limited to discrete values; also works for reals, vectors, etc.). Notation looks fine to me, as long as you specify that (⋅,⋅) means KL divergence. Everyone writes things a little bit differently. – user20160

I've created this answer from a comment so that this question is answered. Better to have a short answer than no answer at
all.


Find all positive integers $n$ such that $\phi(n)=10$

Question: Find all positive integers n such that $\phi(n)=10$.
I have found 2 positive integers: $11$ and $22$.
Is there any more?
Thanks!
Comment: I know what you mean by ϕ, but some people here don't. It'd be best to define it in the question
Comment: https://math.stackexchange.com/questions/1507449/number-theory-find-all-solutions-of-phin-16-and-phin-24  https://math.stackexchange.com/questions/101566/using-eulers-totient-function-how-do-i-find-all-values-n-such-that-phin-12  https://math.stackexchange.com/questions/127998/find-all-positive-integers-n-such-that-phin-6
Answer: Write $n=p_1^{a_1}p_2^{a_2}\dots p_i^{a_i}$, where $p_1,p_2,\dots,p_i$ are distinct primes.
$\phi(n)=\phi(p_1^{a_1}p_2^{a_2}\dots p_i^{a_i})=(p_1^{a_1}-p_1^{a_1-1})(p_2^{a_2}-p_2^{a_2-1})\dots(p_i^{a_i}-p_i^{a_i-1})=10$
if, say, $p_1^{a_1}-p_1^{a_1-1}=1$, then $p_1=2$ and $a_1=1$. If $p_1^{a_1}-p_1^{a_1-1}=10$, what can you say about $p_1$ and $a_1$?
Now, $10=2\times5$ is the only nontrivial factorization of $10$.
So in this case either $n=p_1^{a_1}p_2^{a_2}p_3^{a_3}$ where $p_1^{a_1}-p_1^{a_1-1}=1, p_2^{a_2}-p_2^{a_2-1}=2, p_3^{a_3}-p_3^{a_3-1}=5$, or $n=p_1^{a_1}p_2^{a_2}$ where $p_1^{a_1}-p_1^{a_1-1}=2, p_2^{a_2}-p_2^{a_2-1}=5$. There is no such $n$ which satisfies either of these.
Conclude that $n=11,22$ are the only solutions.
Answer: Hint: Let $p$ be a prime.

If $p$ divides $n$, then $p-1$ divides $\phi(n)$.
If $p^2$ divides $n$, then $p$ divides $\phi(n)$.

You can also use that
$\phi(n) \geq \frac{\sqrt{n}}{\sqrt{2}}$
(see this question)
and so limit a brute force search to $n\le 200$.


On the uniqueness of the solutions for $ y=x \frac{d y}{d x}-\left(\frac{d y}{d x}\right)^2 $

Question: I was solving a multiple select question:
The differential equation
$$
y=x \frac{d y}{d x}-\left(\frac{d y}{d x}\right)^2,
$$
has more than one solutions passing through the point
(a) $(0,1)$,
(b) $(1,1)$,
(c) $(2,1)$,
(d) $(2,-1)$.
My attempts:
i. I got $$y'=f(x,y)=\frac{x \pm \sqrt{x^2-4y}}{2}~~~~~~~~~~~~~~~~~~~(*),$$while I couldn't proceed further,
ii. Any thing can be deduced from the fact: 'For the initial data $(2,1)$, $y'(2)$ is unique and for the data $(2,-1)$, $y'(2)$ is not unique' ?
iii. For the sake of Picard's theorem, we have $$|f(x,y_1)-f(x,y_2)|=\frac{2|y_1-y_2|}{\sqrt{x^2-4y_1}+\sqrt{x^2-4y_2}},$$
and thereby, there is a unique solution for the ODE $(*)$ with initial data $(2,-1)$ valid in some neighbourhood of $x=2$. But still we have two distinct $f(x,y)$'s, right?
How to deal such conditions? Thanks in advance.
Comment: $$y=xy'-(y')^2\\
-y=(y')^2-xy'\\
-\left(y-\frac{x^2}{4}\right)=(y')^2-xy'+\frac{x^2}{4}\\
-\left(y-\frac{x^2}{4}\right)=\left(y'-\frac{x}{2}\right)^2\\
u=-\left(y-\frac{x^2}{4}\right)\\
u=(u')^2\\
2x+C=\int\frac{\pm du}{2\sqrt u}\\
\left(2x+C\right)^2=u\\
y=\frac{x^2}{4}-\left(2x+C\right)^2\\
y=-\left(\frac{3x}{2}+C\right)\left(3x+C\right)\\
y=-\left(\frac{9}{2}x^2+\frac{9C}{2}x+C^2\right)\\
C=\frac{-9x/2\pm\sqrt{(9x/2)^2-4(9x^2/2+y)}}{2}$$
Using this, we can check $(c)$ and $(d)$ are the only correct answers.
Answer: Maybe we can try to solve the equation. Let $p=\frac{dy}{dx}$. Differentiate once and we obtain $(x-2p)\frac{dp}{dx}=0$.
The first case is $x=2p$. We can get $y=\frac14x^2$. The second case is $p=C$. We can get $y=Cx-C^2$.
For (c)(d), the formulae give multiple solutions. For (a)(b), we put the initial data in and then see that there is no real solution for $\frac{dy}{dx}$ at this point.
Comment: This doesn't quite answer the question. They're looking for theoretical ways to determine when solutions exist uniquely. Moreover you are not given that the solution is second differentiable so, without further justification, you can't claim to cover all solutions by taking $dp/dx$. And it needs justification to assume one of the two factors is *always* zero in $(x-2p)p'=0$.
Comment: @FShrike We need not find all the solutions. For (c)(d), we can work out two solutions by solving it. For (a)(b), we can prove that there is no solution. Also, general theory for uniqueness of solution is a hard problem.


php_mod and script execution time

Question: I have a script with a long execution time.
Apache is using php_mod.
I start the process in the browser ( open the url ).
I close the browser and the process is still running.
How can I find the apache process and stop it (linux os )
Answer:
How can I find the apache process and stop it (linux os )

If you know you ip, find apache process PID:
netstat -anp | grep 10.1.1.1 | grep \:80
tcp        0      0 127.0.0.1:80            10.1.1.1:48125         ESTABLISHED 7191/apache2

And kill it:
kill 7191

Comment: Are you sure? It's not working...
Comment: It works.Perfect.

php_mod and script execution time


Set value to GWT checkbox

Question: I want to set value (number or string) to checkbox. This code
final CheckBox checkBox = new CheckBox("Some label");
checkBox.getElement().setAttribute("value", i.toString());
checkBox.getElement().getStyle().setProperty("color", colorList.get(i));
checkBox.addClickHandler(new ClickHandler() {
        @Override
        public void onClick(ClickEvent event) {
            Object sender = event.getSource();
            if (sender == checkBox) {
                CheckBox checkBox = (CheckBox)event.getSource();
                Window.alert(checkBox.getFormValue());
            }
        }
});

creates  the following HTML:

<input id="gwt-uid-4" type="checkbox" value="on" tabindex="0">
Some label

The attribute value is set to span instead of input. The Window.alert(checkBox.getFormValue()) shows message with string 'on', instead of '3'.
Comment: What do you mean by setting a value (other than on/off) to a checkbox? Do you want a label next to the checkbox?
Comment: User can see checkbox with label 'Some label'. But when the checkbox is checked and the form is submitted, some other value is sent which represents the checkbox e.g. '3', but no 'Some label'.
Comment: Are you using a FormPanel?
Answer: The property "value= on / off" has some predefined meaning for the check box. So don't use the property "value" to store numbers.
If you really want to store the numbers use your own custom properties like -
checkBox.getElement().getFirstChildElement().setAttribute( "customProperty", "3" );

and to access the property use -
checkBox.getElement().getFirstChildElement().getAttribute( "customProperty" );

Answer: Set the value for checkBox to "someValue" using setFormValue
  checkBox.setFormValue("someValue");

Comment: :). I think there is no changes in Check box doc.Anyways thanks for the correction and update.

Set value to GWT checkbox


Stream reduce operation using interface

Question: I have the following structure
public interface ICommon{
   ICommon add(ICommon other);
}

public class Foo implements ICommon{
    ...

    ICommon add(ICommon other){
        return new Bar().add(other);
    }

}

public class Bar implements ICommon{
    ...

    ICommon add(ICommon other){
        ...
    }

}

As part of a composite pattern.
I wanted to use the streams reduce operation, but somehow I can not force the type inference to the interface. I am using this.
List<Foo> list;
list.stream().reduce( new Foo(), (a,b) -> a.add(b));

I am getting an error that ICommon can not be cast to Foo.
I have tried to force cast on parameters but with no success.
Answer: The problem here is that your List is parametrized with Foo, but your reduction operations will be parametrized with ICommon due to the add invocation.
While all Foos are ICommons, not all ICommons will be Foos.
The easiest way for you would be to parametrize your List with ICommon instead, without changing anything else in your (visible) code.
Something like:
List<ICommon> list = [some list of Foos];
list.stream().reduce( new Foo(), (a,b) -> a.add(b));

Comment: The list was parametrized with `Foo` since I was creating a stream of `Foo`. I just listed it as this to provide a minimum example. Thank you. This works.
Answer: Found the solution to the problem. The reduce operation has the signature
T reduce(T identity, BinaryOperator<T> accumulator);

All I had to do is cast to a stream of ICommon
list.stream().map(x-> (ICommon) x).reduce( new Foo(), (a,b) -> a.add(b));

Comment: No need to do a cast, `list.stream().reduce(new Foo(), ICommon::add, ICommon::add);`…
Answer: There is the three-arg version of reduce allowing to change the element type when performing the reduction:
list.stream().reduce(new Foo(), ICommon::add, ICommon::add);

While a reference to the same method is used here, the second argument is a function (BiFunction) with an (ICommon,Foo) -> ICommon signature, whilst the third argument is a function (BinaryOperator) with an (ICommon,ICommon) -> ICommon signature.
Another option is to do a type safe change of the existing List's type:
Collections.<ICommon>unmodifiableList(list).stream().reduce(new Foo(), ICommon::add);

Since immutable lists can guaranty to return values of the super type of the actual element type, while preventing the inserting of new elements, this wrapper allows to change the element type to a super type. Further, since stream operations are read-only operations, the wrapper redirects the stream() invocation to the original list, just returning it as a stream of the supertype. So there is no performance difference to using list.stream() directly.
Comment: This is actually very helpful. Is there a difference for parallel streams between the two versions of `reduce`?
Comment: No practical difference. For the three-arg version, the implementation will typically use the first function for sequential partial evaluation followed by using the second function for merging these results. Since these functions should be semantically equivalent (here, it's a reference to the same method), it makes no difference. The two-arg version does exactly the same, just using the provided function for both, sequential partial evaluation and merging. Likely by calling into exactly the same implementation method internally.
Comment: The second option is the first time I see it. I did not even know it is possible (first time I saw the generic type being passed like that). Do you have a reference to `Collections.unmodifiableList(list)`
Comment: This is just an invocation with an explicit type, as shown [in the tutorial](https://docs.oracle.com/javase/tutorial/java/generics/methods.html) (compare the two variants at the end). The method itself is just [`Collections.unmodifiableList(…)`](https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-). It also works with implicit types, e.g. in an assignment: `List newList = Collections.unmodifiableList(list);`
Comment: I always used the implicit assignment. Thank you!

Stream reduce operation using interface


Ring homomorphism is injective but not surjective

Question: Let $F$ be a field, let $R_1=F[x]$ be the ring of polynomials with coefficients in $F$, and let $R_2$ be the ring of all functions from $F$ to itself, with addition and multiplication defined as the usual operations on functions with values in a ring. The function
$$\phi: R_1 \rightarrow R_2$$
which send the polynomial $f\in F[x]$ to the $F$-valued function $a \rightarrow f(a)$ on $F$ which induces, is a homomorphism.
When $F=\mathbb Q$ or $\mathbb R$, show that the homomorphism $\phi$ is injective but not surjective.

I would start like so
if $\phi(a) = b$ and $\phi(a') = b$, then $a=a'$. Since $a,a' \in F[x]$...
Comment: Note that you will need the fact that $\Bbb Q$ and $\Bbb R$ have characteristic $0$ to show that the map is injective: if $F = \Bbb Z/p\Bbb Z$, then $x^p - x$ is a nonzero polynomial, but $f(a) = a^p - a = a - a = 0$ for all $a\in F$, so $\phi(0) = \phi(x^p - x)$, which means $\phi$ is not injective in this case.
Comment: @Stahl Actually it is sufficient to suppose that the field $F$ is infinite.
Answer: Every function in the image of your map is (or extends to) a continuous function $\mathbb R\to\mathbb R$. Yet there are functions $F\to F$ which are not continuous (or, when $F=\mathbb Q$, do not extend to continuous functions).
This means that the map is not surjective.
Alternatively, every function in the image of your maps is identically zero if it has infinitely many zeroes, yet there exist non-zero functions $F\to F$ which have infinitely many zeroes. This again implies non-surjectivity, and if you look at it correctly, also injectivity.


Automatically drop FK's to re-create a table and then automatically put the FK's back

Question: I have a number of incidents where SSMS tells me table recreation is required, even for something simple like making a column nullable.  If this table is referenced by FK's, I would ideally like to find a way of temporarily removing those FK's from the 'other' tables, re-creating my table, and then re-creating the FK's on the other table again.
Is there a known way of doing this?
Answer: Disabling won't work in this case.
I'd probably do this:

use the SSMS GUI to make my changes

save the script: do not execute it
run this section by section
use a compare tool to generate a better script (eg Red Gate)

Step 4 is optional and you pimp the script from step 2/3 instead


Regex: extract first and last child directories from URL

Question: I need a regular expression that matches two virtual folders; the first and the last in a given URL:
www.example.com/first_folder/some/additional/bunch/of/folders/last_folder/
so far I have this regex: <code>(?<!/)/[^/?#]+</code> but it matches all the folders :(
I need this for a rewrite rule to use in WordPress. A Quick and dirty example for rewriting http://mysite/project/1 into http://mysite/index.php?pagename=project&id=1:
<code>$newrules = array();
$newrules['(project)/(\d*)$'] = 'index.php?pagename=$matches[1]&id=$matches[2]';
</code>
Answer: Quick and dirty solution i.e. not thoroughly tested with the billion possible combinations of urls etc. so take it with a grain of salt..
<code>.*?/(\w+)/.*?/(\w+)/$
</code>
Input :

www.example.com/first_folder/some/additional/bunch/of/folders/last_folder/

Output :
<code>$1 prints first_folder
$2 prints last_folder
</code>
Comment: @numediaweb No prob. Just be careful becasue regexes are fragile beasts :)


Find $ \lim_{x\rightarrow 0}\frac{\tan^2 x+2x}{x+x^2} $ without L'Hôpital's Rule

Question: I need to find $$\lim_{x\to 0}\frac{\tan^2 x+2x}{x+x^2}$$
This is what I did:
Let $f(x)=\frac{\tan^2(x)+2x}{x+x^2}=\frac{\frac{\sin^2(x)}{\cos^2(x)}+2x}{x+x^2}=\frac{\sin^2(x)+2x\cos^2(x)}{x(x+1)\cos^2(x)}=\frac{\sin^2(x)}{x(x+1)\cos^2(x)}+\frac{2}{x+1}=\frac{\sin (x)}{x}\cdot\frac{\sin(x)}{(x+1)\cos^2(x)}+\frac{2}{x+1}$
$\lim_{x\to0}\left(\frac{\sin(x)}{x}\cdot\frac{\sin(x)}{(x+1)\cos^2(x)}+\frac{2}{x+1}\right)=\underbrace{\left(\lim_{x\to0}\frac{\sin(x)}{x}\right)}_{=1}\cdot\left(\lim_{x\to0}\frac{\sin(x)}{(x+1)\cos^2(x)}\right)+\underbrace{\lim_{x\to0}\left(\frac{2}{x+1}\right)}_{=2}=\lim_{x\to0}\frac{\sin(x)}{(x+1)\cos^2(x)}+2=\frac{\lim_{x\to0}\sin(x)}{\underbrace{\left(\lim_{x\to0}x+1\right)}_{=1}\cdot\underbrace{\left(\lim_{x\to0}\cos^2(x)\right)}_{=1^2=1}}+2=\lim_{x\to0}\sin(x)+2=0+2=2$
Is this correct? I feel like I have assumed the some limits rather than proving it.
We are allowed to use the common result $\frac{\sin x}{x}$ is $1$ as $x$ tends to $0$.
Comment: To me, your result is correct. What limits have you assumed?
Comment: like the limit of sinx is 0 as x tends to 0. are we allowed to just assume that?
Comment: @snowman [here](http://meta.math.stackexchange.com/questions/5020/mathjax-basic-tutorial-and-quick-reference) is a quick $\LaTeX$ tutorial, it is worth to take a look.
Comment: If you are allowed to assume $\lim_{x\to 0} \frac{\sin x}{x}=1,$ what other value can have $\lim_{x\to 0} \sin x?$
Comment: im not sure... nothing besides zero?
Comment: very nice computations and made in $\LaTeX$
Comment: I edited the post and added Latex @Dr.SonnhardGraubner ! ;-)
Comment: congratulations it looks very nice really
Answer: $$\frac{\tan^2 x+2x}{x+x^2}$$ is equivalent to$$\frac{\tan(x)\frac{\tan(x)}{x}+2}{1+x}$$


Gaussian Elimination with Parameters

Question: Can someone please help me with the solution of
\begin{align}(\lambda+1)x+y&=\lambda+1\\
x+(\lambda+1)y&=1\\
x+y&=2\lambda+1\end{align}
Answer: from the first equation we get
$$y=\lambda+1-(\lambda+1)x$$
plugging this in the second equation we have
$$x(-\lambda^2-2\lambda)=-\lambda^2-2\lambda$$
if $$-\lambda^2-2\lambda=0$$
we get $$\lambda=0$$ of $$\lambda=-2$$
in the first case we get infinity many Solutions.
for the other case we get no Solutions.
If $$\lambda\ne 0$$ or $$\lambda\ne -2$$
from here we get $$x=1,y=0$$ plugging this in the last equation we get
$$1=\lambda+1$$ thus it must be $$\lambda=0$$ in the other case there are no Solutions.
Comment: $x=y=1$ cannot be a solution.
Comment: you must read my whole post
Comment: ok i have corrected it, thank you
Answer: Subtract the third from the second,
$$\lambda y=-2\lambda.$$
This is possible if either

$\lambda=0$, then all three equations reduce to $x+y=1$;
$y=-2$, giving

$$\begin{cases}(\lambda+1)x=\lambda+3,\\x=2\lambda+3\end{cases}$$ which requires
$$(\lambda+1)(2\lambda+3)=\lambda+3.$$
To summarize
$$\lambda=0\to (t,1-t),$$
$$\lambda=-2\to (-1,-2).$$

More systematic:
The system has a solution when the determinant
$$\begin{vmatrix}\lambda+1&1&\lambda+1\\1&\lambda+1&1\\1&1&2\lambda+1\end{vmatrix}=2l^3+4l^2$$ vanishes.
Then

$\lambda=0\to$

$$\begin{cases}x+y&=1\\
x+y&=1\\
x+y&=1,\end{cases}$$
giving $y=1-x$,

$\lambda=-2\to$

$$\begin{cases}-x+y&=-1\\
x-y&=1\\
x+y&=-3\end{cases},$$
giving $x=-1,y=-2$.
Comment: The condition $\det(a_1, a_2, b) = 0$ seems correct to enforce $0 = z = \det(a_1,a_2,b) / \det(a_1, a_2, a_3)$ in an extended system $(a_1, a_2, a_3) (x,y,z)^T = b$, using Cramer's rule, keeping the same solution set as $(a_1, a_2) (x,y)^T=b$, for some linear independent $a_3$.  I wonder if there is an easier justification.
Comment: @mvw: make the system homogeneous. It has a nontrivial solution only when the determinant is zero.
Answer: If we sum the first and the second we get
$$(\lambda+2)x+(\lambda+2)y=\lambda+2$$
If $\lambda\neq -2$ we can divide by $\lambda+2$ to get
$$x+y=1$$
We also have
$$x+y=2\lambda+1$$
So $\lambda=0$ and the system has infinitely many solutions which you can parametrize.
If $\lambda=-2$ we get $-x+y=-1$ and $x+y=-3$ adding them together we get $y=-2$ and $x=-1$.
Answer: You can write that system more compact as
$$
A u = b \iff \\
[A | b] =
\left[
\begin{array}{cc|c}
\lambda + 1 & 1 & \lambda + 1 \\
1 & \lambda + 1 & 1 \\
1 & 1 & 2\lambda + 1
\end{array}
\right]
\to
\left[
\begin{array}{cc|c}
1 & 1 & 2\lambda + 1 \\
\lambda & 0 & -\lambda \\
0 & \lambda & -2 \lambda
\end{array}
\right]
$$
At this point we need to distinguish further between $\lambda = 0$ and $\lambda \ne 0$.
Case $\lambda = 0$:
We proceed with the system
$$
\left[
\begin{array}{cc|c}
1 & 1 & 1 \\
0 & 0 & 0 \\
0 & 0 & 0
\end{array}
\right]
$$
which has the line $L = \{ (x, y) \mid x + y = 1 \}$ as solution.
Case $\lambda \ne 0$:
We continue with the system
$$
\left[
\begin{array}{cc|c}
1 & 1 & 2\lambda + 1 \\
\lambda & 0 & -\lambda \\
0 & \lambda & -2 \lambda
\end{array}
\right]
$$
We are allowed to divide by $\lambda$ and get
$$
\left[
\begin{array}{cc|c}
1 & 1 & 2\lambda + 1 \\
1 & 0 & -1 \\
0 & 1 & -2
\end{array}
\right]
\to
\left[
\begin{array}{cc|c}
1 & 0 & -1 \\
0 & 1 & -2 \\
1 & 1 & 2\lambda + 1
\end{array}
\right]
\to
\left[
\begin{array}{cc|c}
1 & 0 & -1 \\
0 & 1 & -2 \\
0 & 0 & 2\lambda + 4
\end{array}
\right]
$$
The equation of the last row can only be solved if $2\lambda + 4 = 0 \iff \lambda = -2$ leading to the solution $(x,y) = (-1,-2)$.
Summary
We found a single solution $(-1,-2)$ if $\lambda = -2$, a line of solutions $L = \{ (x,y) \mid x + y = 1 \}$ for $\lambda = 0$, for other values of $\lambda$ the system has no solutions.
Comment: There is no reason to consider the case $\lambda=-1$ specifically.
Comment: Thanks. I found it interesting, but it is not required. I shortened it.

Gaussian Elimination with Parameters


I think these groups don't exist

Question: A group $G$ is said to be decomposable if $G= A \times B$ (direct product). I am looking for an example of an indecomposable group (non-abelian) with a very large centre relative to the order of the group. Are there groups (non-abelian) $G$ such that $G$ is indecomposable and $Z(G) = c|G|$ where $c$ is very small relative to $|G|$?
Comment: What is $c$ here? Obviously it cannot be greater then $1$. On the other hand if it is arbitrary then this is trivially true: every group has such constant, namely $|Z(G)|/|G|$.
Comment: Like $1/|G|$? Every simple group has trivial center and is indecomposable. You can't have smaller center.
Comment: @ freakish  but i want the opposite, group with very large center and indecomposable
Comment: So you meant $c$ as big as possible in $(0,1)$ range.
Comment: @ freakish  yes. or in other words a indecomposable group with largest possible center.
Comment: So how big do you want it to be (note that $c<1/2$ for sure)? You have to put some additional assumptions on $c$. As it is the statement is trivially satisfied by any group.
Answer: It is well-known that $G/Z(G)$ cannot be nontrivial and    cyclic, so we must have $|G/Z(G)| \ge 4$.
For all $n\ge 2$, the group of order $2^{n+1}$ defined by the presentation
$$\langle x,y \mid x^{2^n}=y^2=1,y^{-1}xy=x^{2^{n-1}+1} \rangle$$
has centre $\langle x^2 \rangle$ of index $4$, and it is easily shown to be indecomposable. So there are arbitrarily large finite groups with $|G/Z(G)|=4$.
These groups are sometimes called modular groups. They are one of the four families of nonabelian $2$-groups that have a maximal cyclic subgroup, which are discussed here.
Comment: Thanks for the answer. Please name the group you have given above in the answer. Are there well known groups which satisfy requirements I have mentioned in my question?
Comment: See my edit. I think you could get further examples by allowing $y$ to have order a larger power of $2$, but apart from that I do not know of any other indecomposable groups with $|G/Z(G)|=4$.

I think these groups don't exist


cannot invoke bootstrap plugin from javaScript

Question: I am trying to invoke bootstrap plugin for my webpage.
This is the plugin I am talking about:
Select Picker
Select Picker 2
When I invoke the plugin from my html file like this:

    <select class="selectpicker show-tick" id="tag" ata-style="btn-primary">
        <option>stocks</option>
        <option>bank</option>
        <option>card</option>
    </select>

The output is fine; the select boxes come with all CSS loaded as showed in the links above.
However, when I create the boxes dynamically from a JS file, the select boxes come up as plain Select boxes with no effects.
var selectElement1_act = document.createElement("select");
selectElement1_act.setAttribute("class", "selectpicker");

Any help would be appreciated.
Answer: This is because you're invoking the function on the window load event, and when elements are created dynamically, they do not necessarily exist in the DOM at that point.
Since you're using jQuery, you'd be better to utilise the following code in your JS file instead:
$(function() {
    var selectPicker = $('<select class="selectpicker" />');
    selectPicker.appendTo('body');
    selectPicker.selectpicker('hide');
});

Comment: Can you put together a demo page, please?


Banco de Dados Mysql (Insert Into e ordem correta)

Question: minha dúvida pode parecer boba, mas é algo que eu sempre me perco, como saber a ordem correta do insert into (pra inserir dados na tabela) no banco de dados ? me falaram que é pelas chaves estrangeiras, mas eu não entendi a lógica

as insert ficaram nessa ordem:
posto
 tripulação
missao
funcao
status
tipoaeronave
aeronave
escala
Comment: Você tem alguma estrutura de tabelas para colocar junto à pergunta?
Comment: Pior que não, é que eu tinha essa dúvida, dai eu ia colocando as insert e testando qual dava certo
Comment: Então você tem um exemplo?
Comment: eu tenho um banco aqui, mas é muito grande, como vcs querem que eu envie ?
Comment: Tire uma foto ou print do modelo relacional.
Comment: Monta um exemplo básico aqui http://sqlfiddle.com/
Comment: editei o post com um exemplo
Answer: A ordem para você inserir os dados na Tabela seria primeiro pelas tabelas que serão estrangeiras...
Por exemplo, temos a tabela TIMES e a tabela JOGADORES:
CREATE TABLE TIMES
(
    COD_TIME NUMBER(3) NOT NULL,
    NOME VARCHAR2(100) NOT NULL,

    CONSTRAINT PK_TIMES PRIMARY KEY (COD_TIME)
);

CREATE TABLE JOGADORES
(
    COD_JOGADOR NUMBER(3) NOT NULL,
    NOME VARCHAR2(100) NOT NULL,
    COD_TIME NUMBER(3) NOT NULL,

    CONSTRAINT PK_JOGADORES PRIMARY KEY (COD_JOGADOR),
    CONSTRAINT FK_TIMES_JOGADORES FOREIGN KEY (COD_TIME) REFERENCES TIMES
);

No nosso exemplo, cada jogador tem um time: Se começarmos a popular as tabelas pela tabela JOGADORES, não haverá nenhum time, porque ainda não adicionamos nenhum registro na tabela TIMES.
Na imagem, eu tentei adicionar um JOGADORcom o TIMEde CODIGO = 1, porém me deu um erro, que acontece por não existir um CODIGO = 1na tabela TIMES, ou seja, é como se eu estivesse dizendo que um jogador faz parte de um time que não existe ainda.

A partir do momento que eu criei o TIME, dando um INSERTna tabela TIMEScom um registro de COD_TIME = 1, ai sim podemos adicionar o JOGADOR.

Ou seja, respondendo a sua pergunta, é preciso iniciar a inserção de registro pelas tabelas que não tem um FOREIGN KEYligadas a elas, a não ser que na tabela estrangeira já possua registros.
No seu caso, na tabela AERONAVE, não seria possível adicionar uma AERONAVEsem antes adicionar registro em STATUS e em TIPO_AERONAVE(A não ser que essas colunas aceitem NULL).
Comment: Agora eu entendi, muito obrigado Richard Willian!
Comment: De nada, precisar estamos ai0/


Why does padding affect my results?

Question: I'm learning deep learning, and have been looking at a tutorial for autoencoders using MNIST. It's quite straightforward, and I think I understand it quite well so far. This is the code from the tutorial:
import numpy as np
import matplotlib.pyplot as plt
from keras.datasets import mnist
from keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D
from keras.models import Model
from keras import backend as K
#Load Dataset
(x_train, _), (x_test, _) = mnist.load_data()
#Scale Dataset values to lie between 0 and 1
x_train = x_train.astype('float32') / 255.

x_test = x_test.astype('float32') / 255.
x_train = np.reshape(x_train, (len(x_train), 28, 28, 1))
x_test = np.reshape(x_test, (len(x_test), 28, 28, 1))
#Add Noise to our MNNIST Dataset by sampling random values from Gaussian distribution by using np.random.normal() and adding it to our original images to change pixel values
noise_factor = 0.5
x_train_noisy = x_train + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train.shape)
x_test_noisy = x_test + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test.shape)
x_train_noisy = np.clip(x_train_noisy, 0., 1.)
x_test_noisy = np.clip(x_test_noisy, 0., 1.)

# Model Construction
input_img = Input(shape=(28, 28, 1))

x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# At this point the representation is (7, 7, 32)

x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder = Model(input_img, decoded)
autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

autoencoder.summary()

This builds the following network:
Model: "model_3"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_3 (InputLayer)         (None, 28, 28, 1)         0
_________________________________________________________________
conv2d_11 (Conv2D)           (None, 28, 28, 32)        320
_________________________________________________________________
max_pooling2d_5 (MaxPooling2 (None, 14, 14, 32)        0
_________________________________________________________________
conv2d_12 (Conv2D)           (None, 14, 14, 32)        9248
_________________________________________________________________
max_pooling2d_6 (MaxPooling2 (None, 7, 7, 32)          0
_________________________________________________________________
conv2d_13 (Conv2D)           (None, 7, 7, 32)          9248
_________________________________________________________________
up_sampling2d_5 (UpSampling2 (None, 14, 14, 32)        0
_________________________________________________________________
conv2d_14 (Conv2D)           (None, 14, 14, 32)        9248
_________________________________________________________________
up_sampling2d_6 (UpSampling2 (None, 28, 28, 32)        0
_________________________________________________________________
conv2d_15 (Conv2D)           (None, 28, 28, 1)         289
=================================================================
Total params: 28,353
Trainable params: 28,353
Non-trainable params: 0

So far, so good. I can train it with
autoencoder.fit(x_train_noisy, x_train,
    epochs=10,
    batch_size=128,
    shuffle=True
)

and it converges to a solution with a loss of approx. 0.099 (and roughly the same on the test set).
But now I tried to pad my input images. I did that in order to simplify building deeper autoencoders, but that's not the point here, the point is that even by just padding the images slightly, and keeping everything else constant, the results changed quite a bit:
x_train_padded = np.array([np.pad(x, ((2, 2), (2, 2), (0, 0)), mode='edge') for x in x_train])
x_test_padded = np.array([np.pad(x, ((2, 2), (2, 2), (0, 0)), mode='edge') for x in x_test])

x_train_padded_noisy = x_train_padded + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_train_padded.shape)
x_test_padded_noisy = x_test_padded + noise_factor * np.random.normal(loc=0.0, scale=1.0, size=x_test_padded.shape)
x_train_padded_noisy = np.clip(x_train_padded_noisy, 0., 1.)
x_test_padded_noisy = np.clip(x_test_padded_noisy, 0., 1.)

# Model Construction
input_img_padded = Input(shape=(32, 32, 1))

x = Conv2D(32, (3, 3), activation='relu', padding='same')(input_img_padded)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)

# At this point the representation is (8, 8, 32)

x = Conv2D(32, (3, 3), activation='relu', padding='same')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = UpSampling2D((2, 2))(x)
decoded = Conv2D(1, (3, 3), activation='sigmoid', padding='same')(x)
autoencoder_padded = Model(input_img_padded, decoded)
autoencoder_padded.compile(optimizer='adam', loss='binary_crossentropy')

autoencoder_padded.summary()

As you can see, the network is essentially the same as before (same convolution layers, same total number of parameters):
Model: "model_4"
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input_4 (InputLayer)         (None, 32, 32, 1)         0
_________________________________________________________________
conv2d_16 (Conv2D)           (None, 32, 32, 32)        320
_________________________________________________________________
max_pooling2d_7 (MaxPooling2 (None, 16, 16, 32)        0
_________________________________________________________________
conv2d_17 (Conv2D)           (None, 16, 16, 32)        9248
_________________________________________________________________
max_pooling2d_8 (MaxPooling2 (None, 8, 8, 32)          0
_________________________________________________________________
conv2d_18 (Conv2D)           (None, 8, 8, 32)          9248
_________________________________________________________________
up_sampling2d_7 (UpSampling2 (None, 16, 16, 32)        0
_________________________________________________________________
conv2d_19 (Conv2D)           (None, 16, 16, 32)        9248
_________________________________________________________________
up_sampling2d_8 (UpSampling2 (None, 32, 32, 32)        0
_________________________________________________________________
conv2d_20 (Conv2D)           (None, 32, 32, 1)         289
=================================================================
Total params: 28,353
Trainable params: 28,353
Non-trainable params: 0
_________________________________________________________________

However, when I train this network, using
autoencoder_padded.fit(x_train_padded_noisy, x_train_padded,
    epochs=10,
    batch_size=128,
    shuffle=True
)

it achieves a loss of about 0.075, almost 25% less than before. Why is this? The only change was a couple of additional pixles around the edge of the image. Of course I'm happy for the improvement, but it really confuses me, and I'd like to understand why this happens.
By the way, I repeated the training several times, just in case there was some random effect that gave rise to the difference. But I got approx. the same results every time.
Answer: Actually, I think I figured it out. When the images are padded, the black area (the background) becomes larger. This area should be relatively easy for the autoencoder to learn how to recreate. Therefore, the average loss per pixel decreases, since the number of pixels increases but the difficulty does not increase.
This is also confirmed by noting that the total loss (loss x number of pixels) on the original images was 0.099*28* 28 = 77.6, while the total loss on the padded images was 0.075*32*32 = 76.8, so even though one loss value was larger than the other, the actual loss was more or less the same.
In other words, padding did not improve the autoencoder, but it modified the loss function, making the result at first seem better with padding than without.

Why does padding affect my results?