Cursor index out of bounds Exception, Android database

Question: I am doing an android app. I am trying to get the rows of the database a view them in a text view. At the begining I want to take the first row of the database. Then I use the gyroscope in order to slide the quotes in the screen. But I get this error. Can anyone help me please? Thanks a lot.
This is SlideShow class
package com.example.prova1;

import java.util.HashMap;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class SlideQuote extends Activity implements SensorEventListener
{
 //a TextView
 private TextView quote;
 private TextView author;
 //the Sensor Manager
 private SensorManager sManager;
 float x;
 int id;
 int total;
 String s1,s2;
  Database quotedatabase = new Database(this);

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.slide_quote);

        //get the TextView from the layout file
        quote = (TextView) findViewById(R.id.textView3);
        author = (TextView) findViewById(R.id.textView4);
id=1;
        //get a hook to the sensor service

        sManager= (SensorManager) getSystemService(Context.SENSOR_SERVICE);
     if(sManager.getSensorList(Sensor.TYPE_GYROSCOPE).size()!=0){
      Sensor s =sManager.getSensorList(Sensor.TYPE_GYROSCOPE).get(o);
      sManager.registerListener(this,s ,SensorManager.SENSOR_DELAY_NORMAL);
      quotedatabase.getQuote(id, s1, s2);
      quote.setText(s1);
      author.setText(s2);
    }
    }
    //when this Activity starts
    @Override
 protected void onResume()
 {
  super.onResume();
  /*register the sensor listener to listen to the gyroscope sensor, use the
  callbacks defined in this class, and gather the sensor information as quick
  as possible*/
  sManager.registerListener(this, sManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE),SensorManager.SENSOR_DELAY_FASTEST);
 }

  //When this Activity isn't visible anymore
 @Override
 protected void onStop()
 {
  //unregister the sensor listener
  sManager.unregisterListener(this);
  super.onStop();
 }

 @Override
 public void onAccuracyChanged(Sensor arg0, int arg1)
 {
  //Do nothing.
 }

 @Override
 public void onSensorChanged(SensorEvent event)
 {
  //if sensor is unreliable, return void
  if (event.accuracy == SensorManager.SENSOR_STATUS_UNRELIABLE)
  {
   return;
  }
  //else it will output the Roll, Pitch and Yawn values
  x=event.values[2];
  total=quotedatabase.getQuotesCount();

  if(x>25){
   if(id==total)
   {id=1;
   }
   else{

   id++;
   }
      quotedatabase.getQuote(id, s1, s2);
      quote.setText(s1);
      author.setText(s2);

  }
  if(x<-25){
   if(id==1)
   {id=total;}
   else{
   id--;}

      quotedatabase.getQuote(id, s1, s2);
      quote.setText(s1);
      author.setText(s2);
  }
  }
 @Override
 public void onBackPressed() {
  // TODO Auto-generated method stub
  sManager.unregisterListener(this);
  Intent backIntent = new Intent(getApplication(), Quote.class);
       finish();
       startActivity(backIntent);

 }

 }

And this is the database:
package com.example.prova1;

/**This class is for the database that is used to store quotes*/

import java.util.ArrayList;
import java.util.HashMap;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Database extends SQLiteOpenHelper {

 public Database(Context quoteContext){

 super(quoteContext, "quoteDB", null, 1);

 }

 @Override
 //Create database
 public void onCreate(SQLiteDatabase database) {

  String query = "CREATE TABLE quotelist ( quoteId INTEGER PRIMARY KEY, textQuote TEXT ,textAuthor TEXT)";
  String query1 = "INSERT INTO quotelist  VALUES ( '1', 'RAI', 'uku')";

    database.execSQL(query);
    database.execSQL(query1);
 }

 @Override
 public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {

   String query = "DROP TABLE IF EXISTS quotelist";

   database.execSQL(query);
   onCreate(database);
  }

 //Insert quotes into database
public void insertItem(HashMap<String, String> queryValues){

  SQLiteDatabase database = this.getWritableDatabase();

  ContentValues values = new ContentValues();

  values.put("textQuote", queryValues.get("textQuote"));
  values.put("textAuthor", queryValues.get("textAuthor"));

  database.insert("quotelist", null, values);

  database.close();

 }

//Update the quotes into the database

public int updateItem(HashMap<String, String> queryValues) {

 SQLiteDatabase database = this.getWritableDatabase();

 ContentValues values = new ContentValues();

 values.put("textQuote", queryValues.get("textQuote"));
 values.put("textAuthor", queryValues.get("textAuthor"));

 return database.update("quotelist", values, "quoteId" + " = ?", new String[] {queryValues.get("quoteId") });

}

//Delete quotes into the database

public void deleteItem(String id){

 SQLiteDatabase database = this.getWritableDatabase();

 String deleteQuery = "DELETE FROM quotelist WHERE quoteId='" + id + "'";

 database.execSQL(deleteQuery);
 database.close();

}

public ArrayList<HashMap<String, String>> getAllItems(){

 ArrayList<HashMap<String, String>> itemArrayList = new ArrayList<HashMap<String, String>>();

 String selectQuery = "SELECT * FROM quotelist";

 SQLiteDatabase database = this.getWritableDatabase();

 Cursor cursor = database.rawQuery(selectQuery, null);

 if(cursor.moveToFirst()){

  do{

   HashMap<String, String> itemMap = new HashMap<String, String>();

   itemMap.put("quoteId", cursor.getString(o));
   itemMap.put("textQuote", cursor.getString(1));
   itemMap.put("textAuthor", cursor.getString(2));

  itemArrayList.add(itemMap);

  } while(cursor.moveToNext());

 }
 database.close();

 return itemArrayList;

}

public HashMap<String, String> getItemInfo(String id){

 HashMap<String, String> itemMap = new HashMap<String, String>();

 SQLiteDatabase database = this.getReadableDatabase();

 String selectQuery = "SELECT * FROM quotelist WHERE quoteId='" + id + "'";

 Cursor cursor = database.rawQuery(selectQuery, null);

 if(cursor.moveToFirst()){

  do{

   itemMap.put("quoteId", cursor.getString(o));
   itemMap.put("textQuote", cursor.getString(1));
   itemMap.put("textAuthor", cursor.getString(2));

  } while(cursor.moveToNext());

 }

 return itemMap;

}

public void getQuote(int id, String s1,String s2) {
    SQLiteDatabase db = this.getReadableDatabase();

    Cursor cursor = db.query("quotelist", new String[] { "quoteId",
            "textQuote", "textAuthor" }, "quoteId" + "=?",
            new String[] { String.valueOf(id) }, null, null, null, null);
    if (cursor != null)
        cursor.moveToFirst();
s1=cursor.getString(1);
s2=cursor.getString(2);

}
public int getQuotesCount() {
    String countQuery = "SELECT  * FROM  quotelist" ;
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);
    cursor.close();

    // return count
    return cursor.getCount();
}

The error is  atthis line :   s1=cursor.getString(1);  at DatabaseClass
and at line: quotedatabase.getQuote(id, s1, s2); at ShowQuote class
Answer: This error means there is no data in your cursor. Does your database have data in it? Also you should check the result of moveToFirst as a false value will show you if the cursor is empty.
Answer: The error is here: s1=cursor.getString(1); and
s2=cursor.getString(2);
If the string length is 0, you can not get the second character and you will get an outofboundexception.
Also it has no effect to execute this code. In this case you give a string to your methode. This S1 in your method is not the same string as you gave to your method. By giving a variable to a method JAVA creates a copy of this variable.
Comment: If this was true he would get a CursorOutofBound: Index 2 requested, with a size of 2 or something like that, but I agree with the part about it having no effect on the code, though it should be made clear that it creates a copy of a basic type that is not an Object (Strings behave as basic types)


Imputing the mean value from the 'train set' into the 'test set'

Question: I have looked at a couple questions and answers similar to this, the recommendation seems to be the imputation of mean values from the 'training set' into my 'test set'.
However, what I am trying to find out is:
Why would you use the mean value from your 'train set' and not the mean values from your 'test set'?
What is the theory behind this? What would be the effects of using your test mean instead of the right way which is imputing from the train set
Thank you for the help!
Answer: You assume that train and test come from the same distribution, and hence you use the mean of the train. Also generally training data is more which gives a better estimator of the mean of the distribution. Training is mostly done offline with a lot of data and new test examples can use the estimated mean.
If you use the mean from your test set, you are linking the performance of your procedure to the mean of the new data you are evaluating on. You could be testing on 1 example at a time or 10 or hundreds,so using that as a mean would be more close to the one in training if you have a lot more elements in the test set. Also , for example you use income 100-200 in your train set and test set is made by an adversary who tries to make a prediction for 1000-1200 which does not make sense in your current model.If you use the mean from the test set , the valid test points will be affected by these outliers. But as the training data is large, the effect of outliers in the mean will tend to get averaged out
Comment: So could you say that if on your new data set (test data) you have very different values from what the model was trained on it would affect the results accuracy?    So it is based on the assumption that the passed data to the model is mostly similar the one used in the training set. I think this makes sense now. Thank you for the big help!
Comment: What would be the consequences of using the mean from the test set to fill missing values? How could this negatively affect my results? Thank you for the help!
Comment: Hello ! I updated my answer for your question. Please let me know if you any other question
Answer: The purpose of a test set is to get an estimate of how you model will perform in the real world, using data that has played no part in your model building.
It is important to realise that the mean becomes part of your model you want to test.
The reason for this is that once a model is deployed
 1. you do not want to batch process application of the model, which you would have to do to get an updated mean
 2. After mean centring the model describes how the sales deviate from the mean. Changing the mean changes the baseline and therefore renders the model outdated.


Module name and name of the directory the containing it

Question: Google and stack has come up dry so far, so I'll ask you: In Drupal 8, does my module folder need to match the name of my module?  Some basic testing indicates that they can differ with no harm done, but Drupal 8 is still a bit of a black-box to me and I don't want to find myself painted into a corner because of this.
Comment: If you are contributing it out, I would say, yes. If not, you don't. But it will be easier to track over time with the more you create. I tend to namespace my modules with the project for internal use.
Comment: Why do you need to have it named differently?
Answer: Good conventions mentioned by Kevin aside, there's no technical reason for the names to match, no. What's important is the name of the .info.yml file, that's what defines the module machine name.
Drupal just interrogates the known module folder locations recursively looking for .info.yml files, so anything will get picked up regardless of folder name.


Why do Multiple Calls to jQuery UI Position Result in Different Placement?

Question: I have an odd issue when using jQuery UI to position a div.  Calling the function the first time places the div in the expected location.  Subsequent calls place the div further and further to the right and bottom of the window.  Below is the least amount of code I could reproduce the problem with.
<!DOCTYPE html>

    <title>jQuery UI Position Test </title>

a message to the user

        <input id="ShowMessageButton" type="button" value="Show Message" />

If you run the code the first time you get this, which is what I expected.
Screen Shot 1 http://www.michaelware.net/OutsideImages/jQueryPositionIssue/Screen1.png
After the second call
Screen Shot 2 http://www.michaelware.net/OutsideImages/jQueryPositionIssue/Screen2.png
After the third call
Screen Shot 3 http://www.michaelware.net/OutsideImages/jQueryPositionIssue/Screen3.png
Please note you have to let the fadein/fadeout finish or the problem will not occur.
Comment: Here's a JSFiddle with the code in for anyone that wants to try it: http://jsfiddle.net/PaDBk/
Comment: and what you expect on subsequent calls
Answer: Two things. First .bind() has been deprecated in favor of .on(). Second, you need to change the order of your calls to:
$('#ShowMessageButton').on("click", function () {
    $('#message').fadeIn(800).position({
        my: "center top",
        at: "center bottom-12",
        of: "#header"
    }).delay(1000).fadeOut(1000);
});

