Como posso criar uma SeekBar

Question: Quero criar uma barra que  mostra a reprodução da musica. Criei uma SeekBar apra que quando o audio começa a reproduzir mostre o seu tempo de duranção e quanto tempo passou depois de o iniciar ...
SeekBar myseekbar;
myseekbar = (SeekBar) findViewById(R.id.myseekbar);
myseekbar.setMax(mp1.getDuration());
new Timer().schedule(new TimerTask() {
      @Override
      public void run() {
            myseekbar.setProgress(mp1.getCurrentPosition());
      }
},0,1000);

Utilizei esse codigo mas nao da
Como posso fazer para chegar a esse fim.
Comment: Voce tem que pegar o tempo executado do Media player e passar pro SeekBar
Comment: e como eu faço para o pegar?
Answer: Esse exemplo esta usando Android Annotation.
Esta é a activity de um player de um aplicativo de Podcast
package br.com.podcast.podcast.view;

import android.content.ComponentName;
import android.content.ServiceConnection;
import android.graphics.Color;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Extra;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.ViewById;

import br.com.podcast.podcast.R;
import br.com.podcast.podcast.domain.Episodio;
import br.com.podcast.podcast.domain.Podcast;
import br.com.podcast.podcast.service.background.player.PlayerBinder;
import br.com.podcast.podcast.service.background.player.PlayerCallBack;
import br.com.podcast.podcast.service.background.player.PlayerService;
import br.com.podcast.podcast.service.background.player.PlayerService_;
import br.com.podcast.podcast.util.DateUtils;
import br.com.podcast.podcast.util.InternetUtil;
import br.com.podcast.podcast.util.Load;
import jp.wasabeef.picasso.transformations.BlurTransformation;
import jp.wasabeef.picasso.transformations.ColorFilterTransformation;

import static android.os.Build.VERSION;
import static android.os.Build.VERSION_CODES;

@EActivity(R.layout.activity_player)
public class PlayerActivity extends AppCompatActivity implements ServiceConnection, PlayerCallBack {
    private PlayerService playerService;
    private boolean connected = false;
    private Load load;

    @ViewById
    ImageView background;
    @ViewById
    TextView title;
    @ViewById
    ImageView logo;
    @ViewById
    TextView progressTime;
    @ViewById
    TextView duration;
    @ViewById
    SeekBar progressBar;
    @ViewById
    ImageView playPause;
    @ViewById
    TextView titleEpisodio;
    @ViewById
    Toolbar toolbar;
    @Extra
    Podcast podcast;
    @Extra
    Integer selectedPodcast;
    @Extra
    Boolean autoPlay;

    @AfterViews
    void afterViewPlayerActivity(){
        PlayerService_.intent(getApplication()).start();
        doBindService();
        load = new Load(this, Load.Color.DARK);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        load.show();
    }

