Friday, March 27, 2015

ArrayHashSet, a Set in Java with random access and list iterators

Java offers a very useful Set implementation called LinkedHashSet, which keeps a parallel list to a HashSet that keeps the original insertion order available for iteration. This leads to more predictable outcomes with minimum overhead.

I've already wondered why they have not provided an analogous ArrayHashSet implementation in which an ArrayList fills the role of the parallel list, which would make middle-insertion slower, but on the other hand would offer random access.

Also, I don't know why they don't let LinkedHashSet provide a list iterator for more flexible iteration.

Anyway, given all that, I have implemented ArrayHashSet, which provides random access and list iterator. Here's the code:

/*
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this arrayList of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this arrayList of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-expresso nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.sri.ai.util.collect;

import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.ListIterator;

/**
 * Analogous to {@link LinkedHashSet}, but with an {@link ArrayList} instead of a {@link LinkedList},
 * offering the same advantages (random access) and disadvantages (slower addition and removal of elements),
 * but with the extra advantage of offering an iterator that is actually a {@link ListIterator}.
 * @author braz
 *
 * @param
 */
public class ArrayHashSet extends AbstractSet {

private HashSet   set;
private ArrayList arrayList;

public ArrayHashSet() {
this.set  = new HashSet();
this.arrayList = new ArrayList();
}

// additional random access methods

public ArrayHashSetIterator listIterator() {
return new ArrayHashSetIterator(arrayList.listIterator());
}

public ArrayHashSetIterator listIterator(int index) {
return new ArrayHashSetIterator(arrayList.listIterator(index));
}

public E get(int index) {
return listIterator(index).next();
}

public void set(int index, E element) {
ArrayHashSet.ArrayHashSetIterator listIterator = listIterator(index);
listIterator.next();
listIterator.set(element);
}

// end of additional random access methods

// required implementations

public ArrayHashSet(Collection collection) {
this();
addAll(collection);
}

@Override
public boolean add(E element) {
boolean modified = set.add(element);
if (modified) {
arrayList.add(element);
}
return modified;
}

@Override
public ArrayHashSetIterator iterator() {
return new ArrayHashSetIterator(arrayList.listIterator());
}

@Override
public int size() {
return set.size();
}

// end of required implementations

// methods not required to be implemented, but more efficient

@Override
public boolean contains(Object o) {
return set.contains(o);
}

@Override
public Object[] toArray() {
return arrayList.toArray();
}

@Override
public T[] toArray(T[] a) {
return arrayList.toArray(a);
}

@Override
public boolean remove(Object o) {
boolean removed = set.remove(o);
if (removed) {
arrayList.remove(o);
}
return removed;
}

@Override
public void clear() {
set.clear();
arrayList.clear();
}

// end of methods not required to be implemented, but more efficient

private class ArrayHashSetIterator implements ListIterator {

private ListIterator arrayListIterator;
private E lastElementProvided;

public ArrayHashSetIterator(ListIterator arrayListIterator) {
this.arrayListIterator = arrayListIterator;
}

@Override
public boolean hasNext() {
return arrayListIterator.hasNext();
}

@Override
public E next() {
return lastElementProvided = arrayListIterator.next();
}

@Override
public void add(E element) {
if (set.add(element)) {
arrayListIterator.add(element);
}
}

@Override
public boolean hasPrevious() {
return arrayListIterator.hasPrevious();
}

@Override
public int nextIndex() {
return arrayListIterator.nextIndex();
}

@Override
public E previous() {
return lastElementProvided = arrayListIterator.previous();
}

@Override
public int previousIndex() {
return arrayListIterator.previousIndex();
}

@Override
public void remove() {
arrayListIterator.remove();
set.remove(lastElementProvided);
}

@Override
public void set(E element) {
if (element.equals(lastElementProvided)) {
// no need to do anything
}
else {
if (set.contains(element)) {
// cannot add because element would appear more than once
throw new IllegalArgumentException("Cannot set already-present element in a different position in ArrayHashSet.");
}
else {
arrayListIterator.set(element);
set.remove(lastElementProvided);
set.add(element);
}
}
}
}
}