jsFiddle example
As the docs for position state, "Note: jQuery UI does not support positioning hidden elements". Since you are trying to use position on a hidden element, you need to make it visible first. You can witness this if you don't totally fade the element out in this example using the format of your original code.
Comment: This gives exactly the behavior I want but I don't understand why.  Does the fadeIn not complete before the position?  In the Fiddle you can clearly see the div fading in after its in the correct position over the header and content areas.
Comment: In your original code, the click triggers a call to position on a hidden element every time, which position can't deal with. If the element isn't completely hidden, it works fine.
Answer: Try This -
$('#message').position({ my: "center top", at: "center bottom-12", of: "#header" });
$('#ShowMessageButton').bind("click", function () {
      $('#message').fadeIn(800).delay(1000).fadeOut(1000);
});

Fiddle


Why is tabbed space width not constant

Question: I wrote a sample, to test tabbed space specifier " \t " but the results were quite surprising.
At some places, spaces are short and at some places they are long, and some doesn't even have effect !
Comment: Because it wouldn't be very useful for printing tabular data if it was constant.
Answer: It works as intended.
The original purpose of tab is printing tables. Rather than expanding to exactly N spaces (N being tab width, normally 4 or 8), it sets X coordinate of the cursor to the next multiple of N.
Consider this example:
<code>#include <stdio.h>

int main()
{
    for (int i = 0; i < 20; i++)
    {
        for (int j = 0; j < i; j++)
            putchar('#');
        puts("\t|");
    }
}
</code>
Output for tab width 8:
<code>        |
#       |
##      |
###     |
####    |
#####   |
######  |
####### |
########        |
#########       |
##########      |
###########     |
############    |
#############   |
##############  |
############### |
################        |
#################       |
##################      |
###################     |
</code>
Output for tab width 4:
<code>    |
#   |
##  |
### |
####    |
#####   |
######  |
####### |
########    |
#########   |
##########  |
########### |
############    |
#############   |
##############  |
############### |
################    |
#################   |
##################  |
################### |
</code>

Why is tabbed space width not constant


Check if item with particular value of a property is present in an array

Question: I have a website that is required to read data from an external txt file..I had put that file in the App_data folder and read the data from that file using this code
the txt file contain 3 texts.. which are: kevin123 INFO102lec INFO102k
   protected void logInButton_Click(object sender, EventArgs e)
{
    string usernameListString = File.ReadAllText(Server.MapPath("~") + "/App_Data/usernameFile.txt");

    string[] userAray = usernameListString.Split(' ');
    bool usernameExists = false;
    for (int i = 0; i < userAray.Length; i++)
    {
        if (usernameTextBox.Text == userAray[i])
        {
            welcomeLabel.Text = "Welcome" + userAray[i];
        }

        if (usernameTextBox.Text != userAray[i])
        {
            welcomeLabel.Text = "unknown user";
        }

        usernameExists = true;

    }

I want to  write a for loop that goes through each item of userArray. At the end of the loop, set usernameExists to true only if the username entered in the username text box is present in the array.
I coded something which when I type 1 of those name in the textbox?? it give an welcome label if the name doesn exist?? and vice versa!
IS anything wrong with my code above?
Comment: It looks that you started to do this and then you stopped for some reason?
Answer: You can use Linq for this:
usernameExists = userArray.Any(x => x == usernameTextBox.Text);

Also, in your example, you declare userAray as a string array.. yet you use it like a single string.
Answer: This would work:
bool usernameExists = false;
for (int  i = 0; i < userAray.Length; i++)
{
    if (userAray[i] == "kevin123")
    {
        usernameExists = true;
        break; // stop checking more values
    }
}

Or more simply:
bool usernameExists = userAray.Contains("kevin123");

Or if you want to check whether the array contains any of several values:
string[] userNamesToSearchFor = new[] { "kevin123", "INFO102lec", "INFO102k" };
bool usernameExists = userAray.Intersect(userNamesToSearchFor).Any();

Regarding your update, the problem is that you're trying to update your labels as you're looping through the array. You need to first determine if the username exists in the array (usernameExists) and then set the labels indicating the result, for example:
for (int  i = 0; i < userAray.Length; i++)
{
    if (userAray[i] == usernameTextBox.Text)
    {
        usernameExists = true;
        break; // stop checking more values
    }
}

if (usernameExists)
{
    welcomeLabel.Text = "Welcome " + usernameTextBox.Text;
}
else
{
    welcomeLabel.Text = "unknown user";
}

Comment: I think he wants to check for 3 names: `kevin123`, `INFO102lec`, or `INFO102k`
Comment: thanks....and how do i code when i type 1 of those name in the textbox?? it give an error if the name doesnt exist?? and vice versa!
Comment: ok, i have a text box name: usernameTextBox! and when i type kevin123 in the textbox, the loop started to search for that name..and give an label :) Thanks for your help
Comment: @PéBin Unfortunately, that still doesn't help. I would need to know exactly what code you're using and where you're seeing that error.
Answer: try this
protected void logInButton_Click(object sender, EventArgs e)
{
string usernameListString = File.ReadAllText(Server.MapPath("~")
      + "/App_Data/usernameFile.txt");

string[] userAray = usernameListString.Split(' ');
bool usernameExists = false;
for (int  i = 0; i < userAray.Length; i++)
{
    if (userAray[i]==usernameTextBox.Text)
    {
        welcomeLabel.Text = "Welcome" + userAray[i];
        usernameExists = true;
    }
    else
    {
        welcomeLabel.Text = "unknown user";
    }
}

Comment: just replace **"kevin123"** with **usernameTextBox.Text** in if condition. then show the output in label.
Comment: thanks but i also would like to know how to code when i type kevin123 in the usernameTextBox and it tell me that kevin123 is exist in the system? thanks
Comment: and add code in else section for not matching the name in array.
Comment: i did what you did....But it always come to the result that the name doesnt exsist ==> label.text = unknown user
Comment: is is like this?
if (userAray[i] == usernameTextBox.Text)
            {
                welcomeLabel.Text = "Welcome " + userAray[i];
                usernameExists = true;
            }

            if (userAray[i] != usernameTextBox.Text)
            {
                welcomeLabel.Text = "Unknown User";
            }
Comment: @PéBin haha.. dear if condition should be like this **if (userAray[i]==usernameTextBox.Text)**. try this
Comment: thanks you so much .. but im kind of new to programming ...:(
Comment: It is ok @PéBin we all start like this. just keep it up :-)
Comment: if this answer helps you then don't forget to check the answer.
Answer: The problem is that you need == and try ReadAllLines to read all lines from the file
string[] userAray = File.ReadAllLines();
if (userAray[i] == "kevin123" || userAray[i] == "INFO102lec" || userAray[i] == "INFO102k")
{
   usernameExists = true;
   break;
}

Single = is used for assignment. == is for comparision
Comment: Hrmm.. I just figured that was a typo by the OP.. since the code is half baked anyway.
Answer: try:
    bool usernameExists = false;
    for (int  i = 0; i < userAray.Length; i++)
    {
        if (userAray == "kevin123")
        {
          usernameExists =true;
        }
    }


Will goals be disallowed if striker's foot is in a dangerous position?

Question: Was having a doubt when I came across this video recently. Edison Cavani Amazing Disallowed Goal vs. Barcelona. The goal was ruled out for offside. But suppose there was no offside and Cavani tried this overhead kick. As we can see from the video the defender was trying to clear the ball, and Cavani's striking foot was dangerously close to the defender's head. And maybe even made contact. So question is will this goal be disallowed because of dangerous play if Cavani had actually scored and there was no offside. And if it did, is it fair to the striker who was just trying to make contact with the ball ?
Answer: A goal like that can indeed be disallowed for playing in a dangerous manner.
According to Law 12:

Playing in a dangerous manner
Playing in a dangerous manner is any action that, while trying to play
the ball, threatens injury to someone (including the player themself)
and includes preventing a nearby opponent from playing the ball for
fear of injury.
A scissors or bicycle kick is permissible provided that it is not
dangerous to an opponent.

With the defender that close to the ball this situation is dangerous enough to disallow the goal and award an indirect free kick to the defending team.


MVC ASP.NET Writing to database is looping unintentionally

Question: I'm very new to MVC and ASP.NET, but I've come across a weird issue.
I'm adding one entry to my database, then saving the changes, but that is not what happens.
[Authorize]
    public ActionResult EggRecieved()
    {
        //CHECK IF THEY HAVE RECIEVED EGG IN LAST DAY

        var user = db.PDCUsers.SingleOrDefault(x => x.Username == User.Identity.Name);
        var userId = user.Id;

        var possibleEggs = db.PkmnDatas.Where(x => x.CanBeEgg == true);
        var rand = new Random();
        var no = rand.Next(0, possibleEggs.Count());
        var pokemonArray = possibleEggs.ToArray();
        var pokemon = pokemonArray[rand.Next(0, pokemonArray.Count())];
        Boolean? isMale = null;
        if (pokemon.GenderRatio != null)
        {
            isMale = rand.Next(0, 1000) < pokemon.GenderRatio1.ChanceMale * 1000;
        }
        var isShiny = 1 == rand.Next(1,Constants.SHINYCHANCE);
        //GET THE ABILITIES

        db.PlayerPkmns.Add(new PlayerPkmn
        {
            PkmnDataId = pokemon.Id,
            Nickname = pokemon.Name,
            IsMale = (bool)isMale,
            Nature = rand.Next(1, 25),
            Ability = 1,
            EggObtained = DateTime.Now,
            Shiny = isShiny,
            CurrentTrainerId = userId,
            OriginalTrainerId = userId,
            StepsToHatch = Constants.TOTALSTEPSFOREGGHATCH
        });

        db.SaveChanges();

        return EggRecieved();
    }

When I click a button I call this method, then the server takes half a minute and says it cannot access the view.
Then I check my database and I have 500+ new rows of data.
I'm not sure what's happening, but it looks like it is continuously calling this method and writing to the database.
Anyone know why, and how I can fix it?
Answer: You are recursively calling the method in the end, with
return EggRecieved();

You should return an ActionResult there, like
return View("MyView");

Comment: I feel very stupid.
Late night coding is not the most effective
Answer: This is happening because int the return statement, you wrote:
return EggRecieved();

It means you are calling the same method again and it stuck in endless loop throwing error. Remove that with your View or Json or etc.
Answer: You have wrote at the end of code is return EggRecieved() that is worng,
you need return view() that you want.
so replace EggRecieved to view(); because when you return EggRecieved() then it makes recursive loop and call each time
thank you


How to change text color in Angular Material 15?

Question: I am using Angular Material 15, and defined a theme like so :
<code>$blue-palette: (
  50: #e3f2fd,
  100: #bbdefb,
  200: #90caf9,
  300: #64b5f6,
  400: #42a5f5,
  500: #2196f3,
  600: #1e88e5,
  700: #1976d2,
  800: #1565c0,
  900: #0d47a1,
  A100: #82b1ff,
  A200: #448aff,
  A400: #2979ff,
  A700: #2962ff,
  contrast: (
    50: $dark-primary-text,
    100: $dark-primary-text,
    200: $dark-primary-text,
    300: $dark-primary-text,
    400: $dark-primary-text,
    500: $light-primary-text,
    600: $light-primary-text,
    700: $light-primary-text,
    800: $light-primary-text,
    900: $light-primary-text,
    A100: $dark-primary-text,
    A200: $light-primary-text,
    A400: $light-primary-text,
    A700: $light-primary-text,
  )
);
$my-palette: mat.define-palette($blue-palette, 500);
$my-theme: mat.define-light-theme($palette);
@include mat.all-component-themes($my-theme);
</code>
But I would like to change the font color. It seems like Angular Material automatically defines the font color, black or white, according to my primary color. How do I change the font color ?
Answer: This is an issue in angular material itself, you can follow-up on this thread, once it's resolved, you will have your code working: https://github.com/angular/components/issues/26056


Unable to loop the files to perform diff in python

Question: I am new to python. I am writing a python script to find the diff between 2 html file1: beta.vidup.me-log-2016-09-21-17:43:28.html and file2: beta.vidup.me-log-2016-09-21-17:47:48.html.
To give an idea about my file organization: I have 2 directories 2016-09-21 and 2016-09-22. file1: beta.vidup.me-log-2016-09-21-17:43:28.html is present in dir1 and file2: beta.vidup.me-log-2016-09-21-17:47:48.html is present in dir2.
Below is my snippet:
dir1 = raw_input("Enter date of Archive folder to compare with in format yyyy-mm-dd---->\n")
dir2 = raw_input("Enter date of folder to compare in format yyyy-mm-dd----->\n")

now = datetime.now()
folder_output = '/home/diff_output/{}'.format(now.strftime('%Y-%m-%d'))

mkdir(folder_output)

fname1 = '/home/output/%s/beta.vidup.me-log-2016-09-21-17:43:28.html'%dir1
fname2 = '/home/output/%s/beta.vidup.me-log-2016-09-21-17:47:48.html'%dir2

# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)

cmd = "diff "+fname1+'\t'+fname2
curl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)