    private void init(){
        playPause.setSelected(true);
        resetSeekBar();
        title.setText(podcast.getNome());

        //Imagem do album
        int size = (InternetUtil.screenWidth(this) / 5) * 4;
        Picasso.with(this).load(podcast.getImage()).resize(size,size).placeholder(R.drawable.holder_quad).into(logo);

        //Imagem de fundo
        Transformation blur = new BlurTransformation(this, 12, 4);
        Transformation filterColor = new ColorFilterTransformation(Color.argb(130, 0, 0, 0));
        Picasso.with(this).load(podcast.getImage()).transform(blur).transform(filterColor).into(background);

        if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.BLACK);
        }

        playerService.setPodcast(podcast, selectedPodcast, autoPlay);
    }

    private void resetSeekBar(){
        progressBar.setProgress(0);
        progressBar.setSecondaryProgress(0);
        progressTime.setText("00:00");
        duration.setText("00:00");
    }

    //Clicks Controll

    @Click
    void back(){
        if(connected){
            playerService.playPreview();
        }
    }

    @Click
    void back30(){
        if(connected) {
            int newPosition = playerService.getPosition() - (30 * 1000);
            if(newPosition < 0 ){
                playerService.seekTo(0);
                onProgress(0);
            }else{
                playerService.seekTo(newPosition); //30 segundos
                onProgress(newPosition);
            }
        }
    }

    @Click
    void playPause(){
        if(connected) {
            if (playerService.isPlaying()) {
                playerService.pause();
            } else {
                playerService.play();
            }
            playPause.setSelected(!playerService.isPlaying());
        }
    }

    @Click
    void nex30(){
        if(connected){
            int newPosition = playerService.getPosition() + (30 * 1000);

            if(newPosition > playerService.getDuration()){
                playerService.playNext();
                onProgress(playerService.getDuration());
            }else{
                playerService.seekTo(newPosition); //30 segundos
                onProgress(newPosition);
            }
        }
    }

    @Click
    void next(){
        if(connected){
            playerService.playNext();
        }
    }

    //Listeners player

    @Override
    public void onProgress(int progress) {
        progressBar.setProgress(progress);
        progressTime.setText(DateUtils.toClockTimestampInSecondsFormat(playerService.getPosition()));
    }

    @Override
    public void onPrepare(Episodio episodio) {
        load.hide();
        progressBar.setMax(playerService.getDuration());
        duration.setText(DateUtils.toClockTimestampInSecondsFormat(playerService.getDuration()));
        progressBar.setProgress(playerService.getPosition());
        progressBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                if (connected) {
                    progressTime.setText(DateUtils.toClockTimestampInSecondsFormat(progress));
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                playerService.stopProgressUpdate();
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (connected) {
                    playerService.seekTo(seekBar.getProgress());
                    playerService.startProgressUpdate();
                }
            }
        });
        progressTime.setText(DateUtils.toClockTimestampInSecondsFormat(playerService.getPosition()));
        playPause.setSelected(!playerService.isPlaying());
        titleEpisodio.setText(episodio.getTitulo());
    }

    @Override
    public void onPlayer() {
        playPause.setSelected(!playerService.isPlaying());
    }

    @Override
    public void onPause_() {
        playPause.setSelected(!playerService.isPlaying());
    }

    @Override
    public void onEpisodioChange(Episodio episodio) {
        titleEpisodio.setText(episodio.getTitulo());
    }

    @Override
    public void onError(String message) {
        load.hide();
        Toast.makeText(this, message, Toast.LENGTH_LONG).show();
    }

    @Override
    public void onBufferInit() {
        load.show();
    }

    @Override
    public void onBufferFinish() {
        load.hide();
    }

    @Override
    public void onBufferUpdate(int percent) {
        progressBar.setSecondaryProgress((int)(playerService.getDuration() * (percent / 100.0)));
    }

    @Override
    public void onServiceConnected(ComponentName componentName, IBinder binder) {
        playerService = ((PlayerBinder) binder).getService();
        playerService.setPlayerCallBack(this);
        init();
    }

    @Override
    public void onServiceDisconnected(ComponentName componentName) {
        playerService = null;
        connected = false;
    }

    private void doBindService(){
        try{
            if(!connected){
                bindService(PlayerService_.intent(getApplication()).get(), this, 0);
                connected = true;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    private void doUnbindService(){
        try{
            if(connected){
                unbindService(this);
                connected = false;
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    protected void onDestroy() {
        doUnbindService();
        super.onDestroy();
    }

    @OptionsItem(android.R.id.home)
    void homeSelected() {
        finish();
    }
}
}

Esta Classe é um Service que sera nosso PlayerService. O service garante que o audio vai continuar sendo executado mesmo que a aplicação seja colocada em segundo plano.
package br.com.podcast.podcast.service.background.player;

import android.app.Service;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Handler;
import android.os.IBinder;
import android.os.PowerManager;

import org.androidannotations.annotations.EService;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

import br.com.podcast.podcast.domain.Podcast;
import br.com.podcast.podcast.domain.helper.FinishGeneralEventBusHelper;
import br.com.podcast.podcast.domain.helper.PlayerCommandEventBusHelper;
import br.com.podcast.podcast.domain.helper.PlayerStatusEventBusHelper;
import lombok.Setter;

@EService
public class PlayerService extends Service implements MediaPlayer.OnPreparedListener, MediaPlayer.OnErrorListener, MediaPlayer.OnCompletionListener,
        CallbackEventsCallAndPhone, MediaPlayer.OnInfoListener, MediaPlayer.OnBufferingUpdateListener, RemoteControlListener {
    private final IBinder playerBinder = new PlayerBinder(this);
    private MediaPlayer player;
    private int currentPosition = 0;
    private final Handler handler = new Handler();
    private boolean isMyPause = false;
    private final Runnable callBackRunnable = new Runnable() {
        public void run() {
            if(player.isPlaying() && playerCallBack != null)
                playerCallBack.onProgress(player.getCurrentPosition());
        }
    };
    private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    private ScheduledFuture<?> callBackHandle;
    private RemoteControlNotification remoteControlNotification;
    private Podcast podcast;

    @Setter
    private PlayerCallBack playerCallBack;

    public void onCreate(){
        super.onCreate();
        initPlayer();
        PlayerIntentReceiver.getInstance(this, getApplicationContext()).register();
        EventBus.getDefault().register(this);
        remoteControlNotification = RemoteControlNotification.getInstance(this, this);
        remoteControlNotification.register();
    }

    private void initPlayer() {
        player = new MediaPlayer();
        player.setWakeMode(getApplicationContext(), PowerManager.PARTIAL_WAKE_LOCK);
        player.setAudioStreamType(AudioManager.STREAM_MUSIC);
        player.setOnPreparedListener(this);
        player.setOnCompletionListener(this);
        player.setOnErrorListener(this);
        player.setOnInfoListener(this);
        player.setOnBufferingUpdateListener(this);
    }

    @Override
    public IBinder onBind(Intent intent) {
        return playerBinder;
    }

    @Override
    public boolean onUnbind(Intent intent){
        playerCallBack = null;
        stopProgressUpdate();
        return false;
    }

    public void startProgressUpdate() {
        if(player.isPlaying() && playerCallBack != null){
            if (!scheduler.isShutdown())
                callBackHandle = scheduler.scheduleAtFixedRate(new Runnable() {
                    @Override
                    public void run() {
                        handler.post(callBackRunnable);
                    }
                }, 500, 500, TimeUnit.MILLISECONDS);
        }
    }

    public void stopProgressUpdate(){
        if(callBackHandle != null)
            callBackHandle.cancel(false);
    }

    public void stopService(){
        stopSelf();
    }

    public void setPodcast(Podcast podcast, Integer selectedPosition, Boolean autoPlay){
        if(this.podcast != null) {
            if(podcast.getUrl().equals(this.podcast.getUrl())){
                if(isPlaying()){
                    if(selectedPosition != null && selectedPosition >= 0 ){
                        pause();
                        jumpTo(selectedPosition);
                    }
                    playerCallBack.onPrepare(this.podcast.getEpisodios().get(currentPosition));
                }else{
                    if(selectedPosition != null && selectedPosition >= 0 )
                        currentPosition = selectedPosition;

                    if(autoPlay != null && autoPlay)
                        play();

                    playerCallBack.onPrepare(this.podcast.getEpisodios().get(currentPosition));
                }
            }else{
                this.podcast = podcast;
                if(isPlaying()) {
                    pause();
                }
                currentPosition = 0;
                resetPlayer();
            }
        }else{
            this.podcast = podcast;
            if(selectedPosition != null && selectedPosition >= 0 )
                currentPosition = selectedPosition;
            resetPlayer();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onCommand(final PlayerCommandEventBusHelper helper){
        switch (helper.command){
            case NEXT:
                playNext();
                break;
            case PAUSE:
                pause();
                break;
            case PlAYER:
                play();
                break;
            case PREVIEW:
                playPreview();
                break;
            case STOP:
                stopPodcast();
                break;
            case STATUS_REQUEST:
                break;
        }

        EventBus.getDefault().post(getStatus());
    }

    private PlayerStatusEventBusHelper getStatus(){
        PlayerStatusEventBusHelper helper = new PlayerStatusEventBusHelper();
        helper.podcast = podcast;
        helper.episodioCorrente = currentPosition;

        if(isPlaying()) {
            helper.status = PlayerStatusEventBusHelper.Status.PLAYING;
        }else {
            if (podcast != null)
                helper.status = PlayerStatusEventBusHelper.Status.PAUSED;
            else
                helper.status = PlayerStatusEventBusHelper.Status.STOPED;
        }

        return helper;
    }

    public void stopPodcast(){
        stop();
        podcast = null;
        currentPosition = 0;
        initPlayer();
    }

    //Comandos da notificação

    @Override
    public void onPlay() {
       play();
    }

    @Override
    public void onPause() {
        pause();
    }

    @Override
    public void onNext30() {
        int newPosition = getPosition() - (30 * 1000);
        if(newPosition < 0 ){
            seekTo(0);
            if(playerCallBack != null)
                playerCallBack.onProgress(0);
        }else{
            seekTo(newPosition); //30 segundos
            if(playerCallBack != null)
                playerCallBack.onProgress(newPosition);
        }
    }

    @Override
    public void onPreview30() {
        int newPosition = getPosition() + (30 * 1000);

        if(newPosition > getDuration()){
            playNext();
            if(playerCallBack != null)
                playerCallBack.onProgress(getDuration());
        }else{
            seekTo(newPosition); //30 segundos
            if(playerCallBack != null)
                playerCallBack.onProgress(newPosition);
        }
    }

    //Commandos do Player

    public void resetPlayer(){
        try{
            if(player.isLooping())
                player.stop();

            player.release();
            initPlayer();

            if(playerCallBack != null)
                playerCallBack.onEpisodioChange(podcast.getEpisodios().get(currentPosition));

            player.setDataSource(podcast.getEpisodios().get(currentPosition).getUrl());
            player.prepareAsync();
        }catch(Exception e){
            e.printStackTrace();
            playerCallBack.onError("Erro ao chamar o servidor de podcast");
        }

        remoteControlNotification.playNotification(podcast, currentPosition);
    }

    public void playNext(){
        stopProgressUpdate();

        currentPosition++;
        if(currentPosition >= podcast.getEpisodios().size())
            currentPosition = 0;

        resetPlayer();
    }

    public void play(){
        if(!player.isPlaying())
            player.start();

        if(playerCallBack != null){
            playerCallBack.onEpisodioChange(podcast.getEpisodios().get(currentPosition));
            playerCallBack.onPlayer();
            startProgressUpdate();
        }

        remoteControlNotification.playNotification(podcast, currentPosition);
    }

    public void pause(){
        if(player.isPlaying())
            player.pause();

        if(playerCallBack != null)
            playerCallBack.onPause_();

        stopProgressUpdate();

        remoteControlNotification.pauseNotification(podcast, currentPosition);
    }

    public void seekTo(int position){
        player.seekTo(position);
    }

    public void jumpTo(int position){
        pause();
        currentPosition = position;
        resetPlayer();
    }

    public void playPreview(){
        stopProgressUpdate();

        currentPosition--;
        if(currentPosition < 0)
            currentPosition = podcast.getEpisodios().size() - 1;

        resetPlayer();
    }

    public void stop(){
        player.stop();
        player.release();
        stopProgressUpdate();

        remoteControlNotification.stopNotification();
    }

    public boolean isPlaying(){
        try{
            return player.isPlaying();
        }catch (IllegalStateException e){
            e.printStackTrace();
        }

        return false;
    }

    public int getDuration(){
        return player.getDuration();
    }

    public int getPosition(){
        return player.getCurrentPosition();
    }

    //CallBacks

    @Override
    public void onPhoneOut() {
        pause();
    }

    @Override
    public void onCalling() {
        if(player.isPlaying()){
            isMyPause = true;
            pause();
        }
    }

    @Override
    public void onCallStart() {
        if(player.isPlaying()){
            isMyPause = true;
            pause();
        }
    }

    @Override
    public void onCallFinish() {
        if(isMyPause){
            isMyPause = false;
            play();
        }
    }

    @Override
    public void onBufferingUpdate(MediaPlayer mediaPlayer, int percent) {
        if(playerCallBack != null)
            playerCallBack.onBufferUpdate(percent);
    }

    @Override
    public void onCompletion(MediaPlayer mediaPlayer) {
        stopProgressUpdate();

        if(player.getCurrentPosition() > 0){
            playNext();
        }
    }

    @Override
    public boolean onError(MediaPlayer mp, int what, int extra) {
        switch (what){
            case MediaPlayer.MEDIA_ERROR_UNKNOWN:
                playerCallBack.onError("Erro no servidor");
                System.out.println("Erro no servidor");
                return true;
            case MediaPlayer.MEDIA_ERROR_SERVER_DIED:
                playerCallBack.onError("Servidor do Podcast encontra se fora do ar");
                System.out.println("Servidor do Podcast encontra se fora do ar");
                return true;
        }

        switch (extra){
            case MediaPlayer.MEDIA_ERROR_IO:
                playerCallBack.onError("Existe um problema na rede ou no Servidor do podcast");
                System.out.println("Existe um problema na rede ou no Servidor do podcast");
                return true;
            case MediaPlayer.MEDIA_ERROR_MALFORMED:
                playerCallBack.onError("Este podcast esta com audio corrompido");
                System.out.println("Este podcast esta com audio corrompido");
                return true;
            case MediaPlayer.MEDIA_ERROR_UNSUPPORTED:
                playerCallBack.onError("Erro no servidor");
                System.out.println("Erro no servidor");
                return true;
            case MediaPlayer.MEDIA_ERROR_TIMED_OUT:
                playerCallBack.onError("Servidor de podcast temporariamente indisponível");
                System.out.println("Servidor de podcast temporariamente indisponível");
                return true;
        }

        return false;
    }

    @Override
    public boolean onInfo(MediaPlayer mp, int what, int extra) {
        switch (what) {
            case MediaPlayer.MEDIA_INFO_BUFFERING_START:
                if(playerCallBack != null)
                    playerCallBack.onBufferInit();
                pause();
                break;
            case MediaPlayer.MEDIA_INFO_BUFFERING_END:
                if(playerCallBack != null)
                    playerCallBack.onBufferFinish();
                play();
                break;
        }

        return false;
    }

    @Override
    public void onPrepared(MediaPlayer mediaPlayer) {
        if(playerCallBack != null)
            playerCallBack.onPrepare(this.podcast.getEpisodios().get(currentPosition));

        play();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onFinishAplication(FinishGeneralEventBusHelper helper){
        stop();
        onDestroy();
    }

    @Override
    public void onDestroy() {
        PlayerIntentReceiver.getInstance(this, getApplicationContext()).unregister();
        EventBus.getDefault().unregister(this);
        remoteControlNotification.stopNotification();
        remoteControlNotification.unregister();
        stopForeground(true);
    }
}

Comment: Ignore o código que esta sobrando. Tem partes referente a notificação, remoção do fone, atender ligação no meio do podcast e etc. Tem diversos tratamentos que tem que ser considerados.

Como posso criar uma SeekBar


Using init_printing to print multiple Matrices in a in Jupyter notebook cell

Question: I have two matrices that want to print in one jupyter cell using init_printing, When I try printing them both only the last one gets printed.
import numpy as np
from sympy import init_printing, Matrix

L = np.array([[1, 0, 0], [2, 1, 0], [-1, 0.5, -1]])
b = np.array([2, 4, 2])

Matrix(L)
Matrix(b)

Answer: that's how jupyter-notebook cells work, only the last returned output will be shown. If you want both, do:
solution 01
Matrix(L), Matrix(b)

Output:

solution 02
Now, if you do insist to print one after another in the next line, you can use the display module like this:
from IPython.display import display
import numpy as np
from sympy import Matrix

L = np.array([[1, 0, 0], [2, 1, 0], [-1, 0.5, -1]])
b = np.array([2, 4, 2])

display(Matrix(L))
display(Matrix(b))

Output:
Comment: Is there any other way to print them below each other without parentheses  or comma?
Comment: well, you can use display module. I am adding that answer too.


JtextPane with a touch interface

Question: I'm developing a touch based windows 7 app using a tuio client-server setup and a touch library that extends multitouch 4 java.
One of the functionality I'm struggling with is to enable text highlighting when using touch.
I display a simple txt file using a JTextPane to display the text, the highlighting is done by through a drag action.
I get the clicked position where the drag event starts and then when it stops and tried to convert those coordinates to the text panel's space but I get different values from the ones that I should have, usually before the actual text.
The code I'm using to display the document is the following:
<code>            //Create the JDialog that is the container of it
            window = new JDialog(parent);
            window.setUndecorated(true);

            //Create the JTextPane
            text = new JTextPane();
            text.setPage(newFile.toURI().toURL());
            text.setEditable(false);
            text.setHighlighter(null);

            //ScrollPane that will be used to display the text
            JScrollPane scroll = new JScrollPane(text);
            scroll.setPreferredSize(new Dimension(500, 700));

            window.getContentPane().add(scroll, BorderLayout.CENTER);

            window.pack();
            window.setVisible(true);
            window.validate();
</code>
Where the JDialog parent is the main display component used in my app.
The drag is handled as follows:
<code>    @Override
public boolean processGestureEvent(GestureEvent ge) {
    if((ge instanceof DragEvent) && this.component.isHighlight())
    {
        tapCount=0;
        if(this.component.isHighlight())
        {
            //do highlighting
            DragEvent drag = (DragEvent) ge;
            switch (drag.getId()) {
            case GestureEvent.GESTURE_STARTED:
                Point start = drag.getFrom();
                Point calcStart = new Point(start.x - compPosition.x, start.y - compPosition.y);
                startPos = this.textDisplay.viewToModel(calcStart);

                break;

            case GestureEvent.GESTURE_ENDED:
                Point end = drag.getTo();
                Point calcEnd = new Point(end.x - compPosition.x, end.y - compPosition.y);
                endPos = this.textDisplay.viewToModel(calcEnd);

                System.out.println("I have this positions:" + startPos + "/" + endPos);
                System.out.println("Should have " + this.textDisplay.getSelectionStart() + "/" + this.textDisplay.getSelectionEnd());
                System.out.println("And the text is: " + this.textDisplay.getText().substring(startPos, endPos));
                break;
            case GestureEvent.GESTURE_CANCELED:
                startPos = 0;
                endPos = 0;
                break;
            }
        }
        return true;
    }
</code>
In which the compPosition is the JDialog's position that holds the text pane. I'm simulating touch with the mouse so the correct text position for the highlighting I'm getting from the built-in highlighting functionality of the text pane with mouse.
Is the problem because of the JDialog and the JScroll pane that somehow skews the conversion?
The coordinate system for the point's I get from the touch are with the origin in the top left corner of the screen and the text pane's coordinate system origin is in the same top lef corner.
Any ideas on how can I solve the problem? Any suggestions are appreciated.
LE:
I was doing something wrong in that I was adding the gesture processor when I initialized the component and it's position was (0,0) and only afterwards I moved it where I wanted to.
I changed the position calculations as follows:
<code>    Point calcStart = new Point(start.x - this.component.getLocation().x, start.y -this.component.getLocation().y);
</code>
passing instead a reference to the actual component and getting the location when needed.
Answer: try instead of
<code> Point calcStart = new Point(start.x - compPosition.x, start.y - compPosition.y);
</code>
to use
<code> Point calcStart = new Point(start.x, start.y);
</code>
i wonder how it gonna end, so give us some value of what you get
Comment: I tried that and weirdly it gave the same result ... so I'm guessing somehow the location was set to (0,0) , which might make sense since I initialize the gesture processor when I create the component and it's initial position being 0,0, only afterwards I move it to where I want it. Now I pass a reference of the component to the gesture processor and look for the location when I need it: Point calcStart = new Point(start.x - this.component.getLocation().x, start.y - this.component.getLocation().y); This apparently works so far. Thank you for the help :)

JtextPane with a touch interface


How should I take the sign or check isnull() of a column in pandas eval/query function?

Question: Given a pandas dataframe, how should I do sth like the following?
df.eval('B * sign(A)')
df.query('A.notnull()')

It is not letting me because sign(A) and A.notnull() is not recognized.
Comment: Please add a small example dataset.
Answer: eval can only handle a small subset of expressions and sign is not included.
However, with some mental gymnastics you can re-create the sign function. Typically you could just use A/abs(A) to reproduce np.sign, but for 0 that implementation returns NaN, while np.sign(0) == 0. Instead we will work with Boolean Series to get the 0 correct. If you want to return NaN
for sign(NaN), consistent with np.sign, we need even more of a hack.
# B * np.sign(A)
df.eval('B * ((A != 0) - 2*(A < 0))')

# With proper NaN handling
df.eval('B * ((A != 0) - 2*(A < 0)) * arcsin((A != A) + sin(1))')

# Sample Data
          A         B
0 -1.588994  0.856149
1  0.000000  1.461134
2  1.028567 -0.610840
3  0.869345 -1.707041
4 -0.041404  1.322980
5 -0.287458  1.047247
6 -0.628855  1.235757
7 -1.224231  0.863408
8  2.430871  1.509066
9  0.602590  0.529180

(df.eval('B * ((A != 0) - 2*(A < 0))') == df.B*np.sign(df.A)).all()
True

Answer: Try:
import numpy as np

df['B']*np.sign(df['A'].to_numpy())

df[~df['A'].isna()]

As in the comments, remark that np.sign(0)=0


Consulta SQL para campos Chave estrangeira null

Question: Tenho uma relação que possui a tabela:
maoObra
(
        id*
    ,   descricao
    ,   valor
    ,   ...
)

E a tabela:
pecas
(
        id*
    ,   nome
    ,   valor
    ,   ...
)

Tenho uma outra tabela:
ItensServico
(
        id*
    ,   data
    ,   pecas_id
    ,   maoObra_id
    ,   ...
)

Onde os atributos peca_id e maoObra_id são chaves estrangeiras fracas na tabela de ItensServico.
Na tabela ItensServico posso receber uma tupla que, ou é da tabela pecas ou da tabela maoObra e nunca das duas, ou seja, quando em uma tupla eu inserir o atributo peca_id, o atributo maoObra_id será null e o inverso também é verdadeiro.
Eu preciso de uma consulta que retorne a tupla, mas que retorne os atributos de uma tabela quando seu campo chave estrangeira NÃO for nulo.
Answer: Talvez assim consiga resolver o seu problema:
SELECT      ISE.id
        ,   ISE.data
        ,   Id          = ISNULL(MO.id, PE.id)
        ,   Descricao   = (CASE WHEN MO.id IS NOT NULL THEN MO.descricao    ELSE PE.nome    END)
        ,   Valor       = (CASE WHEN MO.id IS NOT NULL THEN MO.valor        ELSE PE.valor   END)
FROM        ItensServico    ISE
LEFT JOIN   maoObra         MO  ON MO.id = ISE.maoObra_id
LEFT JOIN   pecas           PE  ON PE.id = ISE.pecas_id

Comment: Obrigado João, era isso que precisava.


Trying to get IMCE with Filefield Sources working (no Upload button in popup)

Question: I've got a clean install of D6.22, CCK, Filefield, ImageField, Filefield Sources and IMCE. No other modules.
When i go to http://mysite.com/imce it all works perfectly (see screenshot).
But when i try to implement an image field with IMCE as a filefield source, i can click the "browse" link and the IMCE popup comes up, but the only button i get at the top of the popup is "Insert file". No upload, no delete, no resize etc. Here's the screenshot.
I see from the popup's page info that its URL is...
http://mysite.com/filefield/imce/page/field_pics?app=pics|url@edit-field-pics-0-imce-path
(the image field's human name and machine_name are both "pics").
I'm logged in as user-1, and the IMCE user-1 profile is looking good.
Also, there are no javascript errors, and if inspect the popup with Firebug, the upload button is there, but hidden. If I unhide it and click the button, I get the alert "You can not perform this operation". So it seems to be a permissions problem rather than a javascript problem.
I'm sure i've missed something so simple nobody's bothered to mention it in any of the docs. Can someone enlighten me?!
Answer: Ah! Like, Filefield Sources no longer lets IMCE do anything except browse your files and select one of them (for security reasons i think). That's what the situation seems to be, though i can't find anywhere that says that explicitly.
Luckily there's a patch which reinstates the missing functionality...
http://drupal.org/node/877452#comment-4499144
The maintainer of Filefield Sources links to that patch from the module's project page, so it's a recommended patch.
All functioning now :) Can i have my Sunday back now please?
++Thanks to ufku for providing the patch.
Answer: After installing the patch (thank's to chichilatte) you have to check the file browser mode "Full" under field settings -> file sources -> imce file browser settings. Otherwise you can not rename, delete or upload a file.


Content type export broken in PHP 5.4

Question: I am trying to upgrade my server from PHP 5.3 to 5.4. When accessing the Content Type export form in CCK with D6 the form does not display.
This issue is on first display of the form, not just on submit (as I have seen other questions online).
Here is the bad form.

Here is the correct form (on PHP 5.3).

I don't have any errors being thrown to the error_log or watchdog, and I am using Pressflow 6.26.
Comment: Are you sure you have error reporting set to `E_ALL` ?
Comment: yes, I've set reporting to E_ALL without any errors being reported
Answer: Line 623 of modules/cck/modules/content_copy/content_copy.module needs to be changed from:
function template_preprocess_content_copy_export_form($vars) {

to
function template_preprocess_content_copy_export_form(&$vars) {

https://drupal.org/node/2230081

Content type export broken in PHP 5.4


Number of FLOPS required to solve Sherman-Morrison formula for matrix inversion

Question: I have the expression for the inversion of a matrix with the Sherman-Morrison formula as follows (given that $A^{-1}$ is known).
$$B^{-1} = A^{-1} + \frac{A^{-1}\mathbf{uv^*}A^{-1}}{1-\mathbf{v^*}A^{-1}\mathbf{u}}$$
Now I'm trying to figure out how many FLOPS this would require to calculate. This is what I get from looking at it:

one matrix-vector multiplication: $2m^2-m$
one vector-matrix multiplication: $2m^2-m$
one outer product: $m^2$
one Sesquilinear form multiplication ($\mathbf{v^*}A^{-1}u$): $2m^2 +m-1$
one subtraction: $1$ and
one matrix addition: $m^2$

Totaling $8m^2-m$
But everywhere I look it says that the Sherman-Morrison formula is supposed to be able to be calculated in $\sim3m^2$ FLOPS.
What am I missing here?
Comment: @J.M. But with this definition, already $\mathbf{A^{-1}u}$ and $\mathbf{v^*A^{-1}}$ will take $\sim4m^2$ "flop"s. The number $\sim3m^2$ makes most sense when counting just the multiplications. And I also think that the number of multiplication is a good "abstract" effort measure. A "flop" is not necessarily a more accurate "practical" measure, since the number of "load"s and "store"s is often more important than the number of additions.
Comment: True, nobody really takes flops as the be-all and end-all in matters of measuring efficiency, @Thomas. (And data-shuttling can take more time than the arithmetic.) But how are you counting flops? Are you assuming that the explicit $\mathbf A^{-1}$ is being multiplied or (the one used in practice!) are you counting the flops for a backsubstitution from a decomposition of $\mathbf A$?
Comment: FLOPS (floating point operations per second) is a measure of a computer's performance. You don't measure the performance of an algorithm in FLOPS. So I guess that the Sherman-Morrison formula is supposed to take $\approx 3m^2$ multiplications, not FLOPS.
Comment: @Thomas: more likely, Fredrik just wanted a *flop*, [in the sense of Golub and Van Loan](http://books.google.com/books?id=mlOa7wPX6OYC&pg=PA18).
Answer: First off: you do not solve the Sherman-Morrison-Woodbury formula; the intention of this is to allow one to easily solve a set of linear equations when a rank-1 correction has been applied.
Secondly: it looks to me that you're not exploiting the occurrence of common subexpressions. It might aid you to see the computational savings you might accrue if you write SMW this way:
$$(\mathbf A+\mathbf u\mathbf v^T)^{-1}=\mathbf A^{-1}-\frac{\mathbf w(\mathbf A^{-1}\mathbf v)^T}{1-\mathbf v\cdot\mathbf w},\qquad \mathbf w=\mathbf A^{-1}\mathbf u$$
So let's see... assuming you have both the decomposed matrix and the inverse available (you do know that explicitly computing the inverse before multiplying to a vector can be unsound to do, yes?), you compute $\mathbf A^{-1}\mathbf u$ and $\mathbf A^{-1}\mathbf v$, take an outer and an inner product... well, you do the flop accounting. :)
Comment: Following this and doing the flop accounting I got to $\sim 6m^2$ FLOPS and I just asked my professor about it and he agreed it should be $\sim 6m^2$ FLOPS. The statement I found online that it should take $\sim 3m^2$ FLOPS is most likely related to them just counting the multiplications like stated in the comments on the question.


Is it safe to protect the images only with a watermark image style?

Question: I'm building a website for selling stock photography and I'm looking for a way to protect the images. Here's what I'm using right now:

Imagecache Actions to watermark photos
Private file system for uploading all images
The images are displayed on the front only through image styles with watermarking.

Question: is this enough and are there any ways users could bypass this (e.g. by directly trying to access certain image style)?
Thanks!
Comment: I assume you have other image styles, other than the ones you are watermarking?
Comment: Yes, that is correct. However, I will not use any of the other styles to display these images.
Answer: This is probably not as secure as you would like it to be.
Basically if a user has access to view the image they can view any derivative image (image style), so long as they can work out the url to that derivative.
There is a module image style access that allows you to set up access rules per image style.
That should give you the extra protection you need.
Comment: Thanks Rooby, I will check out the module now and keep this thread posted.


Three questions concerning holomorphic functions defined by contour integrals

Question: Consider the following situation: a simple, closed, piecewise smooth curve $\gamma$ in the complex plane and $\Omega$ the bounded connected component of the complement of $\gamma$  in $\mathbb{C}$; a continuous function $\psi:\gamma \rightarrow \mathbb{C}$; a function $f:\Omega \rightarrow \mathbb{C}$ defined by $f(z) = \frac{1}{2\pi i}\int_{\gamma} \frac{\psi(w)}{w-z} dw$ for all $z\in\Omega$.
It's a simple result (than can be found in Rudin's Complex and Real Analysis in a slightly more general version) that $f$ is holomorphic in $\Omega$. I have been thinking in the following related questions:
1 . Is it the case that for every region $\Omega$, obtained as before, and for every holomorphic function $g$ defined there one can find a function $\psi$ defined on $\gamma$ such that the function $f$ constructed as before is $g$? The answer is yes for the case in which the function $g$ admits a holomorphic extension $h$ to an open set $U$ that contains $\overline{\Omega}$, taking $\psi=h$. I made no progress beyond that trivial case.
2 . Is it true that if $\Omega$ is as before and $g:\overline{\Omega}\rightarrow \mathbb{C}$ is holomorphic in $\Omega$ and continuous in $\overline{\Omega}$ then, when performing the previous construction with $\psi=g$, it turns out that $f=g$ in $̣\Omega$? The same trivial case of 1 applies here too.
3 . What conditions can\must be imposed to $\psi$ (or maybe $\gamma$) to obtain in the previous process a function $f$ admitting a continuous extension to $\overline{\Omega}$? No progress.
Thanks.
Answer:
1 . Is it the case that for every region $\Omega$, obtained as before, and for every holomorphic function $g$ defined there one can find a function $\psi$ defined on $\gamma$ such that the function $f$ constructed as before is $g$?

No. For
$$f(z) = \frac{1}{2\pi i}\int_\gamma \frac{\psi(w)}{w-z}\,dw,$$
we have the growth condition
$$\lvert f(z)\rvert \leqslant \frac{K(\psi,\gamma)}{\operatorname{dist}(z,\gamma)},$$
and a function that grows faster - like $g(z) = (z-w_0)^{-2}$ for a $w_0$ on the trace of $\gamma$ - cannot be an integral of the specified form.

2 . Is it true that if $\Omega$ is as before and $g\colon \overline{\Omega}\to\mathbb{C}$ is holomorphic in $\Omega$ and continuous in $\overline{\Omega}$ then, when performing the previous construction with $\psi = g$, it turns out that $f = g$ in ̣$\Omega$?

Yes, since the boundary is sufficiently smooth, the continuity on the closure of $\Omega$ is sufficient for $g$ to be the Cauchy integral of its boundary values. You can approximate the boundary curve $\gamma$ by curves in $\Omega$ with the same parameter interval, such that the integrand $\dfrac{g(\gamma_n(t))}{\gamma_n(t)-z}\gamma_n'(t)$ converges dominatedly to $\dfrac{g(\gamma(t))}{\gamma(t)-z}\gamma'(t)$, and that means the Cauchy formula holds.
For the special case of a disk, much is known about which functions are the Cauchy integral of $L^p$-functions on the boundary circle (Rudin treats some results on $H^p$ spaces in his book), and they have a simple characterisation. For domains with a piecewise smooth boundary, less is known, but experts in the field probably still know much, I don't unfortunately.

3 . What conditions can/must be imposed to $\psi$ (or maybe $\gamma$) to obtain in the previous process a function $f$ admitting a continuous extension to $\overline{\Omega}$?

A necessary condition is that
$$\int_\gamma \psi(w)\cdot w^n\,dw = 0$$
for all $n \geqslant 0$, since if $f$ admits a continuous extension to $\overline{\Omega}$, so does $g_n(z) = f(z)\cdot z^n$, and the integral over its boundary values must vanish by Cauchy's integral theorem and the approximation argument. For nice enough $\gamma$ at least (circles), that condition is also sufficient, if memory serves, but it may not be sufficient for badly behaved curves.


Ubuntu Server IPtables to Allow PPTP VPN Traffic

Question: I am trying to setup a "simple" VPN server for my network so I can easily connect remotely to a few boxes in my network. I can access the VPN and register on the network. I get an IP but can not ping anyone. My local subnet is 192.168.10.0/24
I set the VPN to use the same (20-25 as I know this range is not used).
The VPN server would be on the firewall machine that comes into the network.  It has two nic's on it.
Here is my iptables firewall:
#!/bin/sh

iptables="/sbin/iptables"
modprobe="/sbin/modprobe"
depmod="/sbin/depmod"

EXTIF="eth1"
INTIF="eth2"

load () {

    $depmod -a

    $modprobe ip_tables
    $modprobe ip_conntrack
    $modprobe ip_conntrack_ftp
    $modprobe ip_conntrack_irc
    $modprobe iptable_nat
    $modprobe ip_nat_ftp
    $modprobe ip_conntrack_pptp
    $modprobe ip_nat_pptp

echo "enable forwarding..."
echo "1" > /proc/sys/net/ipv4/ip_forward
echo "enable dynamic addr"
echo "1" > /proc/sys/net/ipv4/ip_dynaddr

#  start firewall

    #default policies
    $iptables -P INPUT DROP
    $iptables -F INPUT
    $iptables -P OUTPUT DROP
    $iptables -F OUTPUT
    $iptables -P FORWARD DROP
    $iptables -F FORWARD
    $iptables -t nat -F

echo "  opening loopback interface for socket based services."
$iptables -A INPUT -i lo -j ACCEPT
$iptables -A OUTPUT -o lo -j ACCEPT

echo "  allow all connections OUT and ONLY existing related ones IN"
$iptables -A INPUT -i $INTIF -j ACCEPT
$iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$iptables -A OUTPUT -o $EXTIF -j ACCEPT
$iptables -A OUTPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
$iptables -A FORWARD -i $EXTIF -o $INTIF -m state --state ESTABLISHED,RELATED -j ACCEPT
$iptables -A FORWARD -i $INTIF -o $EXTIF -j ACCEPT
$iptables -A FORWARD -j LOG --log-level 7 --log-prefix "Dropped by firewall: "

$iptables -A INPUT -j LOG --log-level 7 --log-prefix "Dropped by firewall: "
$iptables -A OUTPUT -j LOG --log-level 7 --log-prefix "Dropped by firewall: "

echo "  enabling SNAT (MASQUERADE) functionality on $EXTIF"
$iptables -t nat -A POSTROUTING -o $EXTIF -j MASQUERADE

$iptables -A INPUT -i $INTIF -j ACCEPT
$iptables -A OUTPUT -o $INTIF -j ACCEPT

echo "  Allowing packets with ICMP data (pings)"
$iptables -A INPUT -p icmp -j ACCEPT
$iptables -A OUTPUT -p icmp -j ACCEPT

$iptables -A INPUT -p udp -i $INTIF --dport 67 -m state --state NEW -j ACCEPT

echo "  port 137 for netBios"
$iptables -A INPUT -i $INTIF -p udp --dport 137 -j ACCEPT
$iptables -A OUTPUT -o $INTIF -p udp --dport 137 -j ACCEPT

echo "  opening port 53 for DNS queries"
$iptables -A INPUT -p udp -i $EXTIF --sport 53 -j ACCEPT

#echo "  opening port 22 for internal ssh"
$iptables -A INPUT -i $INTIF -p tcp --dport 22 -j ACCEPT

$iptables -A INPUT -p gre -j ACCEPT
$iptables -A FORWARD -p gre -j ACCEPT

echo "  opening port 1723 for VPN Server"
$iptables  -A INPUT -p tcp -i $EXTIF --dport 1723 -m state --state NEW -j ACCEPT

echo "  opening port 80 for webserver"
$iptables -A INPUT -p tcp -i $EXTIF --dport 80 -m state --state NEW -j ACCEPT

echo "  opening port 21 for FTP Server"
$iptables  -A INPUT -p tcp -i $EXTIF --dport 21 -m state --state NEW -j ACCEPT

echo "  opening ssh for web on port 2609 for firewig"
$iptables -A INPUT -p tcp --dport 2609 -j ACCEPT
$iptables -A OUTPUT -p tcp --dport 2609 -j ACCEPT

echo "  opening ssh for web on port 22  for betty"
$iptables -A PREROUTING -t nat -i $EXTIF -p tcp --dport 22 -j DNAT  --to 192.168.10.96:2302
$iptables -A FORWARD -p tcp -m state --state NEW -d 192.168.10.96 --dport 2302 -j ACCEPT

#echo "   opening Apache webserver for HoH"
$iptables -A PREROUTING -t nat -i $EXTIF -p tcp --dport 80 -j DNAT --to 192.168.10.96:80
$iptables -A FORWARD -p tcp -m state --state NEW -d 192.168.10.96 --dport 80 -j ACCEPT

}
flush() {
    echo "flushing rules...."
    $iptables -P FORWARD ACCEPT
    $iptables -F INPUT
    $iptables -P INPUT ACCEPT
}

case "$1" in

    start|restart)
    flush
    load
    ;;
    stop)
    flush
    ;;
*)
    echo "usage: start|stop|restart."
;;

esac
exit 0
}

The debug log has a lot of lines like:
Jul  2 17:07:36 firewig kernel: [602396.900564] Dropped by firewall: IN=ppp0 OUT= MAC= SRC=192.168.10.21 DST=255.255.255.255 LEN=205 TOS=0x00 PREC=0x00 TTL=128 ID=28241 PROTO=UDP SPT=138 DPT=138 LEN=185

and
Jul  2 17:14:44 firewig kernel: [602825.036556] Dropped by firewall: IN=ppp0 OUT=eth2 SRC=192.168.10.21 DST=192.168.10.96 LEN=60 TOS=0x00 PREC=0x00 TTL=127 ID=6866 PROTO=ICMP TYPE=8 CODE=0 ID=1536 SEQ=512

I am sure it is my firewall rules that are not allowing the traffic to work but I can not figure it out.
EDIT
So I realized today that I still cannot ping but I can view shares and mstsc etc. I still get a lot of packets dropped in the syslog. Not sure why these are being dropped and why I cannot ping, probably related, if not the same question:
syslog extract:
Jul  4 14:53:40 firewig kernel: [767160.780550] Dropped by firewall: IN=ppp0 OUT=eth2 SRC=192.168.10.21 DST=192.168.10.96 LEN=116 TOS=0x00 PREC=0x00 TTL=127 ID=24588 DF PROTO=TCP SPT=7002 DPT=139 WINDOW=64613 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.781014] Dropped by firewall: IN=eth2 OUT=ppp0 SRC=192.168.10.96 DST=192.168.10.21 LEN=144 TOS=0x00 PREC=0x00 TTL=63 ID=45843 DF PROTO=TCP SPT=139 DPT=7002 WINDOW=65535 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.820569] Dropped by firewall: IN=ppp0 OUT=eth2 SRC=192.168.10.21 DST=192.168.10.96 LEN=152 TOS=0x00 PREC=0x00 TTL=127 ID=24590 DF PROTO=TCP SPT=7002 DPT=139 WINDOW=64574 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.820598] Dropped by firewall: IN=ppp0 OUT=eth2 SRC=192.168.10.21 DST=192.168.10.96 LEN=85 TOS=0x00 PREC=0x00 TTL=127 ID=24592 DF PROTO=TCP SPT=7002 DPT=139 WINDOW=64535 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.821028] Dropped by firewall: IN=eth2 OUT=ppp0 SRC=192.168.10.96 DST=192.168.10.21 LEN=79 TOS=0x00 PREC=0x00 TTL=63 ID=45844 DF PROTO=TCP SPT=139 DPT=7002 WINDOW=65535 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.821073] Dropped by firewall: IN=eth2 OUT=ppp0 SRC=192.168.10.96 DST=192.168.10.21 LEN=79 TOS=0x00 PREC=0x00 TTL=63 ID=45845 DF PROTO=TCP SPT=139 DPT=7002 WINDOW=65535 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.836549] Dropped by firewall: IN=ppp0 OUT=eth2 SRC=192.168.10.21 DST=192.168.10.96 LEN=116 TOS=0x00 PREC=0x00 TTL=127 ID=24594 DF PROTO=TCP SPT=7002 DPT=139 WINDOW=64431 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.836944] Dropped by firewall: IN=eth2 OUT=ppp0 SRC=192.168.10.96 DST=192.168.10.21 LEN=128 TOS=0x00 PREC=0x00 TTL=63 ID=45846 DF PROTO=TCP SPT=139 DPT=7002 WINDOW=65535 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.868549] Dropped by firewall: IN=ppp0 OUT=eth2 SRC=192.168.10.21 DST=192.168.10.96 LEN=130 TOS=0x00 PREC=0x00 TTL=127 ID=24596 DF PROTO=TCP SPT=7002 DPT=139 WINDOW=64392 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.868964] Dropped by firewall: IN=eth2 OUT=ppp0 SRC=192.168.10.96 DST=192.168.10.21 LEN=79 TOS=0x00 PREC=0x00 TTL=63 ID=45847 DF PROTO=TCP SPT=139 DPT=7002 WINDOW=65535 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.872557] Dropped by firewall: IN=ppp0 OUT=eth2 SRC=192.168.10.21 DST=192.168.10.96 LEN=160 TOS=0x00 PREC=0x00 TTL=127 ID=24598 DF PROTO=TCP SPT=7002 DPT=139 WINDOW=64353 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.872967] Dropped by firewall: IN=eth2 OUT=ppp0 SRC=192.168.10.96 DST=192.168.10.21 LEN=79 TOS=0x00 PREC=0x00 TTL=63 ID=45848 DF PROTO=TCP SPT=139 DPT=7002 WINDOW=65535 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.876275] Dropped by firewall: IN=ppp0 OUT=eth1 SRC=192.168.10.21 DST=22.214.171.124 LEN=52 TOS=0x00 PREC=0x00 TTL=127 ID=24600 DF PROTO=TCP SPT=7035 DPT=443 WINDOW=65535 RES=0x00 SYN URGP=0
Jul  4 14:53:40 firewig kernel: [767160.876300] Dropped by firewall: IN=ppp0 OUT=eth1 SRC=192.168.10.21 DST=126.96.36.199 LEN=52 TOS=0x00 PREC=0x00 TTL=127 ID=24602 DF PROTO=TCP SPT=7036 DPT=443 WINDOW=65535 RES=0x00 SYN URGP=0
Jul  4 14:53:40 firewig kernel: [767160.888548] Dropped by firewall: IN=ppp0 OUT=eth2 SRC=192.168.10.21 DST=192.168.10.96 LEN=116 TOS=0x00 PREC=0x00 TTL=127 ID=24604 DF PROTO=TCP SPT=7002 DPT=139 WINDOW=64265 RES=0x00 ACK PSH URGP=0
Jul  4 14:53:40 firewig kernel: [767160.888935] Dropped by firewall: IN=eth2 OUT=ppp0 SRC=192.168.10.96 DST=192.168.10.21 LEN=112 TOS=0x00 PREC=0x00 TTL=63 ID=45849 DF PROTO=TCP SPT=139 DPT=7002 WINDOW=65535 RES=0x00 ACK PSH URGP=0