and here is a unit testing class:

/*
 * Copyright (c) 2013, SRI International
 * All rights reserved.
 * Licensed under the The BSD 3-Clause License;
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 * 
 * http://opensource.org/licenses/BSD-3-Clause
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 
 * Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * Neither the name of the aic-util nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.sri.ai.test.util.collect;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.NoSuchElementException;

import org.junit.Before;
import org.junit.Test;

import com.sri.ai.util.Util;
import com.sri.ai.util.collect.ArrayHashSet;

public class ArrayHashSetTest {

@Before
public void setUp() throws Exception {
}

@Test
public void test() {

ArrayHashSet set;

set = new ArrayHashSet();
assertTrue(set.isEmpty());
assertEquals(set.size(), 0);

assertEquals(Util.list(), Util.listFrom(set.iterator())); // tests iteration order

set.add("tree");
assertFalse(set.isEmpty());
assertEquals(set.size(), 1);
assertEquals("tree", set.get(0));

set.add("almond");
assertFalse(set.isEmpty());
assertEquals(set.size(), 2);
assertEquals("tree", set.get(0));
assertEquals("almond", set.get(1));

assertEquals(Util.list("tree", "almond"), Util.listFrom(set.iterator())); // tests iteration order

set.clear();
assertTrue(set.isEmpty());
assertEquals(set.size(), 0);

set.add("almond");
assertFalse(set.isEmpty());
assertEquals(set.size(), 1);
assertEquals("almond", set.get(0));

set.add("tree");
assertFalse(set.isEmpty());
assertEquals(set.size(), 2);
assertEquals("almond", set.get(0));
assertEquals("tree", set.get(1));

set.add("leaf");
assertFalse(set.isEmpty());
assertEquals(set.size(), 3);
assertEquals("almond", set.get(0));
assertEquals("tree", set.get(1));
assertEquals("leaf", set.get(2));

assertEquals(Util.list("almond", "tree", "leaf"), Util.listFrom(set.iterator())); // tests iteration order

set.remove("tree");
assertFalse(set.isEmpty());
assertEquals(set.size(), 2);
assertEquals("almond", set.get(0));
assertEquals("leaf", set.get(1));

assertEquals(Util.list("almond", "leaf"), Util.listFrom(set.iterator())); // tests iteration order

set.remove("leaf");
assertFalse(set.isEmpty());
assertEquals(set.size(), 1);
assertEquals("almond", set.get(0));

assertEquals(Util.list("almond"), Util.listFrom(set.iterator())); // tests iteration order

set.remove("almond");
assertTrue(set.isEmpty());
assertEquals(set.size(), 0);

assertEquals(Util.list(), Util.listFrom(set.iterator())); // tests iteration order

set.add("almond");
set.add("tree");
set.add("tree");
set.add("almond");
set.add("leaf");
set.add("tree");

assertEquals("almond", set.get(0));
assertEquals("tree", set.get(1));
assertEquals("leaf", set.get(2));
assertEquals(Util.list("almond", "tree", "leaf"), Util.listFrom(set.iterator())); // tests iteration order

set.set(2, "trunk");
assertEquals("almond", set.get(0));
assertEquals("tree", set.get(1));
assertEquals("trunk", set.get(2));
assertEquals(Util.list("almond", "tree", "trunk"), Util.listFrom(set.iterator())); // tests iteration order

set.set(2, "leaf");

assertEquals("almond", set.get(0));
assertEquals("tree", set.get(1));
assertEquals("leaf", set.get(2));
assertEquals(Util.list("almond", "tree", "leaf"), Util.listFrom(set.iterator())); // tests iteration order

set.set(2, "leaf");

assertEquals("almond", set.get(0));
assertEquals("tree", set.get(1));
assertEquals("leaf", set.get(2));
assertEquals(Util.list("almond", "tree", "leaf"), Util.listFrom(set.iterator())); // tests iteration order

try {
set.set(2, "almond"); // cannot add an already-present element in a different position
fail("Should have thrown an exception");
}
catch (IllegalArgumentException e) {
// good, did what it had to do
}
catch (Exception e) {
fail("Should have thrown an IllegalArgumentException but threw " + e);
}

try {
set.set(3, "almond"); // cannot set a non-existing position
fail("Should have thrown an exception");
}
catch (NoSuchElementException e) {
// good, did what it had to do
}
catch (Exception e) {
fail("Should have thrown an NoSuchElementException but threw " + e);
}

// should remain the same after failed operations
assertEquals("almond", set.get(0));
assertEquals("tree", set.get(1));
assertEquals("leaf", set.get(2));
assertEquals(Util.list("almond", "tree", "leaf"), Util.listFrom(set.iterator())); // tests iteration order
}
}


Monday, June 16, 2014

Resolução de equações de segundo grau através de fatoração

Escrevi uma explicação sobre a resolução de equações de segundo grau para o meu sobrinho que pode ser útil para outras pessoas:

Primeiro precisamos relembrar fatoração. Temos DOIS casos de fatoração relevante aqui:

Método de fatoração No1 - trinômio:

É quando temos uma expressão:

x^2 +Sx + P, onde S é a soma de dois números a e b, e P seu produto.

Por exemplo

x^2 + x - 6           (x^2 quer dizer "x ao quadrado")

Nesse caso S = 1 (invisível, multiplicando o x, e P é -6). Pensando nas possibilidades, dois números com soma 1 e produto -6 são 3 e -2. Então x^2 + x - 6 = (x + 3)(x - 2)

Podemos dizer isso porque, aplicando a distributiva,

(x + 3)(x - 2) = x^2 + 3x -2x -6 = x^2 + (3 - 2)x - 6

Ou seja, os números a e b iguais a 3 e -2, quando aplicamos a distributiva, multiplicam o x e são somados, gerando S igual a 1, e sozinhos, sem multiplicar o x, são multiplicados, gerando P igual a -6.

Método de fatoração No 2 - trinômio quadrado perfeito:

É um caso particular do primeiro método, onde a e b são o mesmo número (vamos chamar esse valor comum de d).

Se temos x^2 + 2dx + d^2, isso é o mesmo que (x + d)^2

Por que esse é um caso particular do primeiro método? Por que acima temos que S = 2d, e P é igual a d^2. Os números a e b que geram essa soma e produto são d e d (somados dão 2d e multiplicados dão d^2).

Na verdade, como esse método é um caso particular do primeiro, se você lembrar só do primeiro método usando S e P, já dá pra resolver os dois casos. Por exemplo, se você ver:

x^2 + 6x + 9

e usar S e P, vai achar 3 e 3. Isso é o mesmo que você acharia usando o trinômio quadrado perfeito, pois 6 = 2.d = 2.3 e 9 = d^2 = 3^2.

Portanto

x^2 + 6x + 9 = (x + 3)(x + 3)

Note que os casos acima SÓ funcionam quando o x^2 é multiplicado por 1. Se tivermos um caso, por exemplo com 3 multiplicando o x^2:

3x^2 + 3x - 6

temos que primeiro fatorar o número que multiplica o x^2 (fazer a distributiva ao contrário):

3x^2 + 3x - 6 = 3 ( x^2 + x - 2)

Agora o que está dentro do parênteses pode ser fatorado de acordo com S e P. Achamos os números 3 e -2. Assim, reescrevemos o acima como:

3 ( x^2 + x - 2) = 3 ( (x + 3)(x - 2) ) = que é o mesmo que 3 vezes (x + 3) vezes (x - 2) 3(x + 3)(x - 2)

Note que o fato pra passar de 3 ( (x + 3)(x - 2) ) para 3(x + 3)(x - 2) pode ter te dado a impressão de deveria ter usado a distributiva porque o (x + 3)(x - 2) estava entre parênteses, mas isso não é correto porque a distributiva SÓ se aplica quando o que está entre parêntes é uma SOMA ou SUBTRAÇÃO.

Bom, ok, o que está acima é o que precisamos relembrar sobre fatoração. Agora vamos ver como podemos usar isso para resolver equações de segundo grau.

O que é uma equação de segundo grau? É quando temos uma igualdade (=) envolvendo uma variável, e essa variável aparece elevada ao quadrado. Alguns exemplos são:

3x^2 + 2x + 1 = x^2 + x
3x^2 + 2x + 1 = 0
3x^2 + 1 = x^2 + x
x^2 = 0 x^2 - 5 = 0

Todas essas são equações do segundo grau porque o x aparece elevado ao quadrado, mesmo que o x sem ser ao quadrado também apareça.

Pra resolver uma equação de segundo grau, primeiro passamos tudo para um lado e deixamos o 0 sozinho do outro lado:

3x^2 + 2x + 1 = x^2 + x - 3
3x^2 + 2x + 1 - x^2 - x + 3 = 0

depois reduzimos os termos semelhantes, isso é, somamos tudo que multiplica o x^2 , o que multiplica o x, e os números sem x:

3x^2 + 2x + 1 - x^2 - x + 3 = 0
(3 - 1)x^2 + (2 - 1)x + 1 + 3 = 0
2x^2 + 1x + 4 = 0
2x^2 + x + 4 = 0

Depois que isolamos o zero e reduzimos os termos semelhantes, SEMPRE teremos alguém multiplicando o x^2, alguém multiplicando o x, e um número sozinho. Chamamos o multiplicador de x^2 de a, o multiplicado de x de b, e o número sozinho de c.

2x^2 + x + 4 = 0
tem a = 2, b = 1 e c = 4

Quando um ou mais termos não aparece, é porque ele é 0. Por exemplo, 3x^2 + 2x = 0

tem a = 3 e b = 2, mas qual o c? É 0, porque o acima é o mesmo que

3x^2 + 2x + 0 = 0

ou seja, o numero sozinho é 0.

Outro exemplo:

2x^2 + 3 = 0

Desta vez esta faltando o termo com x, mas o acima é igual a

2x^2 + 0.x + 3 = 0

ou, seja, é facil ver que o b nesse caso é 0.

As vezes tanto o b quanto o c são zero:

x^2 = 0

tem b e c iguais a zero, pois o acima é igual a

x^2 + 0.x + 0 = 0

Lembre-se de prestar atenção nos numeros negativos:

-2x^2 + 3x -1 = 0
tem a = -2, b igual a 3 e c igual a -1

Ou seja, é preciso prestar atenção nos sinais.

Note que o a nunca será 0, pois se estiver faltando um termo com x^2, então nem temos uma equação de 2o grau, que sempre exige um termo com x^2 !

Muito bem, agora sabemos "extrair" os coeficientes a, b, e c de uma equação de segundo grau. Pra resumir, jogamos tudo pro lado esquerdo da equação, reduzimos (somamos os multiplicadores de termos semelhantes) e identificamos o a, b e c como multiplicadores de x^2, x e números sozinhos.

Agora podemos estudar varios casos pra solucionar equações de segundo grau:

Caso 1:

b e c são zero:

Nesse caso temos apenas:

a.x^2 = 0

então

x^2 = 0/a
x^2 = 0
x = raiz(0)        (raiz(alguma coisa) é a notação que uso para a raiz do que vem dentro dos ()s)
x = 0

Esse caso cobre os seguintes exemplos, entre outros:

5x^2 = 0
-3x^2 = 0
-x^2 = 0

O x é 0 em todos os casos que caem no Caso 1, portanto o conjunto solução é S = { 0 }.

Caso 2: 

 O c é zero, mas o b não é:

ax^2 + bx = 0

note que quando é assim podemos fatorar o x:

x( a.x + b) = 0

Ou seja, x vezes (ax + b) é igual a zero. O único jeito de multiplicar duas coisas e dar 0 é se pelo menos uma dessas coisas é igual a zero. Isso quer dizer que ou o x é 0, ou (ax + b) é zero.

Se o x é 0, então pronto, achamos uma solução. Mas se ele não for, outra solução é dada a partir de

ax + b = 0

que é uma equação de PRIMEIRO grau que já sabemos resolver. Fazemos assim:

ax = - b x = -b/a

Ou seja, -b/a é outra soluçãp. Voce nao precisa decorar essa formula se não quiser, basta lembrar de fatorar o x quando o c for zero, ver que x = 0 é uma solução e que a solução da equação do primeiro grau deve ser outra:

Exemplo no caso 2:

x^2 - 4x = 0
x( x - 4) = 0

Então x = 0 é solução e x - 4 = 0, ou seja x = 4 também é solução. Portanto, o conjunto solução é S = {0, 4} (dois valores para x que tornam a equacao verdadeira).

Outro exemplo:

2x^2 + 3x = 0
x ( 2x + 3) = 0
então

x = 0 ou

2x + 3 = 0

qie significa que

x = -3/2

Portanto, S = {0, -3/2}.

Caso 3:

b é zero e c não é zero.

Entao temos

ax^2 + c = 0
ax^2 = - c
x^2 = -c/a
x = + ou - raiz(-c/a)

O "+ ou -" significa que o -c/a funciona como solução tanto do jeito que está quando com o sinal invertido. Isso vai ficar mais claro nos exemplos abaixo.

Nesse caso tambem não precisa decorar a fórmula, basta repetir esse raciocínio quando ver que b é 0. Por exemplo:

2^x^2 - 8 = 0
2^x^2 = 8
x^2 = 8/2
x^2 = 4
x = + ou - raiz(4)
x = 2 ou x = -2

Aqui entendemos porque o "+ ou -". Isso acontece porque tanto a raiz(4), que é 2, serve de solução, quanto o negativo dela, -2. Podemos ver isso simplesmente substituindo o valor do x com esses dois valores:

x^2 = 4
(2)^2 = 4
4 = 4, portanto 2 é solução.

Agora vamos testar o -2:
x^2 = 4
(-2)^2 = 4
4 = 4, também funciona, pois o quadrado de -2 também é 4.

Os dois valores, 2 e -2, tornam a equação verdadeira, portanto S = {-2, 2}

Caso 4:

a = 1, mas nem o b nem o c são zero.

Nesse caso é um pouco mais difícil, pois precisamos fatorar o termo da esquerda:

x^2 + x - 6 = 0

De acordo com o caso S e P, podemos escrever o lado da esquerda como:

(x + 3)(x - 2) = 0

Lembra o que falamos sobre o único jeito de dois números se multiplicarem dar zero é se pelo menos um for zero? Então isso quer dizer que

x + 3 = 0 ou x - 2 = 0

Temos agora duas equações de primeiro grau, cada uma dando uma solução. A primeira dá x = -3 e a segunda dá x = 2, portanto S = {-3, 2}. Ou seja, tanto x = -3 quanto x = 2 tornam a equação verdadeira.

Mais um exemplo, dessa vez com a regra do trinômio quadrado perfeito:

x^2 - 6x + 9

Como S = -6 é o mesmo que (-3) + (-3),  e P = 9 é o mesmo que (-3).(-3), temos

(x - 3)^2 = 0
que é o mesmo que
(x - 3)(x - 3) = 0

Caímos de novo na situação em que duas coisas multiplicadas são 0 e portanto uma delas deve ser zero. Mas na verdade essas "duas coisas" são a "mesma coisa", (x-3), só que duplicada. Isso quer dizer que x - 3 deve ser zero para que essa equação funcione e seja verdadeira. x - 3 = 0 portanto x = 3.

Essa é a única solução, portanto S = {3}.

Caso 5:

Nem b nem c são zero, e a não é 1.

O Caso 4 só funciona quando a = 1, porque a fatoração só funciona quando o x^2 não é multiplicado por ninguém. Porem podemos lidar com esse caso desse jeito aqui:

ax^2 + bx + c = 0
e o a não é 1.

Se multiplicarmos a equacao dos dois lados por 1/a, cancelaremos aquele a!

1/a ( ax^2 + bx + c ) = 1/a . 0
1/a ( ax^2 + bx + c ) = 0       (pois 1/a . 0 dá 0)
1/a . ax^2 + 1/a . bx + 1/a . c = 0      (distributiva)
ax^2 / a + (b/a)x + c/a = 0      (produto de frações)
x^2 + (b/a)x + c/a = 0           (cancelamos o a "de baixo"  com o a multiplicando o x^2)

Agora temos uma nova equacão do segundo grau. Quem são o a, b, e c dessa nova equacao? Vamos chama-los de a', b' e c' pra não confundir com o a, b, e c originais.

O a' é quem multiplica o x^2, ou seja , a' = 1.
b' é quem multiplica o x, portanto b' = b/a
c'
é quem está sozinho, sem x nenhum, ou seja, c' = c/a.

Como o a' dessa equação é 1 podemos usar o Caso 4 nela. Ou seja, o Caso 5 nos ajuda a transformar o problema em um novo problema que podemos resolver com o Caso 4.

Vamos ver alguns exemplos:

2x^2 + 10x + 8 = 0

Como a não é 1, usamos o Caso 5:

1/2 (2x^2 + 10x + 8) = 1/2 . 0

Usando as mesmas regras que usamos antes:

1/2 (2x^2 + 10x + 8) = 1/2 . 0
1/2 . 2x^2 + 1/2 . 10x + 1/2 . 8 = 0
x^2 + 5x + 4 = 0

(note que multiplicar por 1/2 é a mesma coisa que dividir por 2, e de fato foi esse o efeito, cada coeficiente passou a ter a metade do valor).

Agora temos uma equação na qual podemos usar o Caso 4! S = 5 e P = 4 são soma e produto de 4 e 1, portanto

(x + 4)(x + 1) = 0
o que leva a dizer que
x + 4 = 0
ou
x + 1 = 0
e portanto
x = -4
ou
x = -1

Assim, S = {-4, -1}.

Wednesday, February 9, 2011

How to pin a document to the taskbar in Windows 7, instead of to the jump list of the corresponding application

When you drag a document to the taskbar in Windows 7, it will add that document to the jump list associated to the corresponding application (if the application is not there already, it places it there). The problem with it is that, to access the document, you need to right-click the application icon to show the jump list, and then click on the document. It takes too long. Sometimes you want to have super quick access to that one document (while we are at it, it may be worth mentioning that Win+1, Win+2 etc give access to items in the taskbar -- another very quick way of accessing them).

A basic solution is to create a shortcut for the application and add the corresponding document as an argument to the shortcut. See the steps below for details. This was elaborated from this page (here's a version with lots of screen shots).

- Go to document of interest, right-click on it, go to Properties, copy and paste its location, that is, its path;
- Drag the application icon (from, say, the Start Menu) to the Desktop to make a shortcut;
- Right-click on application shortcut, go to Properties, and paste, between quotes, the path to the file (see example below);
- Add, the the file path, the name of the file (because the location in its Properties window contains only the path, not the name, of the file).

Example:

When you open the shortcut properties, you may seem something like:

"C:/Program Files/Application.exe"

Make that
"C:/Program Files/Application.exe" "C:/Users/John/Documents/FileOfInterest.xxx"

PS: This assumes that the application takes a document full path and name as a first argument, and opens it. This is the case for most applications, but not necessarily all. In the case this is not true, you would have to find out how to open a file with that application from the command prompt (because using a shortcut is like running a command from the command prompt).

Monday, March 30, 2009

Online financial software: a strange mixture of brilliance and stupidity

I have been checking online financial software and I am scandalized. How can a development team do such great work and then ruin with with one or two very retarded things?

All these sites are very slick-looking, using modern Web technology and smart interfaces that simplify entering data and navigating around. They have gadgets, widgets and their pages are highly configurable. Some of them have mobile software clients. The interface is intuitive and looks great. You have lots of ways for doing the same thing. The graphics are pretty polished. And then something really stupid comes along.

Mint.com and Quicken Online, for example, won't let you go ahead with the application unless you have managed to establish a connection with a financial institution. What if I don't want to do that and just want to enter transactions manually?

Buxfer.com is great, very light and intuitive looking. I even subscribed to a Pro membership because I thought I had everything I needed. Then... its financial projections don't go further than three months into the future! What the... ?

ClearCheckbook.com is also very slick, but won't take recurring transaction into account before they actually take place. So much for future projections!

But the top prize goes to Geezeo.com. It won't let you enter transactions manually, period. You need to use an export file from your financial institution, or delete the account.

The funny thing about recurring, future transactions is that they take no extra programming effort to create. They have a date, just like any other transaction, that has to be taken into account when generating reports with date ranges that include them, that is, reports about the future. The really stupid thing is that these development teams, all five of them, have gone to pains to prevent the software from doing this very important thing. Mind boggling.

Monday, September 8, 2008

Two good free games: Alien Wars and Real Racing

The web site gametop.com gives away some pretty good games. While these are free, full downloadable games, they are not malware. The only thing they do is loading the site's home page when you leave them.

If you like space shooters, I highly recommend Alien Wars. It is a typical shooter with a variety of stages, weapons and enemies, but with really well done graphics, sound effects, addictive music and balanced, engaging action.

This game, by the way, is exactly the same game as "Alien Stars", which the maker sells on their own web site.

Another great title available at the site is Real Racing, an addictive and cute car racing game.

Thursday, September 4, 2008

Taskbar Shuffle - reordering your Windows taskbar buttons and system tray icons

I have wished for a long time for a way to move my Windows taskbar buttons around. Since I usually have quite a lot of open applications, it would be convenient to have the most used ones in the same place most of the time, so I don't have to look for them every time I need them. It would also be nice to order buttons by subject, or importance during a task.

I did try searching for software doing that. What I had in mind was some kind of taskbar alternative. I tried ObjectDock, RocketDock and such, but they didn't do anything like what I wanted. So I eventually gave up.

Then I stumbled upon a solution by sheer luck. When I saw the name "Taskbar Shuffle" in some random freeware web site, I knew I had found it.

It was even better than I had expected. I didn't really want a taskbar alternative. I just thought I needed one if I wanted different functionality. Turns out Taskbar Shuffle doesn't take anything out. Everything looks and works exactly the same, but you can now drag taskbar buttons. And it allows you to do the same with the system tray icons, although I never actually use that.

It seems to have other options, such as tweaking the button grouping behavior. Since I don't use that anyway, I never checked. What I really wanted was this one thing, moving the buttons, and I got it without losing anything else. Just perfect.

Ditto, an open-source Windows clipboard extension

The Australian aborigines believe your Windows clipboard is an extension of your soul. Freud conjectured it plays the role of a computer's subconscious.

If so, then Ditto is both mind-expanding and soul-searching.

How many times have you wished you hadn't copied something into your clipboard and as a result lost that precious bit of information sitting there before? Or that you could copy several separate bits of info from one application before going to another to paste them?

Your clipboard will do the usual Ctrl-X, -C and -V after you install Ditto. However Ctrl-` will open a window showing all previous items that were there before, so you don't lose anything. You can also search for items, associate shortcuts to them etc, although I personally only use the main functionality.

Ditto is extremely unobtrusive. In fact I had to look its name up before posting this because I haven't thought of it for months. It is also highly configurable, although I haven't spent time fiddling with its options; it simply works.