file_data = curl.stdout.read()
print file_data

fname1.close()
fname2.close()

I wish to perform diff using the subprocess module only.
I want my code to take fname1 from dir1 and fname2 from dir2 and perform the diff and output it to a folder and Loop back to pick the next file in dir1 for fname1 and next file from dir2 for fname2 and perform the diff again.
Thanks for your time and advice in advance.
Comment: Do you have a question? See [ask].
Comment: My apologies. I edited my question
Comment: You don't need to open the files. Also, it should be `f1.close()`, not `fname1.close()`.
Answer: Below are the functions you need with examples. Merge them using the logic with for loop.
You can use subprocess.check_output() to get output from the command. Try:
cmd = ["diff", fname1, +fname2]
output = subprocess.check_output(cmd)
print output

if you want to write this to file:
with open('/paht/to/file', 'w+') as f:
    f.write(output)

In order to get list of files in directory, use listdir(), isfile and join functions of os module. For example:
from os import listdir
from os.path import isfile, join
only_files = [f for f in listdir('path') if isfile(join('path', f))]
# only_files will contain list of all files in 'path' path

As you mentioned, you don't have much idea on loop. To give you the basic idea on how your loop should work. Below is the example (instead of copying, try to understand each line. it'll be helpful for you in future):
for f1, f2 in zip(file_list_1, file_list_2): # takes first, second, etc files corresponding to each list
    output = subprocess.check_output(['diff', f1, f2]) # generate diff of both file
    with open('diff-{}-{}'.format(f1, f2), 'w+') as f:
        f.write(output)  # write the diff to third file

Modify the above logic as per your requirement.
Comment: Hi Moinuddin, Appreciate your reply. However I am able to perform diff but I am not strong in loop functions. I wish to loop  to pick new files for fname1 and fname2 from 2 different folders
Comment: Updated the answer. You also need `os.listdir()` to get all files in the folder
Comment: Thanks a lot for the listdir() and Zip() . it works like wonders.


Como passar valor do javascript para AJAX

Question: Tenho uma função javascript que possui um código AJAX dentro dele.
Queri passar os valores do javascript para o AJAX, segue o código:
    function validarCamposComprar() {

    var campoNomeEmpresa = document.getElementById('fTxtCadastroNomeEmpresa');
    var campoNomeAdmin = document.getElementById('fTxtCadastroNomeAdmin');

    $.ajax({
        type: "POST",
        url: "email.php",
        data: { meuParametro1: campoNomeEmpresa, meuParametro2: campoNomeAdmin },
        complete: function (data) {
            // (...)
        }
    });

    return true;
}

É possível fazer isto, o que estou errando?
Answer: O problema é que com
var campoNomeEmpresa = document.getElementById('fTxtCadastroNomeEmpresa');

você obtém um elemento do DOM. Acredito que você queira enviar o valor ou o texto, se for esse o caso, faça
var nomeEmpresa = document.getElementById('fTxtCadastroNomeEmpresa').value;
var nomeAdmin = document.getElementById('fTxtCadastroNomeAdmin').value;

que retornam string
Comment: Isso ai @paulojean, arrumei na minha resposta, não tinha reparado nisso.
Answer: É possível e deve ser feito.
Utilizando o elemento data do AJAX ou passando diretamente na URL.
Costumo fazer desta maneira:
var campoNomeEmpresa = $('campo1').value;
var campoNomeAdmin= $('campo2').value;
$.ajax({
    type: "POST",
    url: "email.php?meuParametro1=" + campoNomeEmpresa + "&meuParametro2=" + campoNomeAdmin,
    complete: function (data) {
        // (...)
    }
});