Ping turned out to be a red hearing. VPN is working correctly now, although I can not route to the internet from the VPN unfortunately.
Answer: You need rules to allow or drop a bunch of basic traffic.  Try installing shorewall.   It is simple to setup and configure your iptables appropriately.
You need to enable at least some ICMP (echo, site unreachable, etc.) traffic.  Ports 137-139, and 445 are used by windows Netbois (file and printer sharing, etc).
Answer: You have a nice script that allows, amongst other things, traffic from your internal interface to your external without restriction, and traffic from your external to your internal if it matches the state engine.  These interfaces are identified as $EXTIF and $INTIF, and the script sets them to eth1 and eth2 respectively.
Looking at your kernel logs, it seems more as if the internal clients are on ppp0, being VPN clients, and the "external" interface is eth2.
You could try changing the value of $INTIF and $EXTIF in the script (they're near the top) and rerunning it.


countdown in shell without seq or jot

Question: I'm trying to make a countdown timer script that takes a number of seconds as $1, then counts down to zero, showing the current remaining seconds as it goes.
The catch is, I'm doing this on an embedded box that doesn't have seq or jot, which are the two tools I know can generate my list of numbers.
Here's the script as I have it working on a normal (non-embedded) system:
#!/bin/sh

for i in $(/usr/bin/jot ${1:-10} ${1:-10} 1); do
    printf "\r%s " "$i"
    sleep 1
done

echo ""

This works in FreeBSD.  If I'm on a Linux box, I can replace the for line with:
for i in $(/usr/bin/seq ${1:-10} -1 1); do

for the same effect.
But what do I do if I have no jot OR seq?
Comment: What shell are you using?  Not bash, I assume?
Comment: I assume so too.  I don't have a `/bin/bash`, and I know I can't use things like bash parameter expansion. It's a pretty limited system - it doesn't identify itself when I log in, and it has no `uname`. So sorry, let's just assume Bourne.
Answer: The problem with "vanilla" Bourne shell is that there's no such thing; the behavior depends on the particular implementation.  Most modern /bin/shes have POSIX features, but differ in the details. I have a habit of falling back to really ancient Bourne features when I go into "sh-compatibility" mode, which was helpful 30 years ago but is usually overboard on modern systems, even embedded ones. :)
Anyway, here's a generic countdown loop that works even in very old shells, yet still works fine in modern bash/dash/ksh/zsh/etc.  It does require the expr command, which is a pretty safe requirement.
i=10
while [ $i -gt 0 ]; do
  ...
  i=`expr $i - 1`
done

So if your embedded system has printf, here's your script, with the same "default to 10 if no argument specified" behavior:
#!/bin/sh
n=$1
i=${n-10}
while [ $i -gt 0 ]; do
  printf '\r%s ' "$i"
  i=`expr $i - 1`
  sleep 1
done

(The first two lines can probably be replaced with just i=${1-10}, but some - again, ancient - shells didn't like applying special parameter expansions to numeric parameters.)
If the system doesn't have printf, then it becomes problematic; with only the shell's built-in echo (or no builtin and some randomly selected implementation of /bin/echo), there's no guaranteed way to do either of those things (echo a special character or prevent the newline).  You might be able to use \r or \015 to get the carriage return.  You might need -e to get the shell to recognize those (but that might just wind up echoing a literal -e). Putting a literal carriage return inside the script will probably work but makes maintaining the script a pain.
Similarly, -n might squash the newline, but might just echo a literal -n.  The earliest way to squash newlines with echo used the special sequence \c where the newline would naturally go; this still works with some versions of /bin/echo (including the one on Mac OS X) or in conjunction with bash's builtin echo's -e option.
So what seems to be the simplest part of the script might be the part that makes you reach for awk.
Comment: @Graham - we don't know what system you have.  It's rude to downvote everyone's answers just because they happen not to work on it, especially when I said right in the answer that the echo might not work.
`echo ... \c` is how you do no-newlines on some systems (it even still works with `/bin/echo` on OS X).  The octal carriage return was a swing in the dark, but it seemed more likely to work than '\r'.  If you have a way to get a literal CR into the script, that might be best.
Comment: This system does let me use `${1:-10}`, or I wouldn't have included that in my question. Thanks for the warning though, I'll keep it in mind if I need to create ultra-portable scripts in the future. :) And I do have `printf`.
Comment: Also, your `echo` line doesn't work for me.  If I `i=10; echo '\015'$i'\c'`, I get `\01510\c`.
Comment: Hmm, you're right, you did suggest experimenting with echo. I didn't realize that `\015` was a carriage return, and I still don't know what `\c` is. I really think you could have explained this a lot more clearly.  I can't remove the downvote unless your answer changes. Perhaps you could put this stuff in your answer?  It's bizarre that the only answer I got that *worked* for me, somebody else downvoted.
Comment: @Graham - fair enough.  Edited for clarification.
Comment: Great turnaround, that's a much better explanation and expansion.  Changed to +1.  I'm leaving the checkmark on the other answer though, because that's the one that actually worked for me.
Answer: If you're using Bash, this is what you want:
for i in {1..10}

Or no bash? How about the stuff from this post? Bourne Shell For i in (seq)

try
for i in 1 10 15 20
do
   echo "do something with $i"
done

else if you have recent Solaris, there is bash 3 at least. for example
  this give range from 1 to 10 and 15 to 20
for i in {1..10} {15..20}
do
  echo "$i"
done

OR use tool like nawk
for i in `nawk 'BEGIN{ for(i=1;i<=10;i++) print i}'`
do
  echo $i
done

OR even the while loop
while [ "$s" -lt 10 ]; do s=`echo $s+1|bc`; echo $s; done

Comment: I don't have `nawk` or `bc`, but I guess any `awk` will do. Your other suggestions don't work for me at all, and I don't see how they apply to my question.
Comment: @Graham They apply to your question because they are different ways to loop through a set of numbers. You didn't specify that you didn't have nawk or bc or bash; your question asks about counting in shell "without seq or jot". It's very rude to downvote people for not reading your mind; we are trying to be helpful despite your very vague requirements.
Comment: None of your solutions mention how to get a command line option (`$1`) into a countdown. The second solution suggests bash-style ranges, when I was clear that I don't have bash. Your `nawk` solution counts up (not down), but doesn't say how to get `$1` into the script. It seems to me that you came up with a bunch of script segments that are semi-related to the part of my script that I'm *not* having a problem with, and you provided nothing that actually answers it. Sorry, but that warrants a downvote.
Comment: @Graham I'm sorry, but are you saying that your question was about how to get a command line option into the countdown? Because it reads as a question about generating sequences of numbers using tools other than seq and jot. Your examples, don't bother with `$1`, and neither does the answer you accepted. So I don't really understand what you're getting at with your sudden interest in `$1`. And yes, the example I put counts up, because it's a quote from the post I linked you to, which is a very similar question to yours. You also hadn't mentioned not having bash until after my initial post.
Comment: My original question didn't mention bash at all - the subject says "shell", my example code uses /bin/sh, and there's no bash tag. So there's no reason to assume bash. Then when clarification was asked in comments, I provided it. The subject also says "countdown", which is a hint to the direction the numbers should go. The answer I accepted *does* use `${1:-10}`, which is what I used in my example.
Comment: So you applied a punitive downvote to my question because you assumed things incorrectly, then answered your misinterpretation instead of my question?  That's mature.  You DO realize, don't you, that **`${1:-10}` is a reference to `$1`**?  It's in my question, it's in ghoti's answer, but it's not in your answer.  The reason I made it a requirement is specifically because the counting notation you suggested, the `{1..10}` thing, DOES NOT WORK, even in the shell I'm not using, when you try to use variables as part of the range.
Comment: We obviously disagree. This kind of arguing on the internet doesn't do anybody any good, though, so please move on and leave me alone.
Answer: If your shell was bash, you could count down from a fixed number with something like this:
#!/bin/bash

for n in {10..1}; do
  printf "\r%s " $n
  sleep 1
done

But that won't work for you, because bash won't handle things like {${1:-10}..1}, and you've specified you want a command line option.
Of course, you've also said you're not using bash, so we'll assume a simpler shell.
If you have awk, you can use that to count.
#!/bin/sh

for n in $(awk -v m="${1:-10}" 'BEGIN{for(;m;m--){print m}}'); do
  printf "\r%s " $n
  sleep 1
done
printf "\r \r"  # clean up

If you don't have awk, you should be able to do it in pure shell:
#!/bin/sh

n=${1:-10}

while [ $n -gt 0 ]; do
  printf "\r%s " $n
  sleep 1
  n=$((n-1))
done
printf "\r \r"  # clean up

I think the pure-shell version is probably simple enough that it should be preferred over the awk solution.
Of course, as Mark Reed pointed out in comments, if your system doesn't include a printf, then you'll need to perform some ugly echo magic that will depend on your OS or shell... and if your shell doesn't support $((..)), you can replace that line with n=$(expr $n - 1).  If you want to add error handling in case a non-numeric $1 is provided, that wouldn't hurt.
Comment: Depending on the shell, might not have arithmetic expressions `$((...))`
Comment: See the notes on my answer about `${1:-10}` and `printf`.  Also, note that `$((...))` is a POSIXism; the embedded shell might not have it.  If it does, that implies that it has integer arithmetic, which means you can also use `while (( n > 0 )); do`.  Also, `let n-=1` or `(( n-=1 ))` may be more efficient as they avoid the string conversion done by `$((..))`
Comment: True and true.  Since we don't know what the actual system is, we're all guessing as to what it supports.  We don't know if this system includes `expr`.  If it doesn't include `seq` then it could be BSD-based, or it could be Linux+BusyBox.  Or it could be something else entirely, or just very old.  Between the bunch of us, Graham probably has enough to answer his question.

countdown in shell without seq or jot


Why does $\mu > 0$ (or even $\mu > \epsilon$) "seem easier" to substantiate than $\mu \neq 0$?

Question: Consider a random variable $X$ following a normal distribution $N(\mu,\sigma^2)$. Suppose that we have drawn iid samples of $X$, obtaining a data set with a sample mean $\bar{x}>0$.
We want to test whether the data supports $\mu >0$ with a significance level $\alpha \in (0,1)$. Setting
\begin{equation*}
H_0: \mu \le 0,
\quad H_1: \mu > 0,
\end{equation*}
we examine the $p$-value
\begin{equation*}
p \;=\; \Pr(\overline{X} \;\ge\; \bar{x} \mathrel{|} H_0) \;=\; \Pr(\overline{X} \ge \bar{x}\mathrel|{\mu \le 0}) \;\le\; \Pr(\overline{X} \ge \bar{x} \mathrel{|} \mu = 0).
\end{equation*}
[N.B.: As kindly pointed out by @dimitriy, in practice we often set the null hypothesis simply to $\mu=0$, for which the $p$-value would be directly the RHS of the above inequality.]
As in textbooks, the data provides a sufficient evidence to substantiate $\mu > 0$ if $p \le \alpha$.
Now, we change our mind and test whether the data supports $\mu \neq 0$, with
\begin{equation*}
\hat{H}_0: \mu = 0,
\quad \hat{H}_1: \mu \neq 0,
\end{equation*}
and the $p$-value being
\begin{equation*}
\hat{p} \;=\; \Pr(|\overline{X}| \ge \bar{x} \mathrel{|} \hat{H}_0) \;=\; \Pr(|\overline{X}| \ge \bar{x} \mathrel|{\mu = 0}).
\end{equation*}
Again, textbooks tell us that the data provides sufficient evidence to substantiate $\mu \neq 0$ if $\hat{p} \le \alpha$, with $\alpha$ being the aforementioned significance level.
Here comes the part that confuses me. Since
$$\hat{p} \ge 2p,$$
it turns out "easier" to substantiate $\mu >0$ than $\mu \neq 0$, in the sense that the data may strongly support $\mu >0$, but does not constitute sufficient evidence for $\mu \neq 0$; equivalently, the data may strongly suggest rejecting $\mu \le 0$ without being able to reject $\mu = 0$. For instance, imagine that $p \le 0.6\%$, $\hat{p} = 1.2\%$, and $\alpha = 1\%$.
[Update: We can strengthen the above observation a bit: there exists an $\epsilon >0$ that it "seems easier" to substantiate $\mu > \epsilon$ than doing the same for $\mu \neq 0$.]
So what is the logic? What have I overlooked or misunderstood? I believe the calculation is trivial to everyone here, but there seems to be something to say about the interpretation. What is it?
In my textbook, hypothesis testing is compared to a court trial, with the null hypothesis being the suspect is innocent. Following this comparison, a probably quite poor metaphor for the paradox elaborated above is that we may have strong evidence to support that "A killed B by stabbing a dagger into B's chest", but not enough justification to substantiate that "A killed B". No, I must have made some silly mistake.
Thank you very much.
Background: Being not a statistician by any means, I am asked to teach a course that involves a part of statistics. The question occurs to me when I am preparing my notes.
Comment: I've rolled back the question to its original statement. Once you've got answers, it seems unfair to substantially alter how the question should be understood. If you have outstanding concerns which are not contained in the question, that's fine, you can ask a new question, perhaps referring back to this one if you think the context is helpful.
Comment: One possible resolution of the paradox is: the root of the problem is that you are using the same significance level for both the one-sided test and the two-sided test. Since you think the hypothesis $\mu \ne 0$ is more likely to be true than the hypothesis $\mu > 0$, it should take less evidence to make you believe it is true, so you should test at a more lenient significant level.
Comment: You should start a new question rather than editing an existing one.
Comment: Thank you very much @Sycorax. I accept your edition, even though I do not agree that what I did was unfair. I did not change the question but rather added a new angle to help others to understand it. Many thanks again.
Answer: There are some misconceptions in your question that I need to clear up before getting to the answer.
The null hypothesis $H_0$ in a statistical test is always the claim you want to argue against. The alternative hypothesis $H_1$ is the claim you hope to be true.
The null and the alternative need to be

mutually exclusive (no overlap)
collectively exhaustive (partition the parameter space)
the equality sign ($=$, $\ge$, or $\le$) almost always appears in the null.

So your first test should have $H_0: \mu \le 0$ and $H_1: \mu > 0$.
Your second test should have $H_0: \mu = 0$ and $H_1: \mu \ne 0$.
The p-value is the probability of seeing the observed mean (or something even more extreme) if the null hypothesis was true. Then we apply the rule "reject the null when the p-value is small." The basic idea is that if seeing a big mean is unlikely if the null was true, the null is likely to be false.
There is a slight complication in the first test. The null is a composite one: it's an interval rather than a single point. So we will have to calculate the probability when $\mu=0$, then when $\mu =-1$, and also everywhere else below zero, since all those points are inside the null. But that's an infinite number of points! What we do instead is to calculate the probability at the most extreme point of the null hypothesis, closest to alternative parameter space, which is at $\mu = 0$. This means that the p-value is exact only for $\mu=0$. If $\mu<0$, then our p-value is just a conservative bound on the type I error rate (the error being finding a negative effect when there is none). In other words, if the true effect is negative, then finding a false positive result is even less likely than 5% (or whatever value of $\alpha$ your question requires). This is also the reason why statistics packages will express the one-sided null as $\mu=0$ rather than $\mu \le 0$, which is technically correct, but confusing notation.
Now for your question. For both one-sided and two-sided tests, we calculate the p-value with $\mu=0$. Suppose you observe a mean of $u>0$. With a two-sided test, you need to calculate $\Pr(\bar X \ge u \vert \mu=0)$ and $\Pr(\bar X
\le -u \vert \mu=0)$, since both kinds of extreme values constitute evidence against that null.
With a one-sided test, seeing a mean that's less than $-k$ doesn't count as evidence against the null, so we only calculate $\Pr(\bar X \ge u \vert \mu=0)$. This is why the p-value is larger in the two-sided case, which means it's easier to reject in the one-sided case.
Another way to put this is that a two-sided test is just two one-sided tests cobbled together (a superiority and an inferiority one).
Comment: It's easy to test the dagger claim by checking if there's a knife shaped hole. If there isn't one, then that hypothesis is just as dead as B. It's a whole lot harder to refute the claim of all potential murder channels, since you would need to check for holes of all sorts, do bloodwork for poisons, swab the candlesticks for B's blood, and so on. It would require a lot more evidence to rule all of them out.
Comment: Hi @dimitriy! Thank you for your answer! 1. I understand why we calculate the $p$-valaue using $\mu = 0$ instead of $\mu \le 0$ in the one-sided testing, in exactly the same way as you elaborated. Thanks for point this out. 2. I agree with all that you said, but I am afraid that it does not explain the counterintuitive fact that "the $p$-value is larger in the two-sided case, which means it's easier to reject in the one-sided case". Sure, according to how we calculate $p$,  what you said is true (that's also what I quantified in my question), but how to understand it? What is the intuition?
Comment: One-sided tests broaden the claim we wish to reject, so it's easier to do that. Here's a kind of parable that gets at the intuition. Say Steph Curry (a famous basketball player) invites me to his house for a BBQ. We shot some free throws. Steph makes 7 of 7. I make 4 out of my 7. The p-value on the null that Steph and I are just as good at free throws is 0.0507, and so it cannot be rejected at the conventional level. It's just that there is not enough data to tell us apart with only 7 shots apiece.
Comment: But we can summarily reject the null that I am better (or just as good) with the same data in favor of the alternative that I am worse since that p = 0.0253.
Comment: This also illustrates the point that if we fail to reject the null, that does not make it true. Failure to reject the "just as good" null certainly does not mean that I am as good as him.
Comment: Thank you very much @dimitriy. Sorry, maybe I am too blunt to understand it. But it seems to me that your explanation merely repeats the calculation, which people know how to do. What I asked is not how to calculate the values, but how to understand them, Many thanks again.
Comment: Sorry to say, but I much prefer the other answers -- even though I do not fully agree with any of them, because they try to interpret the quantities, instead of just calculating them, although I believe they are also capable of doing so. Thank you.
Comment: My dagger explanation contains no calculations whatsoever, and that's the last bit of intuition I got for you. Best of luck figuring it out.
Comment: Condition (3) (that $H_0$ express an equality) is not a general requirement of hypothesis tests, although it almost always is the case.  (TOST provides a counterexample.)  What we most desire is that $H_0$ permits us to obtain a sampling distribution for a test statistic, or at least effective bounds on its sampling distribution.
Comment: @whuber My understanding is that for TOST the nulls are $\ge \Delta_{L}$ and $ \le \Delta_{U}$, which is consistent with condition (3). Could you elaborate on why you think TOST is a counterexample?
Comment: There can be only one null hypothesis in a single test, by definition.  TOST is a procedure to test a null of the form $\mu\in(-\infty,L]\cup[U,\infty)$ rather than $\mu=\mu_0.$
Comment: @whuber I think I see where our disagreement is coming from. I am talking about TOST as two simultaneous one-sided tests (hence nulls, rather than null), but you are treating them as a single object, hence a single null. For exposition, I prefer my approach, but let me spend some more time thinking about this.
Comment: I only brought up TOST as a practical example.  There are plenty of hypothesis tests, at least theoretically, that do not involve equalities in either $H_0$ or $H_A.$  This might seem like nit-picking, but in this context I think it gets to a key aspect of the distinction between the two tests in the question.
Answer:
the data may strongly support $\mu > 0$, but does not constitute sufficient evidence for $\mu \neq 0$

I don't know if this reasoning helps or if it is 100% correct... You accept to be wrong $\alpha$-percent of the times in hypothetical repeats of the test; for example, you accept to be wrong 5% of the times in rejecting the null.
In a two-tailed test you spread this 5% in the left-tail and in the right-tail of the test distribution. In the one-tailed test, you still accept the be wrong with the same frequency (say, 5%) but always in the same direction so you put all the 5% in the same tail.
In fact, I wouldn't say that in the one-tailed test it is easier to reject, but rather that you need less extreme values. You need less extreme values compared to the two-tailed test in order to keep the overall error rate at the same $\alpha$ level.
Your trial metaphor is interesting but perhaps is not appropriate because A killed B is not more (or less) extreme than A killed B by stabbing.
Answer:
Hypothesis testing: why $\mu > 0$ (or even $\mu > \epsilon$) "seems easier" to substantiate than $\mu \neq 0$?

It seems easier because the one-sided t-test and two-sided t-test have different sensitivity for different values.

The two-sided t-test has sensitivity split for both positive and negative values.
The one-sided t-test has sensitivity for only the positive or only the negative values, and because of that will be 'easier' in substantiating a result (but only for a single side)

Below is a graph of the sensitivity or power for the two tests as a function of the true mean of the distribution. (Power is the probability that an observation is a positive result).

You can see that the one-sided test is not everywhere 'easier' than the two-sided test. It is only 'easier' for the positive values.

Also note that the tests have an equal $5\%$ probability/frequency of a positive result when in reality the effect is negative (when the true mean is equal to zero). So if the true mean is equal to zero then the two hypothesis tests are equally 'easy' in making a false claim of substantiating an alternative hypothesis (like $\mu \neq 0$ or $\mu > 0$).
But they are still different. They will make these (false) claims for different observations and for a specific observation they are not equally 'easy'.
This can occur because there is no unique way to compute p-values and associated hypothesis tests. The p-values based on different methods can be different for a particular observation but on average (for all possible observations) they will be equal.
Below you see a simulation of $10 000$ samples of size $n=5$ drawn from a standard normal distribution. We plot the observed (unbiased) sample standard deviation $s$ and the observed sample mean. Along with it we plot the rejected sampled based on a two-sided t-test and a one-sided t-test.

The amount of rejected samples is in both cases the same, namely $5\%$.
But, the rejected samples are different for the cases. The one-sided t-test is more sensitive to values on one side. The two-sided t-test splits the sensitivity to two sides.

So again, that is why the one-sided test may seem more 'easy' but that is because of the choice to place the sensitivity in a different region. It is only 'easier' for that region.

So it may occur that you reject $H_0: \mu \leq 0$ with an alternative hypothesis $H_a: \mu > 0$, but you can't reject with the same data $H_0: \mu = 0$ with an alternative hypothesis $H_a: \neq 0$.
When such situation occurs then it is seemingly a paradox. But,

It is not purely the data that rejected the $H_0$.
It is also your choice and it is also the alternative hypothesis that 'helps' rejecting the null hypothesis.

The same hypothesis, with the same data, can be or not be rejected, depending on your arbitrary rejection criteria.
Other examples (from this website) were different tests reject a hypothesis for different observations are:

ANOVA with F-test vs Tukey's range test. The ANOVA test and Tukey's procedure test the same null hypothesis 'equality of means' but have different p-values for different observations because they relate to different statistics and have sensitivity in different regions. One looks at the largest difference between samples, the other at the variance.

Mann-Whitney U test versus t-test can be used to test equality of two means. They have different p-values because the one uses the t-statistic based on the ratio of the difference in the means and the standard deviation, the other computes a statistic based on how often a value in the one sample is larger than a sample in the other sample.

Fisher exact test with pooled versus non-pooled data. In that question the Fisher exact test had a surprising behaviour in the power being different when the data was split into groups. But, effectively this was due to using different regions where the tests are sensitive.
Comment: But Bernoulli distribution is not a uniform distribution. The example is about a t-test when the data is normally distributed.
Comment: We have uniform distribution of p-value only for continuous distributions of sample.
Comment: @BotnakovN. I am not sure what you mean. Do you refer to hypothesis tests with discrete distributions?
Comment: for example, suppose that $X_i, 1 \le i \le n$ have distribution $Bern(\theta)$. Hence $p$-value for any statistics doesn't have uniform distribution.
Comment: There's still an important issue lurking here.  For testing against the alternative hypothesis $\mu\gt 0$ where the model includes potential values of $\mu\lt 0,$ it usually is *not* the case that $\Pr(\text{p value}\le p\mid \mu)=p.$  Equality occurs only for $\mu=0.$  For other negative $\mu$ this probability is usually strictly smaller than $p.$
Comment: @whuber do you mean when the null hypothesis is a composite hypothesis $H_0: \mu \leq 0$ instead of point hypothesis $H_0: \mu = 0$?
Comment: Yes, although I was stating it in slightly greater generality than that.

Why does $\mu > 0$ (or even $\mu > \epsilon$) "seem easier” to substantiate than $\mu \neq 0$?


Angular 6 multiples app deployed in firebase hosting

Question: I have a angular 6 app with multiple projects, i configure my rewrite rules to the following:
"rewrites": [
  {
    "source": "/ticket/**",
    "destination": "/ticket-app/index.html"
  },
  {
    "source": "/client/**",
    "destination": "/client-app/index.html"
  },
  {
    "source": "/admin/**",
    "destination": "/admin-app/index.html"
  }
],

The application is deployed without error, but when go to the urls, the applications not working correctly, the .js are no load for the browser. When you viisit the url of the .js is get only the .html page.I can see the following error in the developer console:

All get request to .js files and .css files always return index.html page, i try other rewrite formats like this:
"rewrites": [
  {
    "source": "/ticket/**",
    "destination": "/ticket-app/**"
  },
  {
    "source": "/client/**",
    "destination": "/client-app/"
  },
  {
    "source": "/admin/**",
    "destination": "/admin-app/*"
  }
],

But no working. I build the app with the command:
ng build ticket-app --prod --base-href '/ticket/'

thanks
Answer: You also need to add deployUrl. There are 2 options
1) add --deploy-url param when building the application
ng build ticket-app --prod --base-href '/ticket/'

2) Add configuration in angular.json for each application
"build": {
          "options": {
            ...
            "baseHref": "/ticket/",
            "deployUrl": "/ticket/"
          },
 ...
}


Select duas tabelas

Question: Boa tarde! por favor não estou conseguindo fazer um select
Tenho duas tabelas no banco (campanha e campanha_cliques)
Preciso selecionar somente os resultados que estiver com o tipo=2 da tabela (CAMPANHA) e ao mesmo tempo verificar se na tabela campanha_cliques no campo id_usuario não estiver o id do usuario
quero mostrar somentes os dados de quem ainda nao está no campo id_usuario da tabela campanha_cliques
SELECT DISTINCT
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM
  campanha c
INNER JOIN campanha_cliques p
WHERE p.tipo = '2'
  AND p.id_usuario != '$uiddw'

tentei isso mais nao funciona
Comment: E o que relaciona a tabela campanha com a tabela campanha_cliques? Ou você quer apenas o produto cartesiano das duas tabelas?
Comment: Consegui resolver obrigado pela ajuda
Answer: SELECT DISTINCT
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM
  campanha c
INNER JOIN campanha_cliques p
ON c.id = p.id_campanha
WHERE p.tipo = '2'
  AND p.id_usuario != '$uiddw'

Acredito que só o que falta é o ON que normalmente é utilizado pra fazer a ligação entre as tabelas envolvidas no JOIN.
Comment: Ainda não funciona!

Tipo vou explicar melhor, preciso pegar os dados da tabela "CAMPANHA" com o tipo=2 que nao tenha o id_usuario e id_campanha na tabela CAMPANHA_CLIQUES..