Comment: Como consigo pegar esses valores passados, no meu arquivo PHP?
Comment: Sim, arrumei o exemplo conforme a resposta abaixo.
Comment: @EduardoF.Santos na [tua outra pergunta o Felipe Douradinho já respondeu a isso](http://pt.stackoverflow.com/a/72501/129), usando o `$_POST`. Vê no fim da resposta.


Get ID in HOOK_preprocess_container

Question: I'm currently working a styling of a drupal 8 website. And I'm trying two things.
In HOOK_preprocess_container I want to output a block, I got the block script figured out but I want to filter and output only when the Container ID matches a certain ID. Can someone help me out with this?
Thanks in advance.
My current HOOK:
<code>function HOOK_preprocess_container(&$variables) {
  //something like this
  $containerID = //get container ID;

  $block = \Drupal\block\Entity\Block::load('block_id');
  $block_content = \Drupal::entityTypeManager()
    ->getViewBuilder('block')
    ->view($block);

  if($containerID == "containerID"){
    $variables['containerblock'] = $block_content;
  }
}
</code>
Answer: You should be able to get that with:
<code>$container_id = $variables['element']['#id'];
</code>
Comment: Works like a charm, thank you. For next time, how do I find out where to find an ID, name or something else. When I use Kint I don't see this information.

Get ID in HOOK_preprocess_container


Spark streaming joins with multiple history tables

Question: Spark version: 1.5.2
We are trying to implement streaming for the first time and trying to do the CDC on incoming streams and store results in hdfs.
What is working
We started the POC with 1 table CDC with input file streams. The base (history) table (Hive) is 2.5 GB in SNAPPY compressed PARQUET format. We are joining this with the input dstreams (~10000 records) with streaming interval of 5min.  As we need to join with same base data for each input dstream over and over, we are caching the base data and as a result the joins are working fast.
Working with below setup
--num-executors 8 --executor-cores 5 --driver-memory 1g --executor-memory 4g
what we need suggestion on
If we have to scale up the same solution to do CDC on multiple tables (different CDC operations on ~100 history tables) at the same time in production, we know that caching all that base data is not a good idea because of limited availability of memory.
Is there any better way of doing the joins in streaming without reading all the base data into memory?
is bucketing the hive table will help in anyway?
Answer: I think this section of the docs should be helpful.
You can load your data as RDDs and then join them with your DStreams with the help of the transform operation.


Cópia de arquivos com erro: O sistema não pode encontrar o caminho especificado

Question: Estou com um projeto pessoal que visa automatizar o processo de criação de pastas dos serviços que presto. Atualmente meu objetivo está em criar 2 script básicos para o Prompt do Windows, onde ele cria automaticamente uma série de pastas padronizadas e copia determinados arquivos comuns de uma pasta predefinida para a pasta recém criada.
Atualmente meu script está o seguinte:
@echo off
set /p ano="Entre o Ano a ser criado:"
mkdir %ano%\1.JANEIRO %ano%\2.FEVEREIRO %ano%\3.MARCO %ano%\4.ABRIL %ano%\5.MAIO %ano%\6.JUNHO %ano%\7.JULHO %ano%\8.AGOSTO %ano%\9.SETEMBRO %ano%\10.OUTUBRO %ano%\11.NOVEMBRO %ano%\12.DEZEMBRO
cd %ano%\1.JANEIRO
copy "C:\Users\joao.salvador\Documents\2.SERVIÇOS\00.DOCUMENTOS_PADROES\*.xlsx"
timeout 60

O problema é que, quando rodo este Script, o sistema cria as pastas corretamente porém, ao tentar copiar os arquivos devolve a mensagem:

O sistema não pode encontrar o caminho especificado.

A parte mais estranha, é que quando abro o Promt e utilizo o mesmo código de cópia, funciona normalmente, como demonstrado pelo Log abaixo:
C:\Users\joao.salvador\Documents\2.SERVIÇOS>copy "C:\Users\joao.salvador\Documents\2.SERVIÇOS\00.DOCUMENTOS_PADROES\*.xlsx"
C:\Users\joao.salvador\Documents\2.SERVIÇOS\00.DOCUMENTOS_PADROES\AVBR_Formulario de Reembolso de Despesas de Viagens - Cartao Corporativo.xlsx
C:\Users\joao.salvador\Documents\2.SERVIÇOS\00.DOCUMENTOS_PADROES\AVBR_Formulario de Reembolso de Despesas de Viagens - Reembolso de Despesas.xlsx
C:\Users\joao.salvador\Documents\2.SERVIÇOS\00.DOCUMENTOS_PADROES\ServiceReport_default.xlsx
        3 arquivo(s) copiado(s).

Em minhas pesquisas, verifiquei pessoas citando para ser utilizado o xcopy ao invés de copy, por ser uma versão mais "estável da mesma, mas tentar, continuo tendo um problema similar onde o mesmo me devolve a mensagem:

Arquivo não encontrado  - *.xlsx 0 arquivo(s) copiado(s)

Alguma ideia do por quê deste comportamento estranho?
Informações Adicionais:

Este script é simplesmente um arquivo com extensão .cmd

Meu sistema operacional é um Windows 10 Pro
Answer:
@echo off

%:^{
set/p "_ano=Entre o Ano a ser criado: " || goto %:^{

<con: cd/d "%~dp0" && pushd "%USERPROFILE%\Documents\2.SERVI?OS\00.DOCUMENTOS_PADROES"
set "_mes=JANEIRO,FEVEREIRO,MARCO,ABRIL,MAIO,JUNHO,JULHO,AGOSTO,SETEMBRO,OUTUBRO,NOVEMBRO,DEZEMBRO"

for %%i in (%_mes%)do set/a "_m+=1" & cmd/v/c "md "%~dp0!_ano!\!_m!.%%~i" & >nul copy/b "*.xlsx" "%~dp0!_ano!\!_m!.%%~i""
<con: popd & %__AppDir__%timeout.exe /t 60|echo\Pastas criadas e arquivos *.xlsx copiados para: "%~dp0%_ano%" & goto :eof

Alguma ideia do por quê deste comportamento estranho?
 O uso de Ç no nome de sua pasta \2.SERVIÇOS\

Seu erro pode ser contornado usando o curinga ? em substituição ao Ç:

Em minhas pesquisas, verifiquei pessoas citando para ser utilizado o xcopy ao invés de copy, por ser uma versão mais "estável da mesma, mas tentar, continuo tendo um problema similar onde o mesmo me devolve a mensagem:
XCOPY Copia arquivos e diretórios, incluindo subdiretórios.
COPY Copia um ou mais arquivos de um local para outro.
Não existe versão mais (nem menos) estável
Substituir COPY pelo XCOPY não melhora (nem piorar) o resultado.

A parte mais estranha, é que quando abro o Promt e utilizo o mesmo código de cópia, funciona normalmente, como demonstrado pelo Log abaixo:
Quando usas o prompt, o caminho é digitado corretamente.

rem :: nibir exibição dos comandos no script::
@echo off

rem :: Label :^{ para loop ::
%:^{

rem :: define a variável _ano solicitando o input do usuário ::
set/p "_ano=Entre o Ano a ser criado: " ...

rem :: caso decorrer erro no input da variável _ano, retorna a solicitar ::
... || goto %:^{

rem :: entra no drive:\pasta onde o script está salvo ::
<con: cd/d "%~dp0" ...

rem :: salva a pasta atual e entra e define a pasta de suas planilhas como o primeiro diretorio atual na pilha ::
... pushd "%USERPROFILE%\Documents\2.SERVI?OS\00.DOCUMENTOS_PADROES"

rem :: define um os meses do ano para uso no loop em conjunto com ações/comandos do() ::
set "_mes=JANEIRO,FEVEREIRO,MARCO,ABRIL,MAIO,JUNHO,JULHO,AGOSTO,SETEMBRO,OUTUBRO,NOVEMBRO,DEZEMBRO"

rem :: for loop mes a mes ::
for %%i in (%_mes%)do ...

rem :: cria a variváel  _m (mes numeral) ::
set/a "_m+=1" ...

rem :: invoca o cmd forçando a atualização do valor das variáveis _ano e _m em tempo de execução para o comando que se segue ::
...cmd/v/c ....

rem :: cria a uma sub-pasta _ano\mês numeral + . + mês literal na mesma pasta onde o bat se encontra %~dp0
na mes

cria a pasta atualmente em loop do mês (%%~i == literal), concatenado o  numeral _m + \ + .
"md "%~dp0!_ano!\!_m!.%%~i" ...

rem :: copy 1 ou mais arquivos da pasta atualmete em uso no pushd para pasta em loop do mes ::
copy/b "*.xlsx" "%~dp0!_ano!\!_m!.%%~i""

rem :: restaura a pasta salva anteriormente pelo comando pushd
<con: popd ...

rem :: faz a execução de um timeout em "backgound" redirecionado a saída para a execução do comando echo\ ::
 %__AppDir__%timeout.exe /t 60|echo\Pastas criadas e arquivos *.xlsx copiados para: "%~dp0%_ano%" ...

rem :: sai da execução do script indo para o final do arquivo [End Of File] ::
... goto :eof

Algumas sugestões de leituras adicionas:

 Usando cmd /?

 Usando POPD

 Usando CHCP

 Usando copy

 Usando xcopy

 Usando PUSHD

 Usando Curingas

Algumas leituras adicionais (/English):
[√] For /?
[√] For /F
[√] Goto :Label
[√] Escape Characters
[√] Redirection |, <, > 2>, ...
[√] Conditional Execution || && ...
[√] How does the Windows Command Interpreter cmd.exe Parse Scripts
[√] How-to: Escape Characters, Delimiters and Quotes at the Windows command line


strtotime / 86400 not returning even number of days

Question: I am having a strange result with PHP Version 5.3.1, can anyone explain the below result?
$secondsDiff = strtotime(date("2011-11-10")) - strtotime('2011-07-15');
return ($secondsDiff/86400);

it returns 117.958333333??
When I use dates closer together it generally works.
I had a look through the docs, but couldnt find any reference to this. Is this a known php bug that I just need to learn to live with or am I missing something very obvious?
Answer: This is probably due to a DST switch, which will increase or decrease the length of the period by an hour.
This could be fixed by rounding, or - more elegantly - by PHP 5.3's fancy new DateInterval class which can format periods as days and more.
Stolen from the manual:
<?php
$datetime1 = new DateTime('2009-10-11');
$datetime2 = new DateTime('2009-10-13');
$interval = $datetime1->diff($datetime2);
echo $interval->format('%R%a days');
?>

Comment: Thanks Pekka! Using DateInterval solved the problem nicely :)
Answer: Don't convert seconds into days by dividing them by 86400. It will work most of the time, sure, but in some circumstances it will return an ugly number. That is what is happening here. The usual culprit is a switch into or out of daylight savings time.
The slow way to do it is to use a loop and calls to mktime(); the modern way is to use the DateInterval class.
Answer: The strtotime() function uses the TZ environment variable.  This means that when you convert that date, you end up with a timestamp that isn't exactly at midnight, unless you happen to have your timezone set up for GMT.
So, if you are in an area with daylight savings time, then a date in November will be an hour off than one in July.  You can easily test this using the two dates you provided.  On my server, one appears at 4:00AM, and the other at 5:00AM on their respective dates.
Answer: Another solution:  Add UTC to the string as in
$datetime1 = new DateTime('2009-10-11 UTC');
$datetime2 = new DateTime('2009-10-13 UTC');


The gist of how you create varied pitched/volume realistic drum samples in JavaScript

Question: Like this, what do you need in terms of drum samples to do something like this and have it sound like that native american drum, with dynamic loudness and duration and depth, it's not just the same sound being played over and over again. Does this meanyou need to have 1 sample per variation, so like 100's of samples for asingle drum, or is there a way to somehow modulate the sound to get this kind of variation out of just one sound? I assume there is no real way to practically generate these sounds from scratch, so some sort of sampling is needed. I would just like to know roughly what this is.
I just really want to be enlightened on how drum samples work. Do you have 100's of them somehow programmed into a single pad, and so variations in striking speed invokes different ones? Or how does it work?
Answer: I think you have the gist pretty well already!
You certainly could use a large number of samples per drum, and select them based on striking speed, position, etc. That is by far the most common approach taken in commercial software.
Synthesising drums from scratch isn't impossible, though it is hard to do well. Synth secrets has some articles on drum synthesis.
You could also consider various hybrid approaches. A "Sample + Synthesis" approach would use a number of samples and then alter their pitch, volume, and apply filtering in real time to get more mileage out of a smaller set of samples. You might even be able to enhance this approach by generating samples through synthesising elements of the sound, and then layer those elements (which themselves could be pitch-shifted, filtered, etc).
Another way to go would be to create a physical model of the drum. I'm not sure if that's likely to be possible to do efficiently enough in JavaScript, or if Web Assembly might be usefully brought to bear.


How to set insets

Question: How can I set the insets to a Vaadin label.I have below developed.I want to have some space between the button and label.
        button_3 = new Button();
        button_3.setCaption("Button");
        button_3.setImmediate(true);
        button_3.setWidth("-1px");
        button_3.setHeight("-1px");
        horizontalLayout_4.addComponent(button_3);

        // prize_set_label
        prize_set_label = new Label();
        prize_set_label.setImmediate(false);
        prize_set_label.setWidth("-1px");
        prize_set_label.setHeight("-1px");
        prize_set_label.setValue("small:23");
        horizontalLayout_4.addComponent(prize_set_label);

Comment: Are you developing the UI by hand? ...I recommend you give something like Netbeans a try to save yourself the time and effort.  Spacing like you want above can be easily done.  Even if you still want to do it manually, the code generation with Netbeans UI is tidy enough so that you an see how to do it yourself.
Comment: @JacoVanNiekerk no this  is a vaadin ui.I have used tools like window builder but in vaadin it does not provide similar support
Answer: To add spacing between components in (most) layouts, you can use setSpacing(boolean) - e.g. horizontalLayout_4.setSpacing(true);
If you want to change the size of the spacing, you must change this in CSS.
I don't understand what you are doing there with the size of the button & label; as far as I'm aware, setting the width/height to negative values is not valid in Vaadin.
Comment: Ahh - Of course! Normally, you'd use `setSizeUndefined()` to be clearer. [This chapter](https://vaadin.com/book/-/page/layout.settings.html) in the Book Of Vaadin explains how to change the size of the spacing/margins.
Comment: Thanks Charles.It works as you said I can not set the insets by it but it gives me enough space though.Setting -1px means it handles width and height by auto.It takes the available space in the layout I guess.
Answer: To some who want to modify a Component Insets (e.g. for a clean, readable CellEditor under some LaF (yes I'm looking at you Nimbus!)), the insets are actually the one from the Component Border, so you need to specify an empty border or just remove with it Component.setBorder(null).
N.B. that if no border is defined, it's the Component peer insets that are used.

How to set insets


How to make unpitched percussion and a pitched instrument share a staff in LilyPond?

Question: For parts where the percussionist switches from vibraphone to maracas, for example, is there a way to combine the two on the same staff?  IIRC, LilyPond treats normal staves and percussion staffs completely separately.
Comment: Did you check the Lilypond Documentation online (http://lilypond.org/doc/v2.18/Documentation/notation/common-notation-for-percussion)? It is totally possible to combine different percussion instruments in one staff. If the documentation does not contain your specific problem, please explain a bit more detailed what you want to achieve.
Comment: I was specifically referring to pitches and unpitched instruments sharing a staff. `\drummode` is completely useless for the vibraphone sections of the part: the vibes need normal note entry.
Comment: Okay, I understand. But I fear the only thing you can try is just using a pitched staff for the maracas as well. If you need midi output you can add an invisible additional drum staff that is just used for midi output.
Comment: Say the maracas are a B-flat and override the staff to have one line and give an x notehead?
Answer: I fear the only thing you can try is to just use a pitched staff for the maracas. You can adjust the visibility of accidentals and the line count of the system with overrides.
I am not fully sure, how your score will look in the end, but perhaps you could start like this:
\version "2.18.2"

startMaracas = {
  \stopStaff
  \override Staff.StaffSymbol.line-count = #1
  \override Staff.Accidental.stencil = ##f
  \override NoteHead.style = #'cross
  \startStaff
  \once \override Staff.BarLine.bar-extent = #'(-2 . 2)
}

endMaracas = {
  \stopStaff
  \revert Staff.StaffSymbol.line-count
  \revert Staff.Accidental.stencil
  \revert NoteHead.style
  \startStaff
}

\score {
  <<
  \new Staff {
    c'4^\markup{ "Vibraphone" } d' e' f'
    c'4 d' e' f'
    \startMaracas
    bes'4^\markup{ "Maracas" } bes' bes' bes'
    bes'4 bes' bes' bes'
    \endMaracas
    c'4^\markup{ "Vibraphone" } d' e' f'
    c'4 d' e' f'
  }
  >>
}

Rendering as:

You can add the following function to your code if you want to use \changeInstrument "maracas" or \changeInstrument "somethingelse" instead of the defined macros:
changeInstrument = #(define-music-function (parser location instrument) (string?)
  (if (equal? instrument "maracas")
    #{ \startMaracas #}
    #{ \endMaracas #} )
  )

Comment: I've been studying the score for a concert band piece lately (*Shadow Rituals* by Michael Markowski), and in that piece, I've seen unpitched percussion instruments share *five-line* staves with pitched percussion instruments.
Comment: Could `\startMaracas` and `\endMaracas` be refactored to `\instrumentSwitch "vibes"` and `\instrumentSwitch "maracas"` (with the accompanying instrument switch definitions more or less identical to your start & end maracas definitions)?
Comment: You could add a short scheme function to redefine your code. See my edit.


Counterexample: Homology with Local Coefficients versus Homology with Module Coefficients

Question: Definitions Let $X$ be a nice space with universal covering $\widetilde{X}$.

Homology $H_*(X,R)$ with Ring Coefficients $R$ is the homology of
\begin{align*}
C_n(X;R) :=&\; \text{ free left $R$-module with basis $S_n(X)$}, \\
\cong&\; R \otimes_{\mathbb{Z}} C_n(X)
\end{align*}
where $C_*(X)$ is the singular chain complex.
Homology $H_*(X;M)$ with Module Coefficients $M$, where $M$ is a left $R$-module, is the homology of
$$
C_n(X;M) := M \otimes_R C_n(X;R)
$$
Homology $H_*(X;A)$ with Local Coefficients in $A$, where $A$ is a  left $R[\pi_1X]$-module, is the homology of
$$
C_n(\widetilde{X};R) \otimes_{R[\pi_1X]} A
$$
where we view $C_n(\widetilde{X};R)$ as a right $R[\pi_1X]$-module using the monodromy action on $\widetilde{X}$.

Question: Is the local homology with local coefficients given by a left $R[\pi_1X]$-module $A$ (def. 3) the same as the homology with module coefficients in $A$, viewed as a $R[\pi_X]$-module (def. 2)? In other words, is the expression
$$
H_n(X;A)
$$
even well-defined? If not, what is a counter example?
Answer: They are distinguished by their $\pi_1X$ action. Your definition of coefficients in a module, is the so-called untwisted homology by:
$$
C_n(X;M) := M \otimes_R C_n(X;R) = M \otimes_R R^{|n|} = M^{|n|},
$$
where $|n|$ denotes the number of $n$-cells of $X$ with no $\pi_1$ action on it. (3) in contrast is the twisted homology. As suggested by the name, (2) is the "regular" case, and (3) generalizes it by a  non-trivial $\pi_1X$ action.
So if you want, define $M$ as a $R[\pi_1X]$-module by the trivial action, which then gives module homology as a special case of local coefficients. You can read about this in Hatcher's Algebraic Topology, more specifically you are going to learn if the action on $A$ is given by $a:\pi_1 X\to Aut(A)$, it suffices to look at a cover corresponding to $ker(a)$ while tensoring over $R[\pi_1(X)/ker(a)]$ the group ring over the deck transformations. In the trivial action case, that gives you the equivalence (up to the left- and right-stuff which you have to be careful with).
As you noticed correctly, this potentially leads to confusion. When being sloppy we get weird looking things like
$$
H_1(S^1;\mathbb Z/3) = \mathbb Z/3 \neq 0 = H_1(S^1;\mathbb Z/3),
$$
as computed here Homology with twisted coefficients of the circle . The difference is that on the left hand side I interpreted $\mathbb Z/3$ as a $\mathbb Z$-module, while on the right hand side I mean a module with a non-trivial fundamental group action, that is a $\mathbb Z[\mathbb Z]$-module with non-trivial $\mathbb Z$-action.
Therefore it is common practice to potentially think of every homology coefficients $A$ as $\mathbb [\pi_1X]$-modules via $a: \pi_1X \to Aut(A)$ and write
$$
H_i(X;A),
$$
if $a=1$ and otherwise specify
$$H_i(X;a),$$
by explicitely mentioning the twisting. However if the twisting is clear from context, often the former is (ab)used anyway.
Comment: Thank you, this clears up my understanding. But still the thing is, given a ring $R$ and a $R[\pi_1X]$-module $A$, there are two ways to arrive at $H_*(X;A)$. First use the definition of twisted homology. Second, consider $S = R[\pi_1X]$ as the new base ring (forget that its a group ring!), and view $A$ as a usual module over $S$, and we get $H_*(X;A)$ as the untwisted homology with coefficients in the $S$-module $A$. Are they the same?
Comment: In other words, is twisting again just a special case of taking the untwisted homology with coefficients in a $R[\pi_1X]$-module? I don't think it is, because the former involves $\widetilde{X}$, while the second one uses only $X$ itself.


AWS CIDR Address is not within CIDR Address from VPC

Question: In AWS have created VPC which CIDR  is 10.0.0.24.I want  to creates its  two subnet.its public-subnet is in us-west-1a - IPv4 CIDR 10.0.0.0/24 thenwhen  I create  private subnet is in us-west-1b - IPv4 CIDR=10.0.20.0/24.
I have tried IPv4 CIDR=10.0.20.0/24 not working.
Answer: You have used the entire VPC CIDR (10.0.0.0/24) for the first subnet .. no more space left in the VPC.
Answer: You created your VPC as 10.0.0.0/24, which means all IP addresses must be 10.0.0.x (first 3 parts are 10.0.0, with only the last part changing).
Your first subnet is also 10.0.0.0/24, which means your single subnet has now occupied your entire VPC. So you have 2 problems:

There's no room for another subnet, and
The subnet mask you are using for your second subnet is not in your VPC (10.0.20.x is not inside 10.0.0.x). This is actually what the error message is telling you.

Change your VPC to be 10.0.0.0/16 instead. This means only the first 2 parts are constant (10.0.x.x). Then you can start making your multiple /24 subnets (10.0.0.0/24, 10.0.20.0/24, etc.)


Violin drooping when stopping strings

Question: To avoid the violin drooping when I stop a string I hyperextend the metacarpophalangeal joint of the index finger to let the violin rest on the "mount of jupiter" so to speak, but this makes the "mount of jupiter" reddish after a couple of hours of playing. (Double jointedness/hypermobility in fingers makes this easy to do, but since I got red maybe it is not very good).
What can be done so that the torque of pulling the strings down can be counteracted? Or should I just let the violin rotate perhaps? It is only a few degrees after all.
Some things I believe could counteract it: sharing the pressure with thumb, moving the chin downwards with some extra force, not pulling the string all the way to the fingerboard. Any thoughts?
Answer: What you describe is a perfect example of the more extreme side effects of trying to learn without a proper teacher.

Some things I believe could counteract it: sharing the pressure with thumb

That is the normal way it is done. If you had a teacher he/she would have taught you that in the very lesson. In first position the violin should rest half way up the "V" formed by thumb and forefinger.

moving the chin downwards with some extra force

Again, if you had a teacher then you would already know that when you start using phrases like "extra force" that you are describing something dangerous and silly. Applying "force" for any considerable period of time leads to injuries (as you have already discovered). Don't look for more ways to injure yourself.

not pulling the string all the way to the fingerboard

If you had a teacher you would already have learned that the correct motion is not one of pulling the strings down to the fingerboard but of pressing them into the fingerboard from as directly above the strings as you can manage. Yes, the amount of downward pressure will need to vary according to the different musical notes/effects you are trying to produce.

Any thoughts?

Do yourself a favour. Get a real flesh-and-blood teacher then he/she will fix these very basic problems in short order and you will be saved from more pain and suffering. What you are currently doing is ingraining, through much practice, habits which not only lead to injury but which take longer to eradicate the longer you persist.
Comment: I can slide without problem, so I don't know what you are talking about. I am only touching it from below when pulling the string down on the fingerboard, on the area just below the index finger on the inside.
Comment: Do you press the string on a bow and arrow as well? I think your semantical hangups are irrelevant. I already have a teacher. He thinks I should just use the thumb, but this contradicts other sources that says no tension anywhere, but without any tension the violin tilts. Also I don't think every teacher knows how to deal with double jointedness.

Violin drooping when stopping strings


Drupal form elements in html table, label (title) rendering

Question: How to place title/label in other cell of the html  table
I have:
function hook_theme() {
  return array(
    'hook_form_table' => array(
      // The renderable element is the form.
      'render element' => 'form',
    ),
  );
}

function hook_form_table_form($form = array(), &$form_state) {

    $form['#theme']= 'hook_form_table';
    $selected_two="";
    $options_second=array("a");
    $form['carmodel'] = array('#type' => 'select',
        '#options' => $options_second,
        '#default_value' => $selected_two,
        '#required' => TRUE, '#title' => t('Car model:'),
        '#validated' => true,
        '#prefix' => '
',
        '#suffix' => '
',
    );

    $options_euro=array("Euro 3,4","Euro 5" );

    $form['euro'] = array('#type' => 'select',
        '#options' => $options_euro,
        '#default_value' => $selected_two,
        '#required' => TRUE,
         '#title' => t('Euro type:'),
        '#validated' => true,
     );

    $form['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Submit'),
    );

  return $form;
}

function theme_hook_form_table(&$variables) {
    $form = $variables['form'];
    $content = '
' . t('Please enter your information below') . '
';
    $content.="
";
    $content.="
";
    $content.="";
    $content.=$form['carmodel']['#title'];
    $form['carmodel']["#type"]="";

    $content.="	";
    $content.="";
    $form['carmodel']['#title']="";
    $form['carmodel']['#required']="";
    $content .= drupal_render($form['carmodel']);
    $content.="	";
    $content.="";
     $content.="hire want euro type  with * and label and all";
    $content.="	";
    $content.="";

    $content .= drupal_render($form['euro']);
    $content.="	";
    $content.="
";
    $content.="
";
  return $content;
}

This give me a table but labels is on top:

How to render label to put it like this:
Answer: My not nice solution is
function drupal_render_label($field){
    $result= "
";
    $result.= "".$field['#title'];
    if ($field['#required']==true){
       $result.= "*";
    }
    $result.="";
    $result.="
";
    return $result;
}

and then:
  $content.="";
  $content.=drupal_render_label($form['carmodel']);
  $content.="	";

Anyone have sth better?


Redirecionar uma rota de um "childModule" para outro "childModule" com o Angular 2 (^4.0)

Question: Olá, estou trabalhando com o Angular onde eu tenho uma estrutura como esta:
|-app
   |-fooModule (fooPage1, fooPage2)
   |-barModule (barPage1, barPage2)

Esses dois módulos estão sendo carregados usando lazyLoad no arquivo de rotas como mostra a seguir:
app-routing.module.ts
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

const routes: Routes = [
    { path: 'bar', loadChildren: './foo/foo.module#FooModule' },
    { path: ''   , loadChildren: './bar/bar.module#BarModule' }
];

@NgModule({
    imports: [RouterModule.forRoot(routes)],
    exports: [RouterModule]
})
export class AppRoutingModule { }

O módulo AppRoutingModule é carregado no módulo inicial do projeto, o AppModule juntamente com outros, e tanto em foo quanto em bar a mesma estrutura mas, ao invés de carregar módulos com lazyLoad, eu carrego componentes, assim:
exemplo: foo-routing.module (bar-routing.module.ts tem a mesma estrutura)
import { FooPage1Component } from './foo-page-1/foo-page-1.component';
import { FooPage2Component } from './foo-page-2/foo-page-2.component';

const routes: Routes = [
    { path: 'page2', component: FooPage2Component },
    { path: ''   , component: FooPage1Component }
];

Meu problema é que, quando eu estou na rota www.meusite.com/page2 e tento forçar um roteamento para o BarModule, o Angular não remove os componentes já carregados do FooPage2Component. É como se ele nao desse update no <router-outlet>. A forma que faço a chamada para outro módulo pode ser vista no componente FooPage2Component:
foo-page-2.component
import { Component } from '@angular/core';
import { Router } from '@angular/router';

@Component({
    selector: 'foo-page-2',
    templateUrl: './foo-page-2.component.html',
    styleUrls: ['./foo-page-2.component.scss']
})
export class FooPage2Component {
    constructor(
        private router:Router
    ){
        // depois de 2 segundos, redirecione para o módulo bar na pagina 2
        setTimeout(()=> router.navigate(['../bar/page2']), 2000);
    }
}

Quando eu compilo o código acima, e faço o teste, o resultado na minha página ao fazer o redirecionamento fica assim:
estrutura renderizada no navegador

        <app>
             <router-outlet></router-outlet>
             <foo-page-2></foo-page-2>
             <bar-page-2></bar-page-2>
        </app>

Então aqui vai a pergunta: se eu tenho 2 módulos diferentes que tem suas próprias rotas e são carregados dinamicamente (com lazyLoad no loadChildren) como eu posso dizer ao angular que eu quero, a partir do componente FooPage2 que está no FooModule, redirecionar para um componente BarPage2 no BarModule?

Nota: já tentei usar o Router.navigate, Router.navigateByUrl, já tentei forçar a navegação passando os parâmetros do NavigationExtra.relativeTo e mesmo assim, não funciona. Infelizmente minha solução até o momento está sendo um (horrível) window.location.href = window.location.href.replace('/page2','/bar/page2);

Alguém pode me ajudar?
Answer: Como não houve nenhum tipo de resposta e, também não encontrei uma solução plausível na internet, minha solução foi criar um serviço que está relacionado ao component AppComponent e que fica escutando um evento customizado. Esse evento customizado simplesmente dispara o array que a referencia do Router precisa para conseguir fazer o redirect sem sair do angular, assim todo o controle de redirecionamento é feito pelo módulo principal independente do módulo filho que eu usar assim:
Classe RootRouterRedirect.ts

export class RootRouterRedirect {

    public constructor(redirect: string | string[]) {
        const event = new CustomEvent('rootRouterRedirect', { detail: [].concat(redirect) });
        window.dispatchEvent(event);
    }

}

E no meu AppComponent eu anexei um método que faz o listener desse evento, assim:
AppComponent

import { Component, HostListener } from '@angular/core';
... outros imports
@Component({
    selector: 'app',
    template: `
        <router-outlet></router-outlet>
    `
})
export class AppComponent {

    ... construtores e métodos

    @HostListener('window:rootRouterRedirect', ['$event'])
    private event(e): void {
        this.router.navigate(e.detail);
    }

}

Porque fazer isso?
O Angular 2 permite que se tenha múltiplos arquivos de rotas sendo
  cada um em cada módulo. Essas rotas podem ter seus redirecionamentos
  mas, pelo que percebi quando se direciona rotas entre módulos
  carregados via lazyload como na descrição do problema, a estrutura
  de rotas não consegue destruir corretamente todos os componentes,
  mantendo assim trechos, rastros ou em ocasiões a própria rota sem
  atualizar.
Agora se caso quem fizer a chamada for a rota que trabalha sobre o
  RouterModule.forRoot(), esse problema não ocorre porque ele é que
  faz o tratamento principal. Além disso, parece que se o módulo não
  está carregado (porque ser um módulo lazyload) a sub-estrutura do
  RouterModule.forChild() que está definida nele ainda não é
  encontrada, portanto não pode ser referenciada.


Calculate transitivity for all networks in a list

Question: I have 500 random networks stored in a list
for (x in seq_len(500L)) {
+     gs[[x]] <- erdos.renyi.game(361, 695, type = "gnm")

I am able to calculate the transitivity for each network individually using
transitivity(gs[[1]])

How do I calculate the transitivity for all the networks and output the values into an excel sheet so as to do some statistical analysis. Anyt thoughts please.
Answer: Use an *apply function, specifically sapply or lapply.
library(igraph)

gs <- list()
for (x in seq_len(500L)) {
  gs[[x]] <- erdos.renyi.game(361, 695, type = "gnm")
}

gs.trans <- sapply(gs, transitivity)
write.csv(gs.trans, file="Graph_transivity.txt")


Japanese Test Sample Question

Question: Sorry for the ambiguity in the title, I had no idea what else to put.
I am studying for a test I have to take for a Japanese scholarship and I have been able to do all of the other sample questions except this one:
The total number of subtractions that result in 11111 remaining after a four-digit number has been subtracted from a five-digit number and the digits 1 through 9 have all been used is...
When I first saw this question I immedietly thought that this was a simple question and only took a little bit of logical guesswork. Soon, I found that 19753 - 8642 = 11111 exactly and figured the number of subtractions must be 1. The answer is not 1, it is 24. This makes no sence to me because the numbers I chose are within the requirements and therefore valid, so even if you could find a 5-digit number that could subtract a 4-digit number 24 times to get 11111, it would not make my answer wrong.
In my opinion this problem can be shown by the equation y - mx = 11111, with y being the 5-digit number, m being the number of subtractions, and x being the 4-digit number.
This test includes topics ranging from algebra 1 to integral calculus, so this question could be an algebra question like I was thinking, but it might be something else.
Rather than showing me the work that results in the answer, 24, I would greatly appreciate it if someone could nudge me in the right direction.
If you need clarification I will be glad to do so. Thank you for your time.
Comment: If I'm not mistaken, the question asks for the **number of pairs** $(x,y)$ such that $x$ is a 5-digit positive integer, $y$ is a 4-digit positive integer, $x$ and $y$ contain all digits from 1 to 9 and $x-y=11111$.
Comment: I recommend you quote the problem exactly.  It is possible you misunderstood the problem.
Answer: I would say when thinking about the number of possible solutions, consider how many times you could arrange the pairs of numbers to get 1. you have 4 pairs. i.e. 4 factorial "number of subtractions."
To clarify, yes youre solution is correct. But I think the question is asking how many different solutions there are. That would be 24 ways you can get 11111.
Comment: Thank you, i understand the question now
Answer: Lattice's comment about the interpretation of the question yields 24 solutions.
Rephrasing it different the question could be thought of as: How many possible subtractions exist between a five digit and four digit number using only the digits 1 to 9 once each which gives  an answer of 11111.
The answer of 24 comes from the following pairs of five and four digit numbers:
$13579-2468=11111$, $13597-2486=11111$, $13759-2648=11111$
$13795-2684=11111$, $13957-2846=11111$, $13975-2864=11111$
$15379-4268=11111$, $15397-4286=11111$, $15739-4628=11111$
$15793-4682=11111$, $15937-4826=11111$, $15973-4862=11111$
$17359-6248=11111$, $17395-6284=11111$, $17539-6428=11111$
$17593-6482=11111$, $17935-6824=11111$, $17953-6842=11111$
$19357-8246=11111$, $19375-8264=11111$, $19537-8426=11111$
$19573-8462=11111$, $19735-8624=11111$, $19753-8642=11111$
You will notice in the answer (and in the one that you found) that the five digit number must consist of odd numbers and the four digit number must consider of the even numbers. This is because for each of the subtractions there needs to be no carry.

Japanese Test Sample Question


estimating variance using only data at the tails without resorting to Gibbs sampling

Question: Suppose we know that the population size is $n=1,000$ but for whatever reason, we only have the bottom $n_1=100$ observations and the top $n_2 = 200$ observations. Furthermore, suppose we know the data $X_i \overset{\text{iid}}{\sim} N(0,\sigma^2)$. Basically, we are missing the middle $700$ observations but would like to estimate the variance $\sigma^2$.
Normally, I would sample the missing observations from a truncated Normal as part of a simple Normal-Inverse Gamma model. But suppose we can't do that. It seems to me that because we know the bounds of the observations we should have an analytical solution or at least some frequentist solution. Ideas?
Comment: Gene, please let me know whether the suggested solution is satisfactory for your purposes. I am curious to know.
Answer: Here is an analytical solution:
If the data $X_1,\ldots,X_n\sim \mathcal{N}(0,\sigma^2)$, then we expect that
$$
\hat{F}_n(x)\approx F(x),
$$
where $\hat{F}_n$ is the empirical CDF and $F$ is the true normal CDF,
$$
F(x)=\Phi\left(\frac{x}{\sigma}\right),
$$
where $\Phi$ is the standard normal CDF. Let $x_{(1)},\ldots,x_{(n)}$ be the order statistics. We know the first 100 and last 200 of them. By definition of the empirical CDF,
$$
\hat{F}_n(x_{(k)})=\frac{k}{n}.
$$
Therefore,
$$
\Phi\left(\frac{x_{(k)}}{\sigma}\right)\approx\frac{k}{n}.
$$
There is a little technical problem with this approximation for $k=n$ (the argument of $\Phi$ becomes infinite). The standard remedy is to replace $\frac{k}{n}$ with $\frac{k}{n+1}$:
$$
\Phi\left(\frac{x_{(k)}}{\sigma}\right)\approx\frac{k}{n+1},
$$
which for large $n$ (as in our case) works well. The above approximation gives an estimate for $\sigma$:
$$
\sigma\approx\frac{x_{(k)}}{\Phi^{-1}\left(\frac{k}{n+1}\right)}.
$$
Since we know $m=n_1+n_2=300$ order statistics, we have $m$ estimates for $\sigma$. To cook up one estimate, we can take the average of them:
$$
\sigma\approx \frac{1}{n_1+n_2}\left(\sum_{k=1}^{n_1}\frac{x_{(k)}}{\Phi^{-1}\left(\frac{k}{n+1}\right)}+\sum_{k=n-n_2+1}^n\frac{x_{(k)}}{\Phi^{-1}\left(\frac{k}{n+1}\right)}\right).
$$
Comment: @Kostia, thanks for the solution. Would you mind modifying your empirical CDF to $\hat{F}_n(t) = \frac{1}{n+1} \sum_{i=1}^{n} 1_{x_i \leq k}$, I feel that that is the more canonical and generic version because it's robust to ties.
Comment: It seems there should be a better way than (equally) averaging these 300 estimates.
Comment: Maybe, but this would be only minor improvements. I checked with simulation: my solution works pretty well.
Comment: I think that the definition I used is more standard and natural. All modifications are for treating certain technical issues, which do not occur in this example. essentially for n=1000 both definitions a the same for all practical purposes :) So let me leave my solution as is, but highlight your comment as the one that "adds something useful to the post."


How to minimize a maximum of a function of 2 parameters with R

Question: I need to maximize wrt x and minimize wrt $\boldsymbol{\alpha}$ the ratio (I will call it f) between the density of a standard normal and a double exponential distribution. So my goal is to find $\boldsymbol{\alpha}$
I write the function:
$ f(x) = \frac{exp(-x^2/2)/\sqrt{2*pi}}{(\alpha/2)*exp(-\alpha * |x|)} $
By taking its derivative w.r.t to x, I find that f is maximum in x=$\pm \boldsymbol{\alpha}$. Now I compute f($\boldsymbol{\alpha}$) = f(- $\boldsymbol{\alpha}$) = $\sqrt{2/π} * exp(α^2 /2) /α$  and I minimize it by taking its derivative in $\boldsymbol{\alpha}$. I will find $\boldsymbol{\alpha}=\pm$1 as a result I was looking for.
How can I find $\boldsymbol{\alpha}$ with R? I built the function but I cannot find a way to solve this kind of problem.
f<-function(param) {
x<-param[1]
alpha<-param[2]
fn<-(exp(-x^2/2)/sqrt(2*pi))/((alpha/2)*exp(-alpha*abs(x)))
return(fn)
}

Comment: Look at ?optim in R
Answer: The value of $f(x,\alpha)$ maximimised with respect to $x$ becomes a new function $g(\alpha)$ that only depends on $\alpha$.  If I understand your problem correctly you then want to minimise $g(\alpha)$ with respect to $\alpha$.  Here is how you could do this numerically in R.
dlaplace <- function(x,alpha)
  alpha/2*exp(-alpha*abs(x))

f <- function(x,alpha)
  dnorm(x)/dlaplace(x,alpha)

g <- function(alpha)
  optimize(f,c(0,10),alpha=alpha,maximum=TRUE)$objective

optimize(g,c(0,10),maximum=FALSE)


Node id in views template

Question: Sorry if this question has been answered already but I couldn't find an exact answer. Maybe I'm missing something.
Context of my issue:
I have a view that lists fields of certain content type. One of the fields is the nid. I need to do some validation in the view template before printing anything. So I need to call a function and check something using the nid as parameter. In my views-view-fields.tpl.php I do something like:
<?php foreach ($fields as $id => $field): ?>
    <?php if ($id == "nid"): ?>
        <?php my_function_here($field->content); ?>
    <?php endif; ?>
<?php endforeach; ?>

As you might expect, $field->content is a string and if I do something like this in my function, it just won't work.
function my_function_here($nid) {
    var_dump(node_load($nid));
}

I tried casting the value to integer, but still no luck (casting returns 0):
function my_function_here($nid) {
    $nid = (int)$nid;
    var_dump(node_load($nid));
}

and
function my_function_here($nid) {
    $nid = intval($nid);
    var_dump(node_load($nid));
}

So my question is: how do I get the nid in that view template?
Big thanks in advance!
Answer: From a similar issue - you can find details here (or just search for views preprocessor):
A preprocessor will give you access to the data, instead of rendered html which you find in the tpl file.

There aren't dynamically generated preprocess functions; instead, all
  of the theme functions use the base preprocessor automatically. So
  views-view--my-first-view.tpl.php still uses
  template_preprocess_views_view(&$vars) as its preprocessor function.
You can add preprocess functions for specific views with the exact
  same naming conventions. Note that that it's named *preprocess not
  preprocessor_.
Note that currently Drupal's design requires that you have the
  .tpl.php file in order to have the preprocess function (though you
  could drop in a generic preprocess function and do switches based upon
  information contained within the $vars['view'] variable. There's an
  issue for this which is fairly easily found by going to the Drupal
  queue and searching in the 'theme system' queue.

Node id in views template


How to get value of a child of node-set?

Question: I am using a variable in my XSLT to select correct node and then I want to use this variable to retrieve its child nodes:
<xsl:variable name="CorrectNode">
              <xsl:choose>
                 <xsl:when test="$Formula='14'">
                    <xsl:value-of select="f:node14" />
                  </xsl:when>
                  <xsl:when test="$Formula='15'">
                     <xsl:value-of select="f:node15" />
                  </xsl:when>
              </xsl:choose>
</xsl:variable>
<Revenue>
   <xsl:value-of select="msxsl:node-set($CorrectNode)/f1:revenueValue" />
</Revenue>

However, it does not output anything. If I have:
<xsl:value-of select="msxsl:node-set($CorrectNode)" />

or
<xsl:copy-of select="msxsl:node-set($CorrectNode)" />

then the values or nodes are outputted but how to access its children?
Answer: This snippet sets the variable to a result tree fragment consisting of one text node, whose value is the string value of the appropriate element.  The variable does not refer to the element itself, only its string value.
But you don't need the RTF and node-set function here at all, as you can do it with a straight select using appropriate predicates, since the test expressions are not context-dependent (a test like $Formula = '15' gives the same value regardless of whether the current context is the f:node15 element or its parent):
<xsl:variable name="CorrectNode"
              select="f:node14[$Formula = '14'] | f:node15[$Formula = '15']" />

This way the variable is a reference to the original element, and you can navigate from there using XPath.


MCMC algorithm going wrong

Question: Given this integral
\begin{equation}
\int_0^\infty \chi_{[1,2]}(x)\Gamma(C,x)\left|\cos(R x)\right| \, dx
\end{equation}
where $\chi_{[1,2]}=\begin{cases}1, & x \in [1,2] \\ 0, & x\not \in [1,2]\end{cases}$
I need to estimate its value using MCMC with Metropolis algorithm and acceptance.
I wrote the sampler as it follows:
def sampler(N):
    step=1
    old=1.5
    i=0
    points=[]
    while i<N:
        points.append(old)
        ant=func(old)
        inov=norm.rvs(0,step)
        new=old+inov
        prox=func(new)
        alphah=min(1,prox/ant)
        if alphah>np.random.uniform():
            old=new
        i+=1
    points.append(old)
    return points

sampler(N) generates $N$ points by using the Metropolis Algorithm with N(0,step) as kernel and starting in 1.5.
The function func(x) evaluates $\chi_{[1,2]}(x)\Gamma(C,x)\left|\cos(R x) \right|$.
After generating $N$ points $x_0,x_1,\ldots,x_N$ the estimative is
\begin{equation}
 \frac{1}{N}\sum_{i=1}^N \chi_{[1,2]}(x_i)\Gamma(C,x_i) \left|\cos(R x_i)\right|
\end{equation}
The problem is that doing like that I find approximately 0.28 while I need to find 0.26
My algorithm is wrong? Or there is something that I'm not seeing?
EDIT: Implementation of func and the Monte Carlo algorithm mc
def func(x):
    c=1.398031095
    d=1.9298378
    if x<=2 and x>=1:
        return gamma.pdf(c,x)*abs(np.cos(d*x))
    return 0

def mc(N):
    points=sampler(N)
    i=0
    g=0
    while i <N:
        g+=func(points[i])
        i+=1
    return g/N

Comment: Add the self study tag.
Comment: It would help if you provided the implemention of `func`, too. Right now I can't find anything wrong with this. Perhaps you're not running it for long enough
Comment: I will post the implemention of ```func``` and  monte carlo
Comment: I found the error, I wrote on ```func``` gamma.pdf(c,x). But the correct function is gamma.pdf(x,c)
Answer: I believe the following should work for you. My question is why do you have new=old+inov in your sampler? I replaced that with new=inov. Also, you should expect to see some deviations. I didn't really dig into this problem much but  I do not think there is much of a difference between 0.26 and 0.28. Is it given that 0.26 is the target value (mean) for this distribution?
import numpy as np
from scipy.stats import gamma, norm

def func(x):
    c=1.398031095
    d=1.9298378
    if x<=2 and x>=1:
        return gamma.pdf(c,x)*abs(np.cos(d*x))
    return 0

def mc(N):
    points=sampler(N)
    i=0
    g=0
    while i <N:
        g+=func(points[i])
        i+=1
    return g/N

def sampler(N):
    step=1
    old=1.5
    i=0
    points=[]
    while i<N:
        points.append(old)
        ant=func(old)
        inov=norm.rvs(0,step)
        new= inov #old+inov
        prox=func(new)
        alphah=min(1,prox/ant)
        if alphah>np.random.uniform():
            old=new
        i+=1
    points.append(old)
    return points

out = sampler(10000)
out = np.array(out)

results = np.array(list(map(func, out)))
print(np.mean(results))

Comment: Yes it is given that we should aim for 0.26
Comment: Doing what you did doesn't chance the markov process?
Answer: Your main confusion is that you appear to not be correctly identifying the difference between the functional and the target distribution. I would say your target distribution appears to be a simple uniform distribution, while the functional is the fancy guy. Confusingly, your functional involves a gamma density.
MCMC gives you an estimate of an expectation. Your expectation is this:
$$
\int_0^\infty \chi_{[1,2]}(x)\Gamma(C,x)\left|\cos(R x)\right| \, dx = E_X[h(x)]
$$
where $h(x) = \frac{1}{\Gamma(c)}x^{c-1}e^{-x} \left|\cos(R x)\right|$ is the function that your func evaluates, and $X \sim \text{Uniform}(1,2]$. $h$ is the functional, and $\text{Uniform}(1,2]$ is the target distribution.
Metropolis-Hastings wouldn't be my first choice for this problem, but you seem to be using random walk Metropolis-Hastings. I think this because you are proposing samples like this:
$$
X^*\mid X_{t-1} \sim \text{Normal}(X_{t-1}, 1).
$$
You call $X^*$ new, and $1$ step (this name confuses me, but that's not that important).
You can write your target distribution as $\pi(x) = \chi_{[1,2]}(x)$, and this makes your acceptance ratio
$$
a(x_{t-1},x^*) = \min(1, \pi(x^*)/\pi(x_{t-1})).
$$
Note that you are incorrectly calculating this with min(1,prox/ant). You are using the $h$ function here, when you shouldn't be (if you're targeting this uniform distribution). Instead, use this code (yours with minimal changes)
from scipy.stats import gamma,norm
import numpy as np
import matplotlib.pyplot as plt

def sampler(N):
    step=1
    old=1.5
    i=0
    points=[]
    while i<N:
        points.append(old)
        ant= 1 if 1 < old <= 2 else 0
        inov=norm.rvs(0,step)
        new=old+inov
        prox = 1 if 1 < new <= 2 else 0
        alphah=min(1,prox/ant)

        if alphah>np.random.uniform():
            old=new
        i+=1
    points.append(old)
    return points

Your mc function appears to be correct. It is calculating
$$
N^{-1} \sum_{i=1}^{N} h(x_t)
$$
where $x_t$ are all your samples (which you are calling points).
Finally, yes, your answer is valid as well. You are likely reversing the argument order in the gamma.pdf function.
Comment: How do you know that the target distribution is the Uniform(1,2) ?
Comment: It doesn't have to be. It could also be a gamma distribution. In that case the functional would only be the trigonometric part and the indicator function multiplied together. If you decided to go this route, you would have to change the code in different ways.
Comment: But taking the gamma as target distribution the integral must be the same right?
Comment: As long as you change the functional accordingly.
Comment: Known that $g \propto \chi_{[1,2]}h(x) $ the integral of g is calculated the way I calculated in my post right? And even knowing this proportionality the target function can be the uniform?
Comment: Targeting the uniform does not make sense since you can easily sample from it. I think you should target the function proportional to gamma times cosine
Comment: @Giiovanna perhaps you mean that one should just use regular Monte Carlo. I agree with that; this is what I was alluding to when I said "Metropolis-Hastings wouldn't be my first choice for this problem." In any event, this is a self-study question, so I figured my role was to help with the understanding, not to advise on superior methods. Also note that targeting that would require you to find the normalizing constant of that unnormalized distribution.
Comment: @Taylor why would you have to find the normalizing constant? If $g(x) = A \Gamma(C,x)|cos(Rx)|$ and $h(x) = \chi[1,2](x)$, then the Metropolis $\alpha$ does not depends on $A$ nor the MC integral estimative $1/N \sum h(x) $. What am I not seeing?
Comment: @Giiovanna you do not need normalizing constants to evaluate the acceptance ratio in the algorithm, but once you have that running, you still have samples from a normalized $g(x)$. If you take sample averages, then you will be estimating an expectation with respect to this normalized distribution, which means you will be estimating a scaled version of what OP is after. If you want to discuss this further, we should probably start do that somewhere else in order to avoid making the comment section too long.
Comment: @Taylor the question is stated as following: Calculate $I = \int h(x) g(x) dx$ via MCMC where $h(x) = \chi[1,2] (x)$ and $g(x) \propto \Gamma(C,x) |cos(Rx)|$. it seems like the intention is to sample from $g$, which is difficult since we don't know it's normalizing constant. Also, one of the questions is why we don't have to know it's constant, and I think is due the cancelation in $alpha$ formula.
Comment: @Taylor alright. Thank you for the help.

MCMC algorithm going wrong


What is the "unit axis"?

Question: As the highlighted text in the screenshot, what is the unit axis? And what mathematical properties the three coordinates X, Y, Z have?
Comment: Isn't it the axis on which we want to rotate the object having unit length, like striking a pencil in the object and rotating around it
Answer: It means a vector of length 1.

What is the "unit axis"?


Copy/Paste data to other existing workbook on timer

Question: I currently have this VBA -
Sub StartTimer()

Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"

End Sub

Sub AutoCalc()

Application.CalculateFull

Sheets("Kine").Range("B603:E603").Copy _
Destination:=Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1")

Application.OnTime Now + TimeValue("00:00:15"), "AutoCalc"

End Sub

The .OnTime command is working perfectly without the Copy/Paste section, which is great.
This gives me a list of values from an SQL query that will auto-update, as well as an average at the bottom of each column of values.
I'm trying to set this up so that the average will automatically be added onto columns in Minitab but I believe that the Macro is stopping the Auto-Update in Minitab.
So my idea is to copy-paste the averages into an Excel Workbook that has no macros of its own and then link that to Minitab.
Have I put in the Copy-Paste code wrong or is there some issue with where the macros need to be stored and how?
Quick Edit - I should add that current code gives "Run-Time Error 9, Subscript out of range" and highlights copy/paste code.
Comment: You need to have the other Excel file open for this to work, check [How to copy data from another workbook (excel)?](http://stackoverflow.com/questions/482717/how-to-copy-data-from-another-workbook-excel) for the same problem, and a solution.
Comment: I'm exploring answers in your posted thread but it doesn't seem to be the same issue.  Both Excel files remain open.  I've tried setting up the cells as an External Reference in the new Workbook, but i think there's an issue due to the values being updated via a macro.  This is why I'm trying to figure out the Copy method, just not sure why it won't work.
Comment: Does the file format of the destination workbook matter?
Comment: You should be able to paste into a variety of different file formats, but you need to address it correctly in your VBA, i.e. you can't expect it to paste into `AutoImportAverages.xlsx` if the workbook is actually called `AutoImportAverages.xls`.
Comment: I'm definately using the correct extension.  I've even tried renaming the Workbook.
Answer: I've found the solution.
My destination workbook was open in a separate window so the source wasn't recognising it as being open.  Bit of a nightmare!
It's necessary to have both workbooks open in the same instance of Excel.
Additionally, my original paste code only pasted "#REF".  I have changed that to -
Workbooks("AutoImportAverages.xlsx").Worksheets("AvgKine").Range("B1:E1").PasteSpecial xlValues

Works much better.
One more thing, in case anybody might find it useful.  The source workbook must be active in order to carry out the auto-update.
Adding below line sorted out most issues though its still a work in progress -
ThisWorkbook.Activate


StringReplace x Array

Question: Tenho o seguinte código:
 Nomes: array[1..10] of string=('Tadeu','Joao','Maria','Ana','Juca','leticia');
 StringReplace(Edit1.Text, Nomes, 'Bloqueado', [rfReplaceAll]);

Preciso fazer com que o StringReplace procure qualquer nome que esteja dentro do array e substitua. Aonde estou errando ?
Comment: Ok obrigado @TiagoSilva pela dica.
Answer: Pelo o que consegui entender do que você apresentou, precisa mudar no vetor o nome digitado no edit pelo valor 'Bloqueado'.
Vai ter que fazer registro por registro do vetor.
var
  I: integer;
begin
  for I := Low(Nomes) to High(Nomes) do
    Nomes[I] := StringReplace(Nomes[I], Edit1.Text, 'Bloqueado', [rfReplaceAll]);
...
end;

StringReplace x Array


Guidance for picking likelihood in Bayesian analysis

Question: I'm doing a Bayesian analysis for a time series response and wonder whether it is possible to get the likelihood function without making distributional assumptions. I suppose my response is log-normal, but what if I do not want to make distributional assumptions?
My setting is a multi-armed bandit problem, so while my pay-off is log-normal, I could also define a discrete variable whether the pay-off increased from the last period to the current period. Then I would be in a binomial setting.
Hence, it is not obvious which likelihood to go for.
Is there a way to let the data decide what the likelihood is? So some form of non-parametric likelihood?
Comment: Have a look at http://en.wikipedia.org/wiki/Marginal_likelihood#Bayesian_model_comparison and let me know if this did not answer your question.
Comment: You can decide based on the marginal likelihood: Define as many different likelihood functions as you like; and select the most plausible ones based on their marginal likelihood (also known as evidence).
Comment: Thanks, Summit. Can you elaborate what you mean by choosing based on the marginal Likelihood? What properties am I evaluating?
Answer: The choice as posed—namely, between a log-normal continuous variable and a discrete binomial one—seems distinct from that of whether to make distributional assumptions. In other words, whether the distribution is log-normal or binomial, it carries assumptions.
To the question of whether continuous or discrete, I would refer you to two posts by Andrew Gelman, arguing for modeling the continuous variable is more efficient, and more informative. A short snippet:

The key is that vote differential is available, and a simply performing a logit model for wins alone is implicitly taking this differential as latent or missing data, thus throwing away information.

In your example, by limiting your model to whether pay-off changed, you ignore how much it changed.
Choosing which distribution (and corresponding likelihood function) amounts to choosing a model, on which topic I'd direct you to the accepted answer to this question.


How can we compare Argentina's 1990 and 2022 World Cup squads?

Question: Why couldn't Argentina win the 1990 FIFA World Cup? What was the main issue with that squad?
What was the main difference between Argentina's 1990 and 2022 World Cup squads?
Comment: This is a totally opinion based question. I've seen the 1990 world cup in tv as an argentine. Word in Argentina was "Maradona is old". He was 30, Messi in the 2022 world cup was 35. Maradona was indeed out of his peak and wasnt the best player in the world anymore, which led Argentina to win in 1986 when he was. Messi numbers  of assists and goals before the 2022 world cup are in the level of  the best player in the world, along with Haaland. Argentina style of play, at least since 1986, relies on having a genius playmaker, since Maradona left in 1994, we were looking for one to replace him
Comment: As I said in the former comment, since at least 1986 Argentina style of play depends on having a genius playmaker. Since Maradona left in 1994, we tried to replace him with Gallardo, Ortega, Aimar, Riquelme. All of them failed. Messi also had a hard time replacing him in the argentine squad until at least 2014 when we reached the World Cup finals, and later 2 Copa America finals in a row.
Answer: Argentina's main problem in 1990 was that they simply didn't score enough goals. In their seven matches at the tournament they scored only five goals, one of the lowest tallies of any team ever to reach the final, and became the first finalists not to score after mustering just one shot on goal in the entire match.
According to Wikipedia, this was the result of an ultra-defensive strategy, rather than a lack of attacking talent:

The South Americans failed to put together a coherent attacking strategy and lost the ball frequently. Instead, they focused on defending at all costs, knowing they would have the advantage if they managed to reach the penalty shoot-out, as they had already advanced twice in the tournament [against Yugoslavia and Italy] by this means.

This strategy ultimately backfired, as the winning goal was scored in the 85th minute, by which time they were already down to ten men and had little hope of equalising even if they started attacking more.
Compare that to Argentina in 2022, who scored fifteen goals in seven matches (seven of which were scored by Lionel Messi), managed twenty shots on goal during the final, and only went to penalties after conceding a late equalizer, rather than penalties having been the plan all along.


How to add css transitions to js scroll in header?

Question: I have a header that appears when the page scrolls down. I am trying to add css transitions to make it fade in and out because I've read that using javascript for fading is not as efficient.
.header-wrapper {
    top:0;
    left:0;
    right:0;
    position: fixed;
    display:none;
    height: 60px;
    border-top: 1px solid #000;
    background: red;
    z-index: 1;
}
.header-wrapper.active {
     display:block;
}
.header {
    background-color:#000;
    height:80px;
}

Here is the js fiddle
$(window).scroll(function () {
    var y = $(window).scrollTop();

    // if above 300 and doesn't have active class yet
    if (y > 300 && !$('.header-wrapper').hasClass('active')) {
        $('.header-wrapper').addClass('active');

    // if below 300 has still has active class
    } else if(y <= 300 && $('.header-wrapper').hasClass('active')) {
        $('.header-wrapper').removeClass('active');
    }
    });

Comment: http://www.w3schools.com/css/css3_transitions.asp
Comment: http://stackoverflow.com/questions/3331353/transitions-on-the-display-property
Comment: You can transition numeric properties, in your case: `opacity`.
Answer: Transitions are added with the css3 property transition.
One common reason for confusion: you can only transition properties that accept numeric values.  Thus, you can't transition between display: block and display: none.
However you can transition between opacity: 0 and opacity: 1 with:
transition: 0.5s opacity
That would look something like this:
.bottomMenu {
    ...
    opacity: 0;
    transition: 0.5s opacity;
    ...
}
.bottomMenu.active {
     opacity: 1;
}

For your particular case, I might recommend transitioning the height between 0 and 60px.
For that you can use:
transition: 0.5s height
So:
.bottomMenu {
    ...
    height: 0;
    transition: 0.5s height;
    ...
}
.bottomMenu.active {
     height: 80px;
}

Comment: This seems like it would work for making the header appear and disappear with a fade, however I'm already achieving that with my javascript. See edit. I am just wanting to keep it how it is and ease it in and out...
Comment: As it says in my comment above, you can't transition from "Display: block" to "Display: none" you have to use "Opacity: 0" to "Opacity: 1"
Answer: To animate the opacity the element must be visible. So remove the display:none and make it fully transparent (opacity:0). You can then use CSS transitions to animate the opacity when the classname changes:
.bottomMenu {
    ...
    display:block;
    opacity: 0;
    transition: opacity .25s ease-in-out;
    -moz-transition: opacity .25s ease-in-out;
    -webkit-transition: opacity .25s ease-in-out;
}
.bottomMenu.active {
     opacity:1
}

http://jsfiddle.net/oL9ro4gL/6/
Furthermore, you're not restricted to just animating the opacity:
.bottomMenu {
    ...
   transition: all .25s ease-in-out;
}
.bottomMenu.active {
    opacity:1;
    height: 60px;
    background-color: blue;
    transform:rotate(180deg);
    color:white;
    font-size:40px;
    etc...
}

http://jsfiddle.net/oL9ro4gL/8/
Answer: Unfortunately, you can't animate the display property. See this question and its suggestions for workarounds.