quero mostrar somente os dados que nao tenha ainda na tabela campanha_cliques.
Comment: consegui, obrigado
Answer: Sua pergunta está meio confusa mas talvez seja:
SELECT
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM campanha c
WHERE NOT EXISTS (SELECT * FROM campanha_cliques p WHERE p.tipo = '2' AND p.id_usuario == c.id_user);

ou ainda:
SELECT
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM campanha c LEFT OUTER JOIN campanha_cliques p ON (p.id_usuario == c.id_user)
WHERE p.tipo = '2' AND p.id_usuario IS NULL;

Comment: consegui, obrigado
Comment: E o que era? É bom ficar documentada a solução para que vier a pesquisar.
Answer: Me corrija se eu tiver entendido errado:
SELECT DISTINCT
  c.id,
  c.site,
  c.cliques_usados,
  c.cliques,
  c.localizacao,
  c.tipo,
  c.id_user
FROM
  campanha c
LEFT JOIN campanha_cliques p on p.id_campanha = p.campanha
WHERE p.tipo = '2'
  AND c.id_user = '$uiddw'
  AND p.i_campanha is null

Este select retornará os dados da tabela campanha com tipo = 2, id_user = '$uiddw', onde NÃO existam registros equivalentes na campanha_cliques.
É isso?
Comment: Se essa ou qualquer uma das respostas tiver sido útil, dê um upvote. Se alguma tiver sido a resposta correta, marque como correta. Se não, poste a solução e depois marque sua resposta como correta. Isso tudo ajuda futuros usuários que tiverem uma dúvida parecida.
Comment: consegui, obrigado

Select duas tabelas


Good book or online resources for systems of linear ordinary differential equations

Question: I am looking for a good and comprehensive book or online resource on systems of linear ordinary differential equations. It has to describe well the solution, stability and determination of arbitrary constants of n x n systems in normal form, including a lot of nonhomogenous cases.
Most of the resources I seem to find are about 2 x 2 or 3 x 3 system and no generalisations to the nth dimension are made.
Comment: If you google "ODE + pdf" you will very likely find lecture notes on several university servers around the globe. Lectures are a good and cheap source for questions like this.
Answer: Since you are asking specifically about linear systems, check out this book: "Linear Ordinary Differential Equations" by Coddington and Carlson.
Comment: @Moo Thanks, I fixed it.


How to compute accuracy for multi class classification problem and how is accuracy equal to weighted precision?

Question: Consider the example in this article
http://text-analytics101.rxnlp.com/2014/10/computing-precision-and-recall-for.html
Will accuracy be (30 + 60 + 80)/300?
what is weighted precision?
Comment: https://en.wikipedia.org/wiki/Confusion_matrix gives you formula for accuracy. You'll just need to substitute the values.
Comment: @SmallChess so the accuracy is calculated separately for each class?
Answer: I've got a wonderful solution and a perfect understandable solution for this problem as I was looking for same from this Question
You can calculate and store accuracy with:
(accuracy <- sum(diag(mat)) / sum(mat))
# [1] 0.9333333

Precision for each class (assuming the predictions are on the rows and the true outcomes are on the columns) can be computed with:
(precision <- diag(mat) / rowSums(mat))
#     setosa versicolor  virginica
#  1.0000000  0.9090909  0.8750000

If you wanted to grab the precision for a particular class, you could do:
(precision.versicolor <- precision["versicolor"])
# versicolor
#  0.9090909

Recall for each class (again assuming the predictions are on the rows and the true outcomes are on the columns) can be calculated with:
 recall <- (diag(mat) / colSums(mat))
    #     setosa versicolor  virginica
    #  1.0000000  0.8695652  0.9130435

If you wanted recall for a particular class, you could do something like:
(recall.virginica <- recall["virginica"])
# virginica
# 0.9130435

If instead you had the true outcomes as the rows and the predicted outcomes as the columns, then you would flip the precision and recall definitions.
Data:
(mat = as.matrix(read.table(text="  setosa versicolor virginica
 setosa         29          0         0
 versicolor      0         20         2
 virginica       0          3        21", header=T)))
#            setosa versicolor virginica
# setosa         29          0         0
# versicolor      0         20         2
# virginica       0          3        21

Answer: Accuracy is for the whole model and your formula is correct.
Precision for one class 'A' is TP_A / (TP_A + FP_A) as in the mentioned article. Now you can calculate average precision of a model. There are a few ways of averaging (micro, macro, weighted), well explained here:

'weighted':
  Calculate metrics for each label, and find their average, weighted by support (the number of true instances for each label). This alters 'macro' to account for label imbalance; (...)
Answer: I think your confusion come from the 3x3 table. But ... the link has an example on precision and recall for Label A. Accuracy is very similar.
Accuracy for A = (30 + 60 + 10 + 20 + 80) / (30 + 20 + 10 + 50 + 60 + 10 + 20 + 20 + 80)

https://en.wikipedia.org/wiki/Confusion_matrix

I don't know what weighted precision is about.
Comment: I do understand the denominator which is N and in numerator 30 + 60 + 80 are examples that were classified correctly, can you explain 10 + 20 in numerator?
Comment: @farheen I merely followed the formula. 60+10+20+80 = TN for label A.
Comment: They are cases that predicted for B and C, but the true labels are not A. (TN for A)
Comment: So we calculate accuracy for each label separately? then what will be the accuracy for entire model?
Answer: Try PyCM, it gives you accuracy and other parameters.

PyCM is a multi-class confusion matrix library written in Python
... and a proper tool for post-classification model evaluation that
  supports most classes and overall statistics parameters.

Check the html version of output.


Number of combinations of dice having different number of faces that add to $10$

Question: A function $\textbf{face}_{\mathrm{sum}}$ that takes a vector  $f$ that represents the number of faces of each die, and a positive integer  $s$ , and returns the probability that the sum of the top faces observed is  $s$ .
For example, if $f=[3,4,5]$  and  $s≤2$  or $s≥13$ , $\textbf{face}_{\mathrm{sum}}$ returns $0$, and if  $s=3$  or  $s=12$ , it returns  $\frac13*\frac14*\frac15=\frac1{60}$
$\textbf{face}_{\mathrm{sum}}([2,4,4,6], 10)$
I tried to solve this but I can't find the formula to calculate the number of combination of die faces that add to $10$, as the number of faces are different for each die.
Since max no of possible combination is $6*4*4*2=192$, all I need to find is the number of combination of die faces that add to $10$.
Thanks in advance.
Comment: The generating function for $f([3,4,5],n)$ is $$x^3\frac{x^3-1}{x-1}\frac{x^4-1}{x-1}\frac{x^5-1}{x-1}= x^{12}+3x^{11}+6x^{10}+9x^9+11x^8+11x^7+9x^6+6x^5+3x^4+x^3$$. The generating function for $f([2,4,4,6],n)$ is $$\scriptsize x^4\frac{x^2-1}{x-1}\frac{x^4-1}{x-1}\frac{x^4-1}{x-1}\frac{x^6-1}{x-1} =x^{16}+4x^{15}+9x^{14}+16x^{13}+23x^{12}+28x^{11}+30x^{10}+28x^9+23x^8+16x^7+9x^6+4x^5+x^4$$.
Comment: don't dice 2 and 3 have the same number of faces?
Comment: yep they have the same number of faces.
Answer: The formula is a bit messy. Suppose there are $n$ dice, and the list is $f=[f_1,f_2,\dots,f_n]$. You want to count the number of integer solutions to the constrained equation
$$
\begin{align}
x_1+x_2+\dots+x_n &= s\\ 1\le x_i&\le f_i\hspace{2cm} \text{ for }i=1,2,\dots,n
\end{align}
$$
To solve this, first, count the number of solutions without the constraint $x_i\le f_i$. This can be shown to be $\binom{s-1}{n-1}$ (imagine a line of $s$ dots, and you place dividers in $n-1$ of the $s-1$ spaces between the dots). Now, use inclusion exclusion to subtract out the "bad" solutions where some $x_i > f_i$. The result is this:
$$
\bbox[10px,border:solid 2px black]{
\Bbb P(\text{sum of dice }=s)
= \frac1{f_1\times\dots\times f_n}
\sum_{S\subseteq \{1,2,\dots,n\}}
(-1)^{|S|}\binom{s-1-\sum_{i\in S}f_i}{n-1}.}
$$
There are $2^n$ summands, one for each subset of $\{1,2,\dots,n\}$ (i.e. subset of the available dice). Also, whenever the upper index $s-1-\sum_{i\in S}f_i$ is negative, the binomial coefficient is defined to be zero.
For the special case where $f_1=f_2=\dots=f_n=d$, that is all of the dice have $d$ sides, this simplifies to
$$
\bbox[10px,border:solid 2px black]{
\Bbb P(\text{sum of $n$ dice with $d$ sides }=s)
= \frac1{d^n}
\sum_{k=0}^n
(-1)^{k}\binom{n}k\binom{s-1-kd}{n-1}.}
$$
For example, when $f=[2,4,4,6]$ and $s=10$, you get
$$
{1\over 2\cdot 4\cdot 4\cdot 6}\left[
\begin{array}{l}\binom{10-1}{4-1}\\
-\binom{10-2-1}{4-1}-\binom{10-4-1}{4-1}
-\binom{10-4-1}{4-1}-\binom{10-6-1}{4-1}\\
+\binom{10-2-4-1}{4-1}+\binom{10-2-4-1}{4-1}
\end{array}\right].
$$
Terms with $\sum_{i\in S}f_i>s-n$ have been omitted, since in these cases the binomial coefficient is zero.
Comment: Thank you for this detailed answer.
Comment: (−1−) can be negative (s=6, d=6, n=6)?
Comment: Seems that in summation, the upper bound should be ⌊(r−n)/s⌋ but not n. https://www.omnicalculator.com/statistics/dice
Answer: Do you know how to find the number of solutions for an equation like the following?
$$
x_1 + x_2 + ... + x_n = k
$$
given $\forall i \in [n]: x_i \in S_i$ for some set $S_i$.


Linear Programming Word Problem: Theater

Question: I'm having trouble understanding the second constraint.

A theater is presenting a program on drinking and driving for students and their parents[...] admission is 2.00 dollars for parents and 1.00 dollar for students. However, the situation has two constraints: 1) The theater can hold no more than 150 people and 2) every two parents must bring at least one student. How many parents and students should attend to raise the maximum amount of money?

Let $x$ = number of students
$y$ = number of parents
Since the question prompt wants the maximum amount of revenue, then the objective function is $z = x + 2y$
The theater can hold no more than 150 people so the first constraint is simply: $x + y \leq 150$
The second constraint is, "every two parents must bring at least one student," but I don't understand how to model this.
I've looked up a solution and it said $y \leq 2x$ is how to model this constraint, but I don't understand why. If there must be at least one student for every two parents then why wouldn't the inequality be $2y \geq x$?
Answer: Yes
y <= 2x
The easiest way to test this is with some data points and sketch a graph:
y_______x
2_______1,2,3,4,5…
4_______2,3,4,5,6…
6_______3,4,5,6,7…
Hence:
y <= 2x
2 <= 2x1  True
and
2y >= x
2x2 >= 5  False
Graph of y <= 2x


Is there a way to use netcat to transfer a gzipped file to a remote destination?

Question: I have an archive I need to transfer to a remote destination but the target drive is not big enough to hold both the compressed archive and the uncompressed files.
I wonder if there are some utilities which can decompress the archive as the data arrives, rather than having to receive the whole thing then decompress it.
netcat is what seems to show up in my searches but I am open to any alternatives.
Comment: curl http://URL/FILE.tar.gz | tar xz ?
Answer: On the sending side:
dd if=somefile.gz bs=32M |nc server 7000

On the receiving side:
nc -l -p 7000 | gzip -dfc|dd bs=32M of=somefile

or if it is a tar.gz
nc -l -p 7000 | gzip -dfc|tar -xvf -

Comment: I am trying to execute if over a secure connection via SSH. I first create a tunnel - ssh root@remote -L 7000:localhost:7000. But after I try to send via  dd if=/archives/file.gz bs=32M | nc localhost 7000 on the local side, an error appera channel 3: open failed: connect failed: Connection refused. Any idea what it could be. I am doing some searching anyway
Comment: You have to first establish the receiving side and then the sender.


Proof of $\text{Span}(A \cup \{x_0\})$ for $x_0 \notin A$ can uniquely determine $x = m + ax_0$?

Question: I have a difficult time understanding the claim $x = m + ax_0$ as stated in below text (it regards the proof of the Hahn-Banach theorem). I would rather look at a proof or a beginning of a proof; could anyone help me out a bit? It would be really appreciated.
Comment: Sorry, an off-topic question. Which pdf(?) software and/or OS render fonts so beautifully? I'm struggling to get any decent font rendering on Linux for example. I hope yours is not some MacOS magic.
Answer: If $x\in \operatorname{Span}(M \cup \{x_0\})$, then $$x = \sum_{i = 1}^k \lambda_i m_i + \alpha x_0$$ for some scalars $\lambda_1,\ldots, \lambda_k, \alpha$ and vectors $m_1,\ldots, m_k\in M$. If $m = \sum \lambda_i m_i$, then $m\in M$ since $M$ is a subspace of $E$. Thus $x = m + \alpha x_0$ with $m\in M$.
Suppose $x = m + \alpha x_0 = m' + \alpha' x_0$ for some $m,m'\in M$ and scalars $\alpha, \alpha'$. Then $(\alpha - \alpha')x_0 = m' - m \in M$; the condition $x_0\in E\setminus M$ forces $\alpha = \alpha'$. Then $m = m'$. Hence, the representation in the proof is unique.
Answer: The condition $x_0 \in E \setminus M$ basically means that $M$ and $\operatorname{span}\{x_0\}$ are linearly independent, and then the condition $E = \operatorname{span}(M \cup \{x_0\})$ means that $E$ is the internal direct sum of $M$ and $\operatorname{span}\{x_0\}$, so each element of $E$ can be represented uniquely as an element of $M$ plus an element of $\operatorname{span}\{x_0\}$, i.e. as $m + ax_0$ where $m \in M$ and $a \in \Bbb R$.
Comment: This is what did it for me, I was unsure of the linear independence (and how to prove it, but I gave it an attempt further down on this post). I am quite inexperienced, but in case someone else (wondering/having the same difficulties I had) comes across this post, an additional suggestion could be to directly look at the definition of the linear span.
Answer: To elaborate this post a little bit; I feel like that there is a need for a proof of $x_0$ being linearly independent to $M$, so here is my suggestion;

Corollary. for any $m \in M$ and $x_0 \in X-M$, the vectors $x_0$ and $m$ are linearly independent.

Proof. the elements (vectors) $m$ and $x_0$ are linearly independent if $am + bx_0=0$ implies $a,b=0$. Suppose $b \neq 0$ and $am + bx_0=0$. It will be shown that this leads to a contradiction.
As $b \neq 0$, the equality $am + bx_0 = 0$ is equivalent to $-\frac{am}{b}=x_0$. Furthermore, over the defined field $a,b \in \mathbb{F}$ we have $-a \in \mathbb{F}$ and $-\frac{a}{b} \in \mathbb{F}$. By the "linear" properties of the space $M$, it yields that $m'=-\frac{am}{b} \in M$.
This leads to a contradiction, as $m'=x_0$ but $m' \in M$ and $x_0 \notin M$.


What are open immersions in terms of rings?

Question: Suppose $A$ and $B$ are two commutative rings, and let $f:A\rightarrow B$ be a ring homomorphism. Suppose that the induced map on the spectra $f^*:Spec B\rightarrow Spec A$ is an open immersion. What does it say about $f$? does it follows that $f$ is a localization?
edit: Thanks for the answer! What about closed immersions? is this case simpler? Is it true that a closed immersion is always of the form $A\rightarrow A/I$ for some ideal $I$ in $A$?
Comment: The question about closed immersions is addressed in Hartshorne exercise II.2.18
Answer: See this.