text:
Search by Topic

Resources tagged with Interactivities similar to Slow Coach:

Filter by: Content type:
Stage:
Challenge level:

There are 219 results

Broad Topics > Information and Communications Technology > Interactivities

An Unhappy End

Stage: 3 Challenge Level:

Two engines, at opposite ends of a single track railway line, set off towards one another just as a fly, sitting on the front of one of the engines, sets off flying along the railway line...

You Tell the Story

Stage: 2 Challenge Level:

Can you create a story that would describe the movement of the man shown on these graphs? Use the interactivity to try out our ideas.

Up and Across

Stage: 3 Challenge Level:

Experiment with the interactivity of "rolling" regular polygons, and explore how the different positions of the red dot affects its vertical and horizontal movement at each stage.

Take the Right Angle

Stage: 2 Challenge Level:

How many times in twelve hours do the hands of a clock form a right angle? Use the interactivity to check your answers.

Do You Measure Up?

Stage: 2 Challenge Level:

A game for two or more players that uses a knowledge of measuring tools. Spin the spinner and identify which jobs can be done with the measuring tool shown.

Take Your Dog for a Walk

Stage: 2 Challenge Level:

Use the interactivity to move Mr Pearson and his dog. Can you move him so that the graph shows a curve?

Stage: 2 Challenge Level:

If you have only four weights, where could you place them in order to balance this equaliser?

Speeding Up, Slowing Down

Stage: 3 Challenge Level:

Experiment with the interactivity of "rolling" regular polygons, and explore how the different positions of the red dot affects its speed at each stage.

How Far Does it Move?

Stage: 3 Challenge Level:

Experiment with the interactivity of "rolling" regular polygons, and explore how the different positions of the red dot affects the distance it travels at each stage.

Fault-free Rectangles

Stage: 2 Challenge Level:

Find out what a "fault-free" rectangle is and try to make some of your own.

Cogs

Stage: 3 Challenge Level:

A and B are two interlocking cogwheels having p teeth and q teeth respectively. One tooth on B is painted red. Find the values of p and q for which the red tooth on B contacts every gap on the. . . .

Red Even

Stage: 2 Challenge Level:

You have 4 red and 5 blue counters. How many ways can they be placed on a 3 by 3 grid so that all the rows columns and diagonals have an even number of red counters?

A Dotty Problem

Stage: 2 Challenge Level:

Starting with the number 180, take away 9 again and again, joining up the dots as you go. Watch out - don't join all the dots!

Board Block Challenge

Stage: 2 Challenge Level:

Choose the size of your pegboard and the shapes you can make. Can you work out the strategies needed to block your opponent?

Teddy Town

Stage: 1 and 2 Challenge Level:

There are nine teddies in Teddy Town - three red, three blue and three yellow. There are also nine houses, three of each colour. Can you put them on the map of Teddy Town according to the rules?

Combining Cuisenaire

Stage: 2 Challenge Level:

Can you find all the different ways of lining up these Cuisenaire rods?

Tetrafit

Stage: 2 Challenge Level:

A tetromino is made up of four squares joined edge to edge. Can this tetromino, together with 15 copies of itself, be used to cover an eight by eight chessboard?

Colour in the Square

Stage: 2 Challenge Level:

Can you put the 25 coloured tiles into the 5 x 5 square so that no column, no row and no diagonal line have tiles of the same colour in them?

Coordinate Tan

Stage: 2 Challenge Level:

What are the coordinates of the coloured dots that mark out the tangram? Try changing the position of the origin. What happens to the coordinates now?

Factor Lines

Stage: 2 Challenge Level:

Arrange the four number cards on the grid, according to the rules, to make a diagonal, vertical or horizontal line.

Odds or Sixes?

Stage: 2 Challenge Level:

Use the interactivity or play this dice game yourself. How could you make it fair?

Fractions and Coins Game

Stage: 2 Challenge Level:

Work out the fractions to match the cards with the same amount of money.

Coin Cogs

Stage: 2 Challenge Level:

Can you work out what is wrong with the cogs on a UK 2 pound coin?

Rod Ratios

Stage: 2 Challenge Level:

Use the Cuisenaire rods environment to investigate ratio. Can you find pairs of rods in the ratio 3:2? How about 9:6?

Nine Colours

Stage: 3 Challenge Level:

You have 27 small cubes, 3 each of nine colours. Use the small cubes to make a 3 by 3 by 3 cube so that each face of the bigger cube contains one of every colour.

Nine-pin Triangles

Stage: 2 Challenge Level:

How many different triangles can you make on a circular pegboard that has nine pegs?

Square Coordinates

Stage: 3 Challenge Level:

A tilted square is a square with no horizontal sides. Can you devise a general instruction for the construction of a square when you are given just one of its sides?

Isosceles Triangles

Stage: 3 Challenge Level:

Draw some isosceles triangles with an area of $9$cm$^2$ and a vertex at (20,20). If all the vertices must have whole number coordinates, how many is it possible to draw?

Number Differences

Stage: 2 Challenge Level:

Place the numbers from 1 to 9 in the squares below so that the difference between joined squares is odd. How many different ways can you do this?

Diamond Mine

Stage: 3 Challenge Level:

Practise your diamond mining skills and your x,y coordination in this homage to Pacman.

Part the Piles

Stage: 2 Challenge Level:

Try to stop your opponent from being able to split the piles of counters into unequal numbers. Can you find a strategy?

Partitioning Revisited

Stage: 3 Challenge Level:

We can show that (x + 1)² = x² + 2x + 1 by considering the area of an (x + 1) by (x + 1) square. Show in a similar way that (x + 2)² = x² + 4x + 4

Lost

Stage: 3 Challenge Level:

Can you locate the lost giraffe? Input coordinates to help you search and find the giraffe in the fewest guesses.

Shuffles Tutorials

Stage: 3 Challenge Level:

Learn how to use the Shuffles interactivity by running through these tutorial demonstrations.

Round Peg Board

Stage: 1 and 2 Challenge Level:

A generic circular pegboard resource.

Picturing Triangle Numbers

Stage: 3 Challenge Level:

Triangle numbers can be represented by a triangular array of squares. What do you notice about the sum of identical triangle numbers?

Triangles All Around

Stage: 2 Challenge Level:

Can you find all the different triangles on these peg boards, and find their angles?

Sorting Symmetries

Stage: 2 Challenge Level:

Find out how we can describe the "symmetries" of this triangle and investigate some combinations of rotating and flipping it.

Train

Stage: 2 Challenge Level:

A train building game for 2 players.

Subtended Angles

Stage: 3 Challenge Level:

What is the relationship between the angle at the centre and the angles at the circumference, for angles which stand on the same arc? Can you prove it?

Gr8 Coach

Stage: 3 Challenge Level:

Can you coach your rowing eight to win?

Triangle Pin-down

Stage: 2 Challenge Level:

Use the interactivity to investigate what kinds of triangles can be drawn on peg boards with different numbers of pegs.

Code Breaker

Stage: 2 Challenge Level:

This problem is based on a code using two different prime numbers less than 10. You'll need to multiply them together and shift the alphabet forwards by the result. Can you decipher the code?

World of Tan 24 - Clocks

Stage: 2 Challenge Level:

Can you fit the tangram pieces into the outlines of these clocks?

World of Tan 25 - Pentominoes

Stage: 2 Challenge Level:

Can you fit the tangram pieces into the outlines of these people?

World of Tan 26 - Old Chestnut

Stage: 2 Challenge Level:

Can you fit the tangram pieces into the outline of this brazier for roasting chestnuts?

World of Tan 22 - an Appealing Stroll

Stage: 2 Challenge Level:

Can you fit the tangram pieces into the outline of the child walking home from school?

World of Tan 21 - Almost There Now

Stage: 2 Challenge Level:

Can you fit the tangram pieces into the outlines of the lobster, yacht and cyclist?

Domino Numbers

Stage: 2 Challenge Level:

Can you see why 2 by 2 could be 5? Can you predict what 2 by 10 will be?

Sliding Puzzle

Stage: 1, 2, 3 and 4 Challenge Level:

The aim of the game is to slide the green square from the top right hand corner to the bottom left hand corner in the least number of moves.


text:
## Monday, May 31, 2010

### Estimation of Safe Walking Route using Google Street View

Woman Follows Google Maps “Walking” Directions, Gets Hit, Sues. Despite how she claims that her blackberry did not display the usual warning “Walking directions are in beta. Use caution – This route may be missing sidewalks or pedestrian paths,” anyone with a common sense could see from street view that there is no sidewalk, and the path might be dangerous for a pedestrian to cross.

But can Google use street view to automatically assess the walkability of a path? Maybe. At least on busy streets, you will find pedestrians. A lot of image recognition research has gone into detecting people in an image, so if you detect people in a street view, it is likely a walkable street. Even if there is no people, it might be possible to detect common pavement painting pattern such as pedestrian cross-walk, or pedestrian walk lights. A street with those computer detectable elements is very likely to be walk-safe.

### GitFarm source code?

It is a popular request to GitFarm for its source code, but these requests are mostly ignored by its provider, Bo (yboycn). GitFarm allows you host a Git repository on Google AppEngine. It turns out that if you know a bit of Java, you can put together something like GitFarm over the weekend. Most of the pieces you need are already available as open source.
• JGit, Git implemented in Java (EDL licensed).
• GaeVFS, a filesystem for Google AppEngine using datastore and memcache API (Apache licensed).
And if you build your Git hosting on AppEngine that way, you are not compelled to publish your source code. Both EDL and Apache License are only triggered by distributing source or object code, not software as a service. You are not even compelled to reproduce a copyright notice for providing software as a service.

## Friday, May 28, 2010

### Tracking Object Ownership in C++

Ever since the beginning when people program in C/C++, the need to track object ownership is recognized, but there has been a lot of confusions. We need ownership tracking in order to know who is responsible for freeing an object. This is essential for preventing memory leaks, since C++ does not typically use garbage collection (though it can use something like Boehm GC). Ownership tracking is also essential for preventing double-free, which can corrupt the memory allocator's internal data structure.

Ownership tracking does not sufficiently prevent all memory related error. For example, another part of the program could retain an aliased const reference to an object and use it after the true owner frees it. After the memory occupied by the object is returned to the allocator, it could be subsequently used for other purpose, which could be corrupted when someone modifies the aliased original object that no longer exists.

We will need to use the concept of linearity if we also want to prevent aliasing, but a lot of seasoned C++ programmers don't understand this. It is evident, for example, from their disdain about std::auto_ptr<>: when an STL algorithm such as sort() is broken when you use auto_ptr, they blame auto_ptr for the culprit. However, I would argue that the culprit is sort(), which creates an alias for an item in the container and abandons it without proof that it is safe to do so (perhaps because by default, C++ treats everything as plain old data, which means copying and assignment has no side-effect). If an algorithm observes object linearity, then it will work with auto_ptr<>. There has been significant research in the programming language ATS which strictly enforces linearity in its type system, and there is a lot of code implementing common algorithms that satisfy linearity.

While it is great if you can use a new language with built-in linearity checking, what if you're stuck with C++ (e.g. I miss template meta-programming and curiously recurring template pattern)?

The real problem I have with auto_ptr<> is that it automatically frees the object when the auto_ptr<> object is destructed. This means that non-linear code can run for a while, erroneously freeing objects until everything mysteriously disappears, and you will be puzzled by what is going on. Distinguishing move semantic and copy semantic is a start: it alleviates the need of the auto_ptr_ref hack (otherwise auto_ptr<> cannot be returned from functions due to the non-const copying constructor). But move semantics doesn't enforce linearity; items can still mysteriously disappear after they're moved out of the container but not moved back in.

C++ type system does not have the power to enforce linearity during compile time, but the next best thing to do is to make programs crash early when non-linearity is detected. I've been using a home-brew linear_ptr<> class which the only difference from auto_ptr<> is that, whenever there is a delete p, I remove it and replace it with assert(p == NULL). Regardless of move or copy semantics, a linear pointer only tracks the ownership but never frees an object. I've been using this ubiquitously in an implementation of a memory allocator with great success.

In using linear_ptr<>, I came to realize that a lot of times, a function or method doesn't intend to take ownership of the object passed in as an argument, nor does it alias the object pointer, but we want to have call-by-reference argument semantics. In these cases, I use the idiom const linear_ptr<T>& which means that the pointer itself should not be modified (still points to the same object, not taking ownership) but the object itself is non-const, so it could be mutated.

It turns out that in a regular C++ program, you don't need to use linear pointer to express the intention that a function does not intend to take ownership of an object. Just use a reference! The key idea is that delete takes a pointer, not a reference. Since a reference cannot be deleted, it signifies the fact that the caller does not pass ownership of the object to the function. Furthermore, it signifies that the reference cannot be NULL, so the object has to exist prior to the call. The caller loans the object to the function; the function borrows the object from the caller. Of course, the function should still refrain from converting the reference to a pointer or making an alias of the reference, but it is much easier for a programmer to self-discipline in a local context of a function.

The role of references in C++ is often under-appreciated. For example, Google C++ Style Guide imposes a rule on references that prevents you from writing a function that wishes to mutate an object but will not claim ownership.

However, it is important to note that, unlike pointers, C++ references cannot be reassigned. Assignment on a reference will be applied on the object being referenced to, not the reference itself. This means that if you want to repeatedly borrow objects in a recursively defined data structure (e.g. singly linked list, binary search tree), you will have to write recursive functions rather than doing it in a loop.

In the case where you really want to write a loop that repeatedly borrows objects from a data structure, you can use a loan pointer, where the object pointer is cast to a pointer of a loan class of that object. A loan class is a mixin that merely disables the constructor and destructor by making them private.
template<class superTp>
class loan : public superTp {
private:
// Disallow constructor and destructor.
loan();
~loan();
};

It most certainly should disallow copy constructor as well, but this is left for an exercise to the reader. When we want to signify the intention to retain ownership, we will make a loan pointer using the following function:
template<class superTp>
loan<superTp> *make_loan(superTp *objp) {
return static_cast<loan<superTp>*>(objp);
}

A similar function can be written for linear_ptr<> as well, to convert from a const linear_ptr<T>& to loan<T> *.

A loan pointer can be reassigned to reference a different object, but the usual caution about aliasing applies. A function should not retain the alias beyond the scope of the function. I rarely use the loan<T> * idiom myself because C++ reference works most of the times for me to write a function with the intent not to claim object ownership from the owner.

In conclusion, yes, there is a difference between pointer and reference in C++, and the difference is often under-appreciated. You may use pointer argument to signal the intent of the function to take ownership from the caller; but when you use a reference argument, the caller keeps ownership, and the function merely borrows the object for use in the function scope. This takes care of ownership dispute in function call. However, outside of a function call, auto_ptr<> is not enough to enforce linearity in code. Since C++ type system does not statically enforce linearity, the next best alternative is to use linear_ptr<> that detects non-linearity and crashes the program in run-time the earliest possible. These facilities can be used together to help programmers write linear code that is free of memory leak and memory corruption problems due to double-free and free-after-use.

## Wednesday, May 26, 2010

### Passing File Descriptor over Unix Domain Sockets

This is a follow-up to my rant in Secrets (everyone should know) About Running Web Servers. I made a few recommendations about structuring a server written in C/C++ and in Java. Namely, the server will consist of several worker processes, and a monitor process that dispatches the work. The overall design is the same for C/C++ and Java, but the motivation and rationale are different.
• C/C++: since memory leak for programs written in this language is unavoidable, there should be a way for a worker process to restart periodically (after a certain number of requests). We need to be able to "switch on" and "switch off" any worker process in a pool of processes without exposing downtime.
• Java: multi-threading interacts poorly with generational garbage collection, so if we don't want to resolve to fudging nursery size, worker process should be single threaded. However, we will be running several concurrent worker processes so we can take advantage of the multi-processor hardware.
Both of these require having a lightweight "monitor" process that listens for network sockets, passes an incoming connection to one of those workers, and monitor the health of these workers. We don't want to fork on socket accept() because some application's startup cost could be quite high (e.g. JVM).

It might be tempting to use FastCGI, but it turns out that the FastCGI protocol requires copying from worker to the monitor, which copies the response back to the client. We want to avoid any redundant copying at all cost. This means once the monitor accepts a listening socket, it will pass the socket directly to a worker, and leave it up to the worker to communicate with the client.

It turns out that folks who designed BSD Unix had already foreseen this scenario. Nowadays, it is possible to do so at least on AIX and on Linux. The basic idea is to establish a Unix Domain socket between monitor and workers. The monitor sends opened socket "credentials" to the worker using a special flag to sendmsg(). The worker receives the socket and works with it as usual.

Since this technique is platform specific (a quick glance on Windows Socket API function list does not reveal any facilities for passing open sockets between processes), we can safely bet that Java does not support it in the Java API. Unix Domain Socket implementations in JNI exist; unfortunately, neither juds nor junixsocket support passing file descriptors. This actually makes a nice business opportunity, selling Java monitor-worker based server framework.

The same can be said about C/C++. Although I'm pretty sure someone has already rolled their own monitor-worker implementation that does what I described here, the effort involved is non-trivial, so I'm willing to bet that someone is going to be willing to pay for such framework.

## Monday, May 24, 2010

### Mac OS X thread local storage

Historically, Mac OS X uses the Mach-O executable format, which does not support thread local storage data section found on most operating systems using ELF executable format (here is how ELF handles thread local storage as documented by Ulrich Drepper). If you try to compile a program using __thread modifier, you'd get the following error.
$cat tls.c static int __thread x; int main() { return 0; }$ gcc tls.c
tls.c:1: error: thread-local storage not supported for this target

However, it has been suggested that pthread_getspecific() has a fast implementation on Mac OS X. The pthreads thread specific functions are POSIX API facility for thread local storage (TLS).

On Mac OS X, pthread_getspecific() for i386 takes exactly three instructions. It is similar to ELF handling of TLS on i386, in the sense that both use the GS segment to point to a thread control block. However, the difference is that ELF only stores the thread pointer in the thread control block, which is then used to calculate the address of the thread local storage data section for that thread. On Mac OS X, pthread specific values are allocated from the thread control block in the GS segment. This saves another indirect reference, so that could be faster.

The way Mac OS X implements pthread_getspecific() for ppc target is more complicated. First, it looks up from the COMMPAGE (defined in cpu_capabilities.h) the address to several possible pthread_getspecific() functions implemented by the mach kernel, and branch there. From there, thread control block is either obtained from SPRG3 (special purpose register #3), or by an ultra-fast trap. The SPRG3 points to thread control block where pthread specifics are allocated from, so the approach is comparable to using GS segment on i386. The ultra-fast trap approach, despite its name, is a slower fall-back approach that appeals to the task scheduler to find out which thread is currently running.

I don't know how other operating systems implement pthread_getspecific(), but if they already support thread local storage variables, they should be able to implement pthread specific like this to achieve the efficiency as expected on Mac OS X:
// Assume we only need a finite number of slots.
#define MAX_SLOTS 512

// For storing allocation status for a pthread-specific key.
static struct {
int created;
void (*destructor)(void *);
} key_descriptors[MAX_SLOTS];

// For locking the routine that has to manipulate key_descriptors,

This is inspired by pthread_tsd.c in Mac OS X Libc. Notice that, unlike their Libc, we can implement pthread_getspecific() and pthread_setspecific() using trivial inline functions or macros that access the specifics array.

Therefore, my recommendation is to use POSIX pthread specific in your program, which will work on any POSIX including Mac OS X. If your operating system has a slow POSIX pthread specific implementation, but the compiler has thread local storage support, then you can roll your fast pthread specific like shown above.

## Thursday, May 20, 2010

### Ambient lighting

Put these LED lighting strips in a clear water hose for ambient lighting project?

### Secrets (everyone should know) About Running Web Servers

Datacenters are expanding, and a lot of them host web servers. It is only imperative that web servers run fast and lean. An ineffective web server implementation can cost you money in many ways.
• You will need beefier hardware; they are more expensive, draw more power, and generate a lot of heat (requires high cooling cost).
• You will need more servers. In addition to all the above disadvantages, they take more room, which costs you more in rent.
Although network bandwidth charges can be expensive, they scale nicely with the amount of traffic you receive. There are still things you can do to halve the bandwidth use, like compressing HTTP content. Other things like combining packet output can shave a few bytes from packet headers, and it improves latency, but these are not the main focus for this article. I want to talk about is, what can make you run a web server that is unnecessarily costly, purely due to the software that runs on the server.

First of all, PHP is expensive to run (i.e. Mark Zuckerberg is an idiot). In the very beginning, there was a web server that can serve static files. Then there was a web server that can serve dynamic content by spawning child processes and delegating requests to them using the CGI protocol. You can write CGI in any language, including C, C++, Perl, and even shell script. This satisfies applications where Server Side Include does not suffice. But for certain applications, the ability to embed code directly inside HTML code makes the program look more elegant because it saves you all the print statements. This is where PHP came along. It was first invoked by Apache over CGI. Each time you make a request to a PHP page, the PHP interpreter has to parse and compile your PHP code from head to toe into bytecode before it can run.

As PHP gains more popularity because of its ease of rapid prototyping, the application size increases. It became impractical to do head to toe parsing each time someone requests a PHP page, which nowadays can easily involve tens of megabytes worth of code. MediaWiki is 44MB. Joomla is 33MB. Wordpress is 9MB. PhpWiki is 1.4MB. This is where mod_php comes along. It avoids having to recompile a massive amount of code by doing it only once, and caching the bytecode in memory. Putting all the bytecode in memory seems to be a small deal, but Apache distributes its workload over several child processes, each of them has an instance of mod_php that caches its own copy of the bytecode. If you increase the number of child processes, you run out of memory. If you limit memory usage, you cannot fully utilize CPU and I/O. This is explained better in Why mod_php is bad for performance. Unfortunately, memory is still the scarce resource. There is a limited amount of memory you can put into each server. You will have to run more servers; they take up more space and cost more money.

Facebook recently developed a version of PHP called HipHop that precompiles your code. This will alleviate the need for mod_php hack, which trades memory usage for performance, a poor tradeoff (maybe Mark Zuckerberg isn't as dumb as I thought).

Now, back to CGI. I did mention that you could write CGI in any language you want. However, some languages have a pretty bad run-time startup cost (Java is particularly notorious due to its Just In Time compilation). A solution is to keep those CGI child processes running, hence the FastCGI protocol is born. This means that your application runs more or less persistently along with the web server. Some languages like Java or Python simply has a web server written in that language, which requires no bridging. FastCGI is just a clumsy way to bridge together a particular server software with dynamic site logic written in a different language, due to administrative reason, e.g. at a web hosting provider. If you can run your own web server, don't bother with FastCGI. For the rest of the article, assume you write a persistent web server from scratch.

The fact that your web application runs persistently on the server has several implications. If you write your application in C or C++, and it has a memory leak, it will keep accumulating memory usage until it runs out of memory and dies. You can limit the effect of memory leak by restarting the server process after a predetermined number of requests, and keep several processes active so that some servers are available to handle requests at any given time.

If you use a garbage collected language like C# or Java, you don't have to worry about memory leak, but multi-threaded request handling is known to interfere with generational garbage collection by prematurely putting would-be garbage in the tenure heap, which causes costly full-collection to run more frequently. There are two solutions, either to increase nursery size, or separate the application into several processes, each running only a few threads. You can mix the two approaches.

This pretty much covers all I want to say. Here is a summary of secrets that I want everyone to know, about running web servers.
• Stay away from mod_php (or mod_perl, mod_python, mod_ruby).
• Stay away from web hosting providers that insist everyone should use mod_php (or other mod_*s) because they are encouraging their customers to be more expensive, and they are often more pricey. That applies to 99.9% of web hosting companies out there.
• If you run your own web server, don't bother with FastCGI. If you can help it, use a web server that can be embedded inside your web application, written in your application's native language.
• It still pays to write a web application in C/C++, especially where scalability is concerned.
Oh, and I forgot. If you can use client-side scripting, use it. Each individual user's computer might compare feebly to your server, but collectively their computing power is more than you can rent from a data center.

## Tuesday, May 18, 2010

### SSH from a jail

In FreeBSD, when you chroot into a jail, you cannot access /dev/tty anymore because the current terminal is not created inside the jail. You'd see something like this:
$cat /dev/tty cat: /dev/tty: Device busy  However, that also means you cannot ssh out of the jail because ssh opens the terminal device when asking for password. One way to work around this is to use public key authentication. However, there is a simpler alternative, using an SSH_ASKPASS hack. The manual page of ssh states that if both DISPLAY and SSH_ASKPASS are set, then the program specified by SSH_ASKPASS will run. The password is printed to standard output of the program. Instead of prompting for a password, we will write a script to echo the password out. $ cat > ssh-login.sh
#!/bin/bash
echo open-sesame
^D
$chmod +x ssh-login.sh$ export DISPLAY=:0.0

Obviously, SSH will still not be able to access the terminal, so any SSH functionality that involves a pseudo-terminal will not work. You will be able to access non-interactive commands, SFTP, and port forwarding this way.

## Wednesday, May 12, 2010

### Abbreviating output to a line in the terminal

A very long time ago, I quickly whipped together a Perl script that will reduce the standard output from another program to just a line, prefixed with an animation character, and optionally suffixed with ellipses if the line is too long. It is useful if only the most recent output is significant, and that I only want to know if the program is making progress. It can be applied to many commands, e.g.
• tar zxvf source-1.1.0.tar.gz | abbrev
• unzip archive.zip | abbrev
• make | abbrev
• tail -F /var/log/messages | abbrev

Compiling is simple if you're using GNU make:
make abbrev

You don't need to supply a Makefile. The implicit rule will work. You can optionally supply CFLAGS=-O3 to the make command, as well as optionally strip the executable after make. To install, just move the executable to somewhere in your PATH. It should compile out of the box on Linux and Mac OS X.

There are some subtle features that one might not notice:
• Output is throttled to a certain frame rate (default 30 fps).
• Resizing the terminal window will cause the line length to adjust accordingly.
Hope you enjoy it.

## Tuesday, May 11, 2010

### Cost estimate for large project

A programming project of mine is growing in the number of lines of code. Although I write documentation as inline comments, I'm at a point where it is nice to have a reference. I've always known that open source projects tend to use Doxygen, which automatically parses your code and extracts documentation from it to be used as a reference. But this is not what I'm going to talk about today, though I'm using Doxygen as an example.

As I read about Doxygen, I noticed that the author offers some form of commercial support: implementing features, fixing bugs, and providing help to answer questions. I think it would be more useful if he could offer the service to retrofit a Doxygen configuration file for an existing project. In doing this, he would learn about large project requirements that he may build into Doxygen in the future. Another reason is that some people might not want to divert their attention to figure out how to configure Doxygen for their project, and they're willing to pay for the service (this is another plausible open source business model).

Most projects have a particular convention, so that setting up Doxygen is only a one-time effort. The nice thing about a computer is that, once the initial setup is done, the cost of recreating the reference from source code is miniscule, and it is a cost that most project teams should be able to absorb. The expensive part is the setup, which requires human attention. This applies to any automation for a project, so I'm going to stop talking about Doxygen from now on.

Not all projects are created equal. It is simple to setup automation for a project that spans only a few thousand lines of code. A large project with millions lines of code has a lot of hidden complexity. One source of the complexity is due to the fact that collaborative effort often has bits of inconsistencies because everyone does things somewhat differently. Even the same person changes the way he/she does things over time. In big-O notation, the amount of inconsistency is O(n) where n is the number of lines of code. In order to resolve these inconsistencies, we need to compare each k inconsistencies to the remaining (k - 1) inconsistencies (so we can devise a workaround or a fix), and the amount of effort required would be O(n2). This is the cost of setting up automation for a project.

It's easy to see that it is the most cost effective to setup automation from when the project is small. In addition, for a large project, the cost estimate would be proportional to the square of the size of the project. A well-maintained project is tree-structured, and (without proof) a divide and conquer strategy could be used to resolve inconsistencies that could hinder automation. The cost would be closer to O(n logn).

I think this very simple asymptotic analysis explains why money spent on multi-million dollar projects tend to vanish. A project involving n workers cannot allow each one of them to collaborate with one another directly, which means the communication complexity alone is O(n2). A ideal, hierarchical, tree-structured organization still requires O(n logn) amount of communication complexity for the time spent on meetings and planning. The cost for the actual work done is still O(n), and the total cost estimate is O(n logn) + O(n) = O(n logn). The cost estimate for a large project never scales linearly, and most directors under-estimate the cost if they use a linear model.

## Friday, May 7, 2010

Suppose we have an allocator that employs a headerless object layout strategy, like Streamflow. The free(p) operation needs a way to tell if p is managed by any of the subheaps, and if so, which subheap manages p. In essence,
void free(void *p) {
SubHeap *h = GetSubHeap(p);
if (h)
h->Free(p);
}

We need a way to quickly lookup the subheap that manages a pointer. The mechanism used in Streamflow is to have a byte vector, each one corresponding to a page in memory denoting whether the subheap is present, and if so, which kind (I imagine subheaps would be in power of two number of pages, naturally aligned to its size). This mechanism is attributed to the Big Bag of Pages (BIBOP) due to Guy Steele, Jr.

On a 32-bit machine with 4KB page size, the byte vector would be 1MB. On a 64-bit machine, it will be a whopping 4096TB unless we use much bigger page size, or that the table would have to be sparse, such as using a multi-level hash table, or using some kind of a binary search tree. Both impose challenge in scalability in a multi-threaded setting because concurrent lookup and update are non-trivial.

Inspired by how Translate and Lookaside Buffer (TLB) works, I postulates that most of the lookup could be localized to the thread's own lookaside table. It is a linearly probed hash table, keyed by the lookup address, with a small probe distance (say, 4). The idea is that if we can't find the entry in four iterations of a search, then we might as well appeal to the global data structure. Hash table insertion may overwrite existing data. The policy to balance probe distance, eviction policy, and performance would be subject to further research.

So we will have GetSubHeap(p) implemented like this:
SubHeap *GetSubHeap(void *p) {
SubHeap *h;
if (h = LocalSubHeapLookup(p))
return h;
if (h = GlobalSubHeapLookup(p)) {
LocalSubHeapInsert(p, h);
return h;
}
return NULL;
}

The LocalSubHeapLookup() and LocalSubHeapInsert() operate on thread local data structure, so they do not need any synchronization. GlobalSubHeapLookup(), on the other hand, must acquire a read lock.

What might be of special interest is GlobalSubHeapRemove(). What if a cached entry of subheap is being removed? Ideally, the removal would only happen if the subheap is empty, which means the program should no longer have references to any objects in the subheap. The entry in the lookaside table would be left unused until it is evicted. However, many programs have double-free problems. This would cause free(p') to find a subheap from p' that is already removed, and call SubHeap::Free(p'). Therefore, the precise problem depends on what the function does. Another problem is if the subheap is removed, but another subheap is constructed on that address. The double-free problem would corrupt the subheap, again, depending on how resilient SubHeap::Free(p') handles corrupted pointers (the subheap might have a different object layout this time). Even if we keep the thread-local lookaside table up to date with the global mapping, there may simply be no good way to handle double-free gracefully.

format: plain


text:
# Latex police size

To change the size of a font use a new font size parameter. The different font sizes are listed below. Aller à Arbitrary font size – Changing the font size is made by changing the font file.

LATEX permet de changer, dans un document, la taille des caractères, soit de façon globale, soit. Knuth (donc LATEX) utilise le point pt qui vaut. The font changing commands are often determined by the document class itself. For the standard classes (with size options in the first row), the .

I have a java lstlisting and it is too big and does not mach a single page. Is there any way to specify font for specific listing, not all in . What I want to know is how to change the font size of Figure or Figure say the figure headers or something. Per default latex use a font size of 10pt (depending of the used documentclass article, report, book und letter ). This could be changed to 11pt or 12pt as a option . I want to use a font size other than the standard LaTeX list of tiny, huge, etc. Related Question: Change font size inside the document.

The standard LaTeX document classes only allow you to choose 10pt, . I’ve got to say, this is one of the arcane areas of latex that really bugs me.

I am writing my thesis and my university requirements ask me to set the footnotes font size at 9pt. I’d like to change text size for some page part, e. That’s also a possibility to choose an intermediate size, for instance if \tiny gets. This topic was discussed in the LaTeX Community Forum, on . To adjust the font size for the whole document, you can set a global option in the.

The second problem is that for whatever reason TMW have disabled the traditional LaTeX font size commands (e.g., \large) in MATLAB forcing . Vector drawing program for LaTeX using PSTricks.


text:
# [Tex/LaTex] Change aspect of headrule and footrule on chapter pages

I want to customize the aspect of headrule and footrule in my document, using report class and titlesec.
Unfortunatly, my redefinition of \makeheadrule and \makefootrule is not taken into account for chapter pages.
Please find below a basic example with code and picture, the rule lines should be blue on both pages.

  \documentclass{report}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage[pagestyles]{titlesec}

\newpagestyle{normalStyle}
\setfoot{}{Footer}{}\footrule}
\pagestyle{normalStyle}

\newpagestyle{chapterStyle}
\setfoot{}{Footer}{}\footrule}
\pagestyle{chapterStyle}

\assignpagestyle{\chapter}{chapterStyle}

\renewcommand\makefootrule{\color{cyan}\rule[\baselineskip]{\linewidth}{0.4pt}}

\begin{document}

\chapter{Some Chapter Title}

\lipsum

\end{document}

The problem is that \headrule calls \setheadrule, which then redefines \makeheadrule. This is done when the page style is set by \pagestyle or \thispagestyle (used for chapter pages). The same applies to \footrule.

Moving the redefinitions of \makeheadrule and \makefootrule into the definitions for the page styles, after \headrule and \footrule, solves this problem.

\documentclass{report}
\usepackage{xcolor}
\usepackage{lipsum}
\usepackage[pagestyles]{titlesec}

\newpagestyle{normalStyle}{%
\setfoot{}{Footer}{}\footrule
\renewcommand\makefootrule{\color{cyan}\rule[\baselineskip]{\linewidth}{0.4pt}}
}

\newpagestyle{chapterStyle}{%
\setfoot{}{Footer}{}\footrule
\renewcommand\makefootrule{\color{cyan}\rule[\baselineskip]{\linewidth}{0.4pt}}
}

\assignpagestyle{\chapter}{chapterStyle}

\pagestyle{normalStyle}

\begin{document}

\chapter{Some Chapter Title}

\lipsum

\end{document}


text:
## Wednesday, October 27, 2010

### Link o' the day: Meditations on Bluish Coder and inadequacy

Every once in a while, I run across someone who makes me feel inadequate. (As opposed to the people I work with every day, who make me feel completely adequate.)

Recently,  I have been interested in programming languages with dependent data types, mostly because they seem to be the natural end point of type system evolution. (Perhaps "end point" is the wrong term; "next step" may be better.) Unfortunately, most of the dependently typed languages I have seen are unappealing for various reasons.
• Epigram seems neat, but the last time I looked at it I seem to recall it only worked within its own IDE. Not a terrible failing, but still.... And version 2 is not out yet.
• Agda also seems neat, but for some reason I am having a hard time getting enthusiastic about it.
• Coq is neat (there may be a trend here), but as a programming language? Certainly, it is the best computer game I have played in ages, but one issue I have is that I find Coq proofs almost unreadable. It does get lots of extra points for Coq'Art, a truly excellent book.
• Many of the others that I have heard about are not actively developed.
However, there is one alternative with a significant feature: ATS has excellent placement in the computer language shootout. (Hey, that's how I found OCaml, which quickly became my last favorite programming language.) Plus, the current implementation is named "Anairiats", which is both cool and enigmatic.

To come back around to the point of this post, I was casting around for more information about ATS, and found Bluish Coder, the blog of Chris Double, who, judging by the tags and posts, seems to have already done most of the things on my to-do list. Scheme in Javascript, ATS, continuations, factor, etc., etc. Worst of all, he's in New Zealand.

[Edit: Yah, and I can't even remember to tag my posts.]

## Saturday, October 23, 2010

### SAML2 Servlet Filter

I have been working on various forms of authentication for Java-based web applications for quite a while. The latest iteration uses the Security Assertion Markup Language, version 2 to interact with an Identity Provider (IdP), which is supported by the authentication system we get to use. When describing the OSGi-based design I came up with to support this thingamajig I again mentioned the code to handle the interaction; the first part of that code is the topic of this post. I intend to describe the servlet filter which stands in front of application servlets; a later post will describe the Assertion Consumer Service.

Because of the way the code is structured, this post must assume that you are familiar with the basic protocol as described in SAML authentication for web applications.

## Background

There are two parts to the application-side (or Service Provider, SP side) of the authentication infrastructure: a servlet filter and the Assertion Consumer Service (or ACS), an application that interprets the SAML response and directs the user's request appropriately. The servlet filter intercepts incoming user requests and either
• passes the request as-is, for those that do not need authentication,
• handles redirected requests from the ACS after the authentication,
• handles requests that are already authenticated,
• does something useful in the case when SAML authentication must be disabled, or
• generates an authentication request.
The first and fourth options are, well, optional. The SAML2 filter only needs the second, third, and fifth branches to function correctly.

I do not intend to provide complete code for the filter, because the implementation I have is tightly bound to the OSGi container and to our framework and the idea of refactoring it for genericity leaves a bad taste in my mouth. The basic structure of the filter, though, is:
try
{
final HttpServletResponse response = (HttpServletResponse) resp;
final HttpServletRequest request =
filteredRequest(new HideClientSsoHeaderHttpRequest((HttpServletRequest) rqst), response, chain, getConfig(config));
if (request == null) { return; }
else if (handleAcsRedirect(request, response, chain)) { return; }
else if (handleAuthenticatedRequest(request, response, chain)) { return; }
else if (handleSamlDisabled(request, response, chain, getConfig(config))) { return; }
else
{
// Unknown key: send Authentication Request
response.sendRedirect(authnRequest(request));
return;
}
}
catch (...) { ... }

The possible exceptions are SecuritySystemException, a high-level exception thrown by the authentication and identity-related parts of our framework, usually indicating an operational failure of some sort, and DecoderException, from Apache Commons Codec and indicates a Base64 or other encoding error.

## Pass through

Passing through requests unfiltered is relatively simple, although there are several related operations and as a result the function is larger than it would seem to be. The filteredRequest function returns either a null, indicating that the request has been handled, or a request which may be the original or may be a modified version. Pass through processing must be done first, since the whole goal is to short-circuit other more expensive processing.
private HttpServletRequest filteredRequest(HttpServletRequest request, HttpServletResponse response, FilterChain chain, Config config) throws IOException, ServletException
{

The basic filter compares the URL from the request with a regular expression pattern; if the URL matches the request is passed on to the next element of the filter chain to be handled and null is returned.
if (config.excludedUrlPattern != null)
{
final String contextPath = request.getContextPath();
final String requestUri = request.getRequestURI();
final int contextBeg = requestUri.indexOf(contextPath);
final int contextEnd = contextBeg + contextPath.length();

String applicationUrl =
(contextBeg < 0 || contextEnd == (requestUri.length() - 1))
? requestUri : requestUri.substring(contextEnd);
if (!applicationUrl.startsWith("/"))
{
applicationUrl = "/" + applicationUrl;
}

if (config.excludedUrlPattern.matcher(applicationUrl).matches())
{
/* Request matches excluded URL pattern; do not continue with filter. */
chain.doFilter(request, response);
return null;
}
}

The second option we currently have is for AMF requests (a.k.a. BlazeDS, as in Adobe Flex), it matches against the destination of the request (which requires grubbing through the request, unfortunately). Corey describes what this does as:
When a Flex client makes a remote object call this ends making two seperate server calls. The first is an administration call that doesn't have a normal AMF payload (that can be decoded). The second is the call (with payload) to the service destination. Because I can't tell what service destination this is targeted to I'm always letting these administration request go through unfiltered. I need to add a check here when this happens to make sure they are targeting the Flex broker with the URL.
if (config.excludedAmfDestinationPattern != null
{
final CapturedBytesHttpRequest captureBytesRequest = new CapturedBytesHttpRequest(request);
final AmfDecoder decoder = new AmfDecoder(captureBytesRequest.getCapturedBytes());
if (!decoder.decode() || config.excludedAmfDestinationPattern.matcher(decoder.getDestination()).matches())
{
/* Request matches AMF destination pattern; do not continue with filter. */
chain.doFilter(captureBytesRequest, response);
return null;
}
else
{
return captureBytesRequest;
}
}

return request;
}

## ACS redirects

When a request is directed to the ACS which contains a successful authentication, it redirects the browser back to the original URL, meaning that the request is captured by this filter. However, the redirected request (which must be a GET) contains a query parameter matching the regular expression pattern "^ACSREQUESTID=(.*)$". (In-band signaling is kind of unpleasant, but there are few other ways to correctly get the requisite information shared by the ACS, this filter, and the browser.) When a request contains the ACSREQUESTID, the value is picked out and decoded. This value is a token that is used as a key for the authentication information as well as to recover the original request, which is handled by this function. private boolean handleAcsRedirect(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws DecoderException, IOException, ServletException { final String queryString = request.getQueryString(); if (queryString != null) { final Matcher matcher = pat.matcher(queryString); if (matcher.matches()) { final String key = new URLCodec().decode(matcher.group(1)); final HttpServletRequestWrapper storedRequest = Storage.getRequestStore().getRequest(key); final IdentityMap mapping = Storage.getIdentityStore().getIdentity(Arrays.asList(key)); if (storedRequest != null && mapping != null) { storedRequest.setRequest(request); chain.doFilter(new AuthenticatedHttpServletRequest(storedRequest, mapping), response); Storage.getRequestStore().removeRequest(key); return true; } } } return false; }  The Storage class provides access to two different storage systems: • The IdentityStore records information returned from the IdP about a user; the information is returned as an IdentityMap, which is used to construct an AuthenticatedHttpServletRequest. public interface IdentityStore { public abstract void storeIdentity(String key, String identity, Map<String, List<String>> attributes, Date expiry); public abstract IdentityMap getIdentity(List keys); public abstract IdentityMap getIdentity(Identity identity); public interface IdentityMap { public abstract String getEmployeeNumber(); public abstract Map<String,List<String>> getAttributes(); public abstract Date getExpiration(); public abstract boolean valid(); } }  • The RequestStore records the original request from the user (which will be stored below) public interface RequestStore { public abstract void storeRequest(String key, HttpServletRequest request); public abstract String getRequestUrl(String key); public abstract HttpServletRequestWrapper getRequest(String key); public abstract void removeRequest(String key); }  The Storage class in turn comes in two forms: • An in-memory store, used for original testing and possibly in case of database failures, and • A database-backed store, used to share requests between load-balanced servers as well as preserving requests in case of server restarts. An AuthenticatedHttpServletRequest recovers a stored request and adds the identity information from the IdentityMap to allow access from the application. ## Authenticated requests Authenticated requests are simple to handle. The token used as a key store the identity information is also added as a cookie to the response by the ACS, so all this method needs to do is to recover the cookie and check it against the IdentityStore (which includes checking if the authentication has timed out). private boolean handleAuthenticatedRequest(final HttpServletRequest request, final HttpServletResponse response, final FilterChain chain) throws DecoderException, IOException, ServletException { final List authCookie = Misc.getCookie(request, AUTH_COOKIE_NAME); final IdentityMap mapping = Storage.getIdentityStore().getIdentity(authCookie); if (mapping != null && mapping.valid()) { chain.doFilter(new AuthenticatedHttpServletRequest(request, mapping), response); return true; } return false; }  ## Disabling SAML The downside of the SAML2 single-sign-on design is that if the IdP is down, the applications are also unavailable. As an alternative, I added a administratively configurable flag to disable SAML2 processing. If SAML2 is disabled, there are three other options: • Some applications are already able to handle their own authentication in place of SAML2. For these applications, if SAML2 is disabled all requests are passed on directly to the application. • Some applications may need a generic username and password based authentication scheme; in this case a application-specified page is displayed with a form request a login. Once the user is logged in, normal identity information is available to the application, so the authenticated requests branch above can be taken. • Any remaining applications should simply display an "unavailable" page. I will not show any code for this method, since it is relatively straightforward and the processing for option 2 is lengthy. ## Generating authentication requests The remaining possibility requires generating an authentication request, storing the original request, and redirecting the browser via the request back to the IdP. private String authnRequest(HttpServletRequest request) throws SecuritySystemException { // Generate the session identification key final String key = Framework.getSessionKeyService().generateSessionKey(); Storage.getRequestStore().storeRequest(key, request); return new AuthenticationRequest(key).toUriString(); }  The session key generated is used as the token for the SAML2 request as well as the value of the cookie identifying future authenticated requests. The key itself is just a cryptographically secure random number. The String generated is the URL of the IdP with the authentication request added as a query parameter, following the SAML2 specifications. This is passed back to the browser as a redirect. ## An AuthenticationRequest To interact with the IdP, I used the fmclientsdk.jar and openssoclientsdk.jar libraries from OpenAM, which was OpenSSO from Sun before the Oracle acquisition. I currently need to use both, because some classes are missing from each. (I really should report that as a bug.) public AuthenticationRequest(String key) throws SecuritySystemException { try { final String destinationUrl = ...; final boolean forceAuthn = ...; final boolean isPassive = ...; final boolean signAuthnRqst = ...; final String acsUrl = ...; final AuthnRequest authnRequest = ProtocolFactory.getInstance().createAuthnRequest(); authnRequest.setID(key); authnRequest.setVersion(SAML_VERSION); authnRequest.setIssueInstant(new Date()); authnRequest.setDestination(destinationUrl); if (forceAuthn) { authnRequest.setForceAuthn(forceAuthn); } if (isPassive) { authnRequest.setIsPassive(isPassive); } authnRequest.setProtocolBinding(PROTOCOL_BINDING); authnRequest.setAssertionConsumerServiceURL(acsUrl); authnRequest.setIssuer(issuer()); authnRequest.setNameIDPolicy(nameIdPolicy()); authnRequest.setRequestedAuthnContext(requestAuthnContext()); if (signAuthnRqst) { sign(authnRequest); } authnRequestMessage = authnRequest.toXMLString(true, true); log.debug("request: " + authnRequestMessage); } catch (SAML2Exception e) { throw new SecuritySystemException("Unable to create SAML2 AuthnRequest", e); } catch (...) { ... } }  The com.sun.identity.saml2.protocol.ProtocolFactory class provides access to the components needed to generate the authentication request. The created object is a com.sun.identity.saml2.protocol.AuthnRequest, which has a large number of properties which can be set to fill in elements in the request. For precise details of which properties are needed, see the SAML2 specifications as well as something like the Federal ICAM (Identity, Credential, and Access Management) Security Assertion Markup Language (SAML) 2.0 Web Browser Single Sign-on (SSO) Profile. (Man, that thing has an unfortunate logo.) This method calls three additional methods to create other parts of the request: • The issuer identifies the server making the authentication request. private Issuer issuer() throws SAML2Exception { final String issuerName = ...; final Issuer issuer = AssertionFactory.getInstance().createIssuer(); issuer.setValue(issuerName); return issuer; }  • The name id policy describes the requested properties of the "name" used to identify the user in the authentication response. In our case, I am actually using an employee number, which is returned as one of the attributes of the user, rather than the SAML2 name. private NameIDPolicy nameIdPolicy() throws SAML2Exception { final String issuerName = ...; final boolean allowCreate = ...; final NameIDPolicy nameIdPolicy = ProtocolFactory.getInstance().createNameIDPolicy(); nameIdPolicy.setFormat(NAMEID_POLICY_FORMAT); nameIdPolicy.setSPNameQualifier(issuerName); nameIdPolicy.setAllowCreate(allowCreate); return nameIdPolicy; }  • Finally, the authentication context describes the requested properties of the authentication itself. For example, it is possible to request various levels of confidence in the authentication, from simple user assertions to multi-factor cryptographic confidence. private RequestedAuthnContext requestAuthnContext() throws SAML2Exception { final RequestedAuthnContext requestedAuthnContext = ProtocolFactory.getInstance().createRequestedAuthnContext(); requestedAuthnContext.setComparison(AUTHN_CONTEXT_COMPARISON); requestedAuthnContext.setAuthnContextClassRef(AUTHN_CONTEXT_CLASSREF_VALUES); return requestedAuthnContext; }  The one tricky feature of the authentication request is the cryptographic signature. The sign method above (invoked when the signAuthnRequest flag is set) signs the authnRequest "internally", using XML encryption. In the HTTP-Redirect binding, requests are signed after being converted to strings, which will be described shortly. Typically, signAuthnRqst should be false. Instead, the authentication request, formatted in XML, is first compressed (using DeflaterOutputStream). public String toEncodedString() throws SecuritySystemException { try { final ByteArrayOutputStream baos = new ByteArrayOutputStream(authnRequestMessage.length()); final DeflaterOutputStream dos = new DeflaterOutputStream(baos, new Deflater(Deflater.BEST_COMPRESSION, true)); dos.write(authnRequestMessage.getBytes()); dos.close(); return encodedString = encode(baos.toByteArray()); } catch (IOException e) { throw new SecuritySystemException("Unable to encode SAML2 AuthnRequest", e); } }  Following compression, the string is Base-64 encoded, then URL-encoded to protect non-URL-safe Base-64 characters. private String encode(byte[] byteArray) { return new String( new URLCodec().encode( Base64.encodeBase64(byteArray) ) ); }  Finally, the query string is partially assembled, with the SAML request and SigAlg (indicating the signature algorithm) parameters. The resulting string is signed and the signature added as a third query parameter. The whole is assembled with the URL to be returned to the browser. public String toUriString() throws SecuritySystemException { try { final String parameters = String.format("%s=%s&%s=%s", SAML_REQUEST, toEncodedString(), "SigAlg", new URLCodec().encode("http://www.w3.org/2000/09/xmldsig#rsa-sha1")); final String signature = signature(parameters); return String.format("%s?%s&%s=%s", destinationUrl, parameters, "Signature", signature); } catch (EncoderException e) { throw new SecuritySystemException("Cannot enecode SigAlg", e); } }  To sign the string, the normal Java cryptography API's are employed. The result is encoded identically to the compressed request. private String signature(String unsigned) throws SecuritySystemException { String signatureAlgorithm = ...; try { final Pair pair = ...; final Signature signature = Signature.getInstance(signatureAlgorithm); signature.initSign(pair.getY()); signature.update(unsigned.getBytes("UTF-8")); return encode( signature.sign() ); } catch (...) { ... } ... }  ## Conclusion The filter is relatively complex, although it follows a simple step-by-step procedure. However, most of the complexity comes from generating the authentication request, which requires interaction with the OpenAM client SDK, Java cryptography and compression, and HTTP URL manipulation. Best of luck, and I hope this helps someone. ## Sunday, October 17, 2010 ### Link o' the Day: Top 10 Performance Problems From the Canned Platypus link o' the day, I found a very nice list of Top 10 Performance Problems taken from Zappos, Monster, Thomson and Co. I am not sure I would call them the "Top 10", but they are certainly significant causes of web application performance issues. (Also, I would switch #10 and the #11 bonus; "Intermittent Problems" is not really a class of problems.) ## Tuesday, October 12, 2010 ### Quote o' the Week: It’s [Not] Faster Because It’s C From Canned Platypus, I’d even argue that the main reason kernel code tends to be efficient is not because it’s written in C but because it’s written with parallelism and reentrancy in mind, by people who understand those issues. A lot of code is faster not because it’s written in C but for the same reasons that it’s written in C. It’s common cause, not cause and effect. The most common cause of all is that C code tends to be written by people who have actually lived outside the Java reality-distortion bubble and been forced to learn how to write efficient code (which they could then do in Java but no longer care to). Smack! There is some good advice there: Focus on stability and features first, scalability and manageability second, per-unit performance last of all, because if you don’t take care of the first two nobody will care about the third. [...] Compare results, not approaches. On the other hand, per-unit performance is a good tool to achieve stability, scalability, and manageability, so don't get carried away. ## Monday, October 11, 2010 ### (How to write a (Lisp) interpreter (in C++) (or make Peter Norvig cry, whichever comes first)) [11/30/2010] Anthony C. Hay's Lisp interpreter in [more than] 90 lines of C++ is very sweet, and much more functional than mine. Check it out! Last week, I was pointed (by reddit) to Peter Norvig's (How to Write a (Lisp) Interpreter (in Python)) and the follow-on, (An ((Even Better) Lisp) Interpreter (in Python)), and for no readily discernible reason was compelled to attempt a translation. Naturally, I chose C++, a language I have less than a year's experience with, which period was more than a couple of years ago, and which I do not especially like. So, in a spirit similar to that needed to properly enjoy tucking a live weasel into my trousers, I set to work. What follows is the important part of the result, the eval function. Value::p and Environment::p are both pointers, to a Value and an Environment respectively. (The necessity of using the weird ::p idiom will be explained shortly, although you might consider the random dynamic_pointer_cast's to be a clue.) Value::p eval(Value::p x, Environment::p env) {  The first branch handles symbols, the value of which is found in the environment. if (x->type() == Value::SYMBOL) { // Look up the value Symbol::p x1 = dynamic_pointer_cast(x); return env->find(x1)[x1];  The second branch handles the other scalar values, which in this interpreter are integers and strings. These values are self-evaluating. } else if (x->type() != Value::LIST) { // Everything else scalar is self-evaluating return x;  The remaining values are all lists of various flavors. To make it easier to manipulate the values, I broke out repr, which is the STL list representing the list, symbol, which is the symbol at the head of the list, it, which is a STL iterator pointing to the second element of the list, and end, which is a STL iterator pointing to the end of the list. } else { List::p l = dynamic_pointer_cast<List>(x); // This is where it gets exciting list<Value::p> repr = l->to_list(); if (repr.size() > 0 && repr.front()->type() == Value::SYMBOL) { Symbol::p symbol = dynamic_pointer_cast<Symbol>(repr.front()); list<Value::p>::iterator it = ++(repr.begin()); // The second element, needed by most branches list<Value::p>::iterator end = repr.end(); // The end of the list  The first of the special forms is quote, which returns the remainder of the list. if ((symbol->to_string()) == "quote") { // quote: Return the rest of the list return List::create(++it, end);  The second special form is if, which evaluates the next element and tests its boolean value; if true the result is the evaluation of third element, if false, the fourth element. The element chosen is handled by the test and possible extra increment. } else if ((symbol->to_string()) == "if") { // if: Yeah, like this is going to work if (!(*eval(*it++, env))) { ++it; } return eval(*it, env);  The third special form is set!, which updates a new binding in the environment. Environment handling is one thing I stole directly from Norvig; find() returns a map, and the ...[var] = val assignment is a normal operator on that map. Very nice. } else if ((symbol->to_string()) == "set!") { // set!: Life does not get worse if I remove this Symbol::p var = dynamic_pointer_cast<Symbol>( eval(*it++, env) ); Value::p val = eval(*it++, env); env->find(var)[var] = val; return val;  The fourth special form is define, which adds a new binding to the environment. The second element of the list must be a symbol, which will not be evaluated, and the third will be the new value, which will be evaluated. } else if ((symbol->to_string()) == "define") { // define: Hey, Mickey! You're so defined... Symbol::p var = dynamic_pointer_cast<Symbol>(*it++); // Yeah, that's not even funny to me Value::p val = eval(*it++, env); env->update(var, val); return val;  The fifth special form is lambda, which is used to create a new function. The second element of the list is a list of argument identifiers, while the third is the body of the function. You will take note that I called out the variables pars and body; if you put both values in the call to Lambda::create, you have to look up the definition of sequence points. } else if ((symbol->to_string()) == "lambda") { // lambda: first element is the parameters, next is the body List::p pars = dynamic_pointer_cast<List>(*it++); List::p body = dynamic_pointer_cast<List>(*it); return Lambda::create(pars, body);  The sixth and final special form is begin, which simply evaluates the remaining values of the list and returns the final value. } else if ((symbol->to_string()) == "begin") { // begin: evaluate 'em all and let McCarthy sort 'em out Value::p value; for (; it != end; ++it) { value = eval(*it, env); } return value;  And the final branch is function application. The first step is to evaluate all of the values in the list. The first element should be a Lambda value, which is called with the arguments and the environment. } else { // application: this is where it gets really exciting list::iterator it = repr.begin(); // The beginning of the list list evaluated; for (; it != end; ++it) { evaluated.push_back( eval(*it, env) ); } { Lambda::p lambda = dynamic_pointer_cast(evaluated.front()); List::p args = List::create(++(evaluated.begin()), evaluated.end()); return (*lambda)(args, env); } } } } }  For those of you who are long-term Lisp users, the collection of closing braces should make you feel all warm inside, right? Unlike Norvig's version, which effectively compiles to Python by using Python's lambda, in C++ I had to implement lambda and function application myself. The Lambda class itself maintains a list of parameter names and the body expression. Function application is handled by operator(), in other words by overloading the function call for Lambda values. The operator() method recursively calls eval on the body expression, after creating a new environment mapping the parameter names to argument values. class Lambda : public Value { public: typedef shared_ptr<Lambda> p; static p create(List::p params, List::p exp) { return p(new Lambda(params,exp)); } Value::t type() { return LAMBDA; } operator bool() { return true; } string to_string() { return "LAMBDA"; } virtual Value::p operator()(List::p args, Environment::p outer) { return eval(_e, create_env(outer, _p->to_list(), args->to_list())); } private: List::p _p; List::p _e; protected: Lambda(List::p p, List::p e) : _p(p), _e(e) { } Environment::p create_env(Environment::p outer, list<Value::p> ¶ms, list<Value::p> &args) { list<Value::p>::iterator p_begin = params.begin(); list<Value::p>::iterator p_end = params.end(); list<Value::p>::iterator a_begin = args.begin(); list<Value::p>::iterator a_end = args.end(); Environment::p env = Environment::create(outer); for (; p_begin != p_end && a_begin != a_end; p_begin++, a_begin++) { Symbol::p symb = dynamic_pointer_cast<Symbol>(*p_begin); env->update(symb, *a_begin); } return env; } };  One (among many) downsides of implementing lambdas manually is that it also requires implementing all of the builtin functions. struct Builtin : public Lambda { virtual Value::p operator()(List::p args, Environment::p outer) = 0; Builtin(List::p p) : Lambda(p,List::p()) { } };  As an example of a builtin function, Plus implements integer addition. Verbose, but tedious. struct Plus : public Builtin { Plus(List::p p) : Builtin(p) { } Value::p operator()(List::p args, Environment::p outer) { list<Value::p>::iterator end = args->to_list().end(); long sum = 0; for (list<Value::p>::iterator i = args->to_list().begin(); i != end; ++i) { sum += dynamic_pointer_cast<Int>(*i)->to_int(); } return Int::p(new Int(sum)); } };  Amazingly, the Environment is not completely hideous. It is fundamentally a loose wrapper around the C++ STL map, using Norvig's tactic of returning the map from find(). class Environment { public: typedef shared_ptr<Environment> p; typedef map<Symbol::p,Value::p,Symbol::less> env_t; static p create() { return p(new Environment()); } static p create(p outer) { return p(new Environment(outer)); } virtual void update(Symbol::p s, Value::p v) { _e.insert( make_pair(s,v) ); } virtual env_t& find(Symbol::p s) { return _e.count(s) > 0 ? _e : _outer->find(s); } private: env_t _e; p _outer; Environment() { } Environment(p outer) : _outer(outer) { } };  Lisp parsing is actually relatively simple, although C++'s string handling is somewhat lacking. To handle it similarly to Norvig, I had to implement replace to provide some convenience. void replace(string &s, string from, string to) { int idx = 0; int next; while ((next = s.find(from, idx)) != string::npos) { s.replace(next, from.size(), to); idx = next + to.size(); } }  Fortunately, C++ lets me faff about with string I/O to break the input string into a list of string tokens. list tokenize(string s) { replace(s, "(", " ( "); replace(s, ")", " ) "); istringstream iss(s); list result; result.insert(result.end(), istream_iterator<string>(iss), istream_iterator<string>()); return result; }  Not to mention using string I/O to parse integers. Value::p atom(string token) { istringstream oss(token); long l; if (oss >> l) { return Int::p(new Int(l)); } else { return Symbol::p(new Symbol(token)); } }  Fortunately, I did not bother to do much error handling anywhere in the code, so the missing bits of read_from are (ahem) unexceptional. Value::p read_from(list<string> &tokens) { if (tokens.size() <= 0) { /* error */ } string token = tokens.front(); tokens.pop_front(); if (token == "(") { list<Value::p> lst; while (tokens.front() != ")") { lst.push_back( read_from(tokens) ); } tokens.pop_front(); return List::create(lst); } else if (token == ")") { /* error */ } else { return atom(token); } }  Anyway, to get back to a discussion I delayed at the top of this post, I avoided memory management almost entirely by using TR1 shared_ptr<T> templates, which almost trivially handle reference counting. To avoid some template syntax nastiness, I adopted an idiom of providing a typedef of "p" in a class C for the type shared_ptr<C>. Now, I really like type-safe collections, and I am duly impressed (and terrified) by some of the bizarre abuses that C++ templates are put to. But shared_ptr's are pretty darn sweet. The only downside is reference counting, which has problems with circular structures. Fortunately, I tend not to create circular structures and could probably deal with them in this Lisp by removing set!. Amazingly, the interpreter does in fact seem to work; the main function below (sorry, no repl as I used up my I/O quota above)... int main(int argc, char *argv[]) { Lispic::Environment::p global = Lispic::global_env(); std::list tokens = Lispic::tokenize( "(begin" " (print (+ 2 3))" " (define double (lambda (x) (+ x x)))" " (print (+ 3 3) (double 4))" ")" ); std::cout << Lispic::eval(Lispic::read_from(tokens), global)->to_string() << std::endl; return 0; }  ...produces the results: $ ./lispic
5
6
8
8

All this in about 300 lines of code. For greater amusement, you can get the source code for this monstrosity.

If you are still reading, thank you very much for your attention. If you were looking for more about the CAP theorem, bear with me. I think I have some ideas on how to actually formalize it using either Unity (I took a class from Jay Misra about 15 years ago) or Leslie Lamport's TLA+ (I have a Microsoft Channel 9 video). I suspect that it is going to involve temporal logic. Duck and cover!

 I just remembered Paul Wilson's incomplete An Introduction to Scheme and its Implementation, which is fortunately still available. At least, I think it was written by Paul Wilson, of the garbage collection reading collection.


text:
## Stream: maths

### Topic: totally_disconnected uses subsingleton, not set.subsingleton

#### Bryan Gin-ge Chen (Feb 14 2021 at 01:59):

I'm hacking on #6188 and I noticed that src#is_totally_disconnected uses docs#subsingleton (on types) rather than docs#set.subsingleton:

def is_totally_disconnected (s : set α) : Prop :=
∀ t, t ⊆ s → is_preconnected t → subsingleton t -- why not t.subsingleton here?

This seems to lead to a bunch of uses of docs#set.subsingleton_coe throughout. Would replacing subsingleton with set.subsingleton be an improvement?

#### Bryan Gin-ge Chen (Feb 14 2021 at 02:05):

Oh, it's not so clear... docs#set.subsingleton_of_image uses subsingleton on types too.

#### Yury G. Kudryashov (Feb 14 2021 at 07:34):

set.subsingleton is a relatively new addition to mathlib, so I'm not surprised that some coded uses _root_.subsingleton instead. I think that rewriting this code in terms of set.subsingleton is a good idea.

Last updated: May 18 2021 at 07:19 UTC


text:
# Comma

Sometimes it's used to write shorter code, so we need to know it in order to understand what's going on.

*!*
let a = (1 + 2, 3 + 4);
*/!*

alert( a ); // 7 (the result of 3 + 4)

## Comma — Structure map

Clickable & Draggable!


text:
Subject: Physics, asked on 30/4/18

## Plz solve que 15...... Q.15. A piece of iron weighs 44.5 gf in air. If the density of iron is , find the weight of iron piece when immersed in water.

Subject: Physics, asked on 29/4/18

## Solve question 3. Q.3. You are provided with a hollow iron ball A of volume 15 $c{m}^{3}$ and mass 12 g and a solid iron ball B of mass 12 g. Both are placed on the surface of water contained in a large tub. (a) Find upthrust on each ball. (b) Which ball will sink ? Give reason for your answer. (Density of iron = 8.0 g $c{m}^{-3}$)

Subject: Physics, asked on 3/3/18

## What is the difference between pressure and thrust as applied in fluids.

Subject: Physics, asked on 3/3/18

## What is the apparent weight of the floation? And the answer given below why is it incorrect ."DO NOT SEND SIMILAR QUERIES"? Ans b(ii) Apparent weight of the flotation means the density of the body submerged in the fluid is equal to the upthrust. That is body will float in the submerged fluid

Subject: Physics, asked on 3/3/18

## Will a body weigh more in air or in vacuum when weighed with a spring balance? Explain your answer.

Subject: Physics, asked on 3/3/18

## If the upthrust is considered as action what is the reaction?This reaction is exerted on what and by what?

Subject: Physics, asked on 3/3/18

## SI unit of upthrust

Subject: Physics, asked on 3/3/18

## Knives are ground to thin edge. Why? (3 marks). Answer given below has given 1 mark out of three.Justify the missing things in this answer.

Subject: Physics, asked on 3/3/18

## Q)What is the apparent weight of the floation? And the answer given below why is it incorrect .I have said many times that "DO NOT SEND SIMILAR QUERIES" Ans : Apparant weight of the flotation means the density of the body submerged in the fluid is equal to the upthrust.    That is body will float in the submerged fluid.

Subject: Physics, asked on 6/2/22

## The radius of a cylinder is 2m.Water is poured into up to a depth 2m.What is the pressure of water on the base of the container?(Density of water=1000kg/m3,acceleration due to gravity=10m/s2) Options- A)10000Pa B)20000Pa C)30000Pa D)40000Pa

Subject: Physics, asked on 5/12/21

## Plz help me out

Subject: Physics, asked on 30/11/21

## Kindly solve the following question.

Subject: Physics, asked on 8/11/21

## Samar weighing 60 kgf is wearing shoes with a heel of are of cross section 10 cm2, while Muskan weighing 36 kgf is wearing sandals with heel of area of cross section 1.2 cm2.(Take g = 10 m/s2) Calculate: (a) The pressure exerted on ground by their heels and (b) Compare the pressure when they stand on the heel of one foot.

Subject: Physics, asked on 24/10/21

## Solve this please.

Subject: Physics, asked on 17/10/21

## Teacher , Please help me with the below question Thank you

What are you looking for?


text:
Thread: series help

1. series help

help i think that the n on the 2 is a misprint
still help me to please

2. Re: series help

The $2^n$ in the denominator? The series wont converge without it so it looks fine to me.

3. Re: series help

Originally Posted by bkbowser
The $2^n$ in the denominator? The series wont converge without it so it looks fine to me.
What do you get as 1st 3 terms of this series?
I'm a bit confused! Thanks.
Trying to set up a looper...

4. Re: series help

Originally Posted by DenisB
What do you get as 1st 3 terms of this series?
I'm a bit confused! Thanks.
Trying to set up a looper...
Denis here is the general term.
$\Large{{a_n} = \frac{{\prod\limits_{k = 1}^n {(2k - 1)} }}{{{2^n}\left[ {\prod\limits_{k = 1}^n {(3k - 2)} } \right]}}}$

5. Re: series help

How in hell did I write down 2^4 instead of 2^n !!
Thanks Plato....

6. Re: series help

how do you solve it??

7. Re: series help

$\displaystyle \Large{{a_n} = \frac{{\prod\limits_{k = 1}^n {(2k - 1)} }}{{{\prod\limits_{k = 1}^n {(3k - 2)} } }}} <1$

8. Re: series help

u and a = numerator calculator
v and b = denominator calculator

Looper program:

u=1:v=1:a=1:b=1
loop n from 1 to ?
a=a*u
b=b*v
t=a/(b*2^n) : current term value
s=s+t : sum so far
u=u+2
v=v+3
next n

s = ~.79025 at n=15 and basically stays at that for eternity!!
All kinda "silly" to me


text:
Dimensionality reduction with least distance distortion

Question: Could I find a dimensionality reduction algorithm without or with minimal distance (cosine) distortion?

Background: I would like to visualize in 2D a sample of news texts for which I also apply clustering.

I present the texts as vectors (like text2vec), but it is desirable to also project the vectors to 2 dimensions. Moreover I consider apply clustering on the 2 dimensions for speedy execution.

My initial guess is using t-SNE with careful tuning of epsilon.

Are there more robust algorithms which do not distort much the distance (cosine) in multidimensional space, thus making clear pictures?

Update.

After several tries I got the following mapping (and clustering based on that):

This is a daily snapshot of Russian news by three major news agencies.

People liked the look, but what I worry about is that in the middle is a huge all-in-one cluster where all kinds of topics have been mixed. However, I know that there are many smaller topics every day that consist of maybe 3-5 news, but the meaning of them is quite specific.

I played quite some time with the parameters of t-SNE:

## rt-sne reduction

mat_dat <- data.matrix(text_centroids[, !c('message_id'), with = F])

rtsne_obj <- Rtsne(X = mat_dat
, dims = 2
, initial_dims = 100
, perplexity = 30
, theta = 0.3
, check_duplicates = FALSE
, pca = TRUE
, partial_pca = FALSE
, max_iter = 1000
, verbose = T
, is_distance = FALSE
, Y_init = NULL
, pca_center = TRUE
, pca_scale = TRUE
, normalize = TRUE
, momentum = 0.5
, final_momentum = 0.8
, eta = 200
, exaggeration_factor = 12

text_centroids[, Dim1 := rtsne_obj$$Y[,1]] text_centroids[, Dim2 := rtsne_obj$$Y[,2]]

ggplot(data = text_centroids) +
geom_point(aes(x = Dim1, y = Dim2), color = 'blue', alpha = 0.1, size = 4) +
theme_minimal()

## cluster with dbscan

clustering_dat <- rtsne_obj$Y[, 1:2] dbscan_knn <- frNN(x = clustering_dat, eps = 2, sort = TRUE, search = "kdtree", bucketSize = 10, splitRule = "suggest", approx = 0) dbscan_obj <- dbscan(x = dbscan_knn , weights = NULL , borderPoints = F ) table(dbscan_obj$cluster)

but could not get any better.

Update 2.

Measuring Pearson's correlation coefficient between the vector of Euclidean distances between full-dimensional points (right triangle of the dist matrix) and t-SNE dimensinal points yielded:

> cor.test(original_text_dist, tsne_dist)

Pearson's product-moment correlation

data:  original_text_dist and tsne_dist
t = 1095.5, df = 2388200, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.5774797 0.5791678
sample estimates:
cor
0.5783244

Hm, it looks that the distances were distorted although the distrortion was not so catastrophic.

I think I could try multidimensional scaling next.

Hmm, it looks like cmdscale in R stats:: does significantly better job at preserving the distances:

> mds <- cmdscale(original_text_dist)
> mds_dist <- dist(mds, diag = F, method = "euclidean", upper = F)
> original_text_dist <- as.vector(original_text_dist)
> mds_dist <- as.vector(mds_dist)
> cor.test(original_text_dist, mds_dist)

Pearson's product-moment correlation

data:  original_text_dist and mds_dist
t = 1834, df = 2388200, p-value < 2.2e-16
alternative hypothesis: true correlation is not equal to 0
95 percent confidence interval:
0.7641945 0.7652476
sample estimates:
cor
0.7647216

• Did you mean dimensionality reduction algorithm? I don't understand how convolution is involved. – user20160 Nov 24 '18 at 12:14
• Yes, I did. I meant convolving many features to just 2. – Alexey Burnakov Nov 24 '18 at 12:56
• Not sure I completely understand your goal, as dimensionality reduction algorithms like t-SNE typically don't involve convolutions. Were you particularly interested in something that uses the convolution operation? – user20160 Nov 25 '18 at 0:16
• (Disclamer: I didn't read the full question.) Cosine similarity is directly related to euclidean distance for normalized vectors called then chord distance. So, if you are using cosine or chord distance, you may use an iterative MDS, even its metric version. MDS is expected to "distort" your distances less than any dimensionality reduction methods. – ttnphns Nov 27 '18 at 11:47
• I won't, sorry. I'll give you a pair of local links at a wild guess, for the start. stats.stackexchange.com/a/36158/3277; stats.stackexchange.com/a/14017/3277; stats.stackexchange.com/a/31291/3277; stats.stackexchange.com/a/208238/3277 – ttnphns Nov 27 '18 at 11:56

The one possible approach could be a relatively new trimap method, based on triplets (in contrast with usual pair-wise distances). It's explicitly optimize to keep the original form (in contrast to t-sne), while making better clusters then PCA. https://github.com/eamid/trimap

First of all, my criterion of choosing any method for dimensionality reduction is visual inspection of a 2D mapping with a layer of clusters and their description. Judging by that, I like t-SNE more given it has mapped some of my texts into clear clusters (topics).

MDS and PCA that I also tried did not give a clustering look to the mapping.

Another option that I explored is a neural network designed specifically (by myself) for this task. Take a look and comment if you think it is worth it.

First, I create input vector which is a concatenation of two objects in the k-dimensonal space.

Then I build a multilayer dense network with bottleneck. The k-dimesnional input vector is being convolved to just 2 scalars.

Notice that the layers that convolve k dimensions to 2 dimensions are shared between both input vectors.

After that I include a stack of dence layers that should approximate euclidean distance (calculated between k-dimensional objects) using just 2-dimensional objects stemming from the bottleneck part.

As a result the NN leans to approximate the euclidean distance from first calculating an input of 2 + 2 scalars denoting the preojection on a 2D plane.

## build keras model for dimension reduction -------------------------

input1 <- layer_input(shape = c(dim(dat_train)[2] / 2), name = 'input1')

input2 <- layer_input(shape = c(dim(dat_train)[2] / 2), name = 'input2')

shared_layer2 <- layer_dense(units = 128, activation = "relu")
shared_layer3 <- layer_dense(units = 32, activation = "relu")
shared_layer4 <- layer_dense(units = 2, name = 'shared_layer4')

text_1_model <- input1 %>%
shared_layer2 %>%
shared_layer3 %>%
shared_layer4

text_2_model <- input2 %>%
shared_layer2 %>%
shared_layer3 %>%
shared_layer4

main_output <- layer_concatenate(c(text_1_model, text_2_model)) %>%
layer_dense(units = 32, activation = "relu") %>%
layer_dense(units = 16, activation = "relu") %>%
layer_dense(units = 1)

convolving_model <- keras_model(
inputs = c(input1, input2),
outputs = main_output
)

summary(convolving_model)

convolving_model %>% compile(
loss = "mse",
metrics = list("mean_squared_error")
)

epochs <- 500
batch_size <- dim(train.x.1)[1]

# Fit the model and store training stats

history <- convolving_model %>% fit(
x = list(train.x.1, train.x.2)
, y = train.y
, batch_size = batch_size
, epochs = epochs
, validation_split = 0.3
)

plot(history)

After the NN has converged, I take the output of a hidden layer that is the 2D plane projections of the two input vectors. Use them as your dimensionality reduction matrix of coordinates.

## get hidden representations

layer_name <- 'shared_layer4'

intermediate_layer_model1 <- keras_model(
inputs = convolving_model$input , outputs = get_output_at(shared_layer4, 1) ) intermediate_layer_model2 <- keras_model( inputs = convolving_model$input
, outputs = get_output_at(shared_layer4, 2)
)

intermediate_output1 <- as.data.table(predict(intermediate_layer_model1, list(train.x.1, train.x.2)))

intermediate_output2 <- as.data.table(predict(intermediate_layer_model2, list(train.x.1, train.x.2)))

calc_coords <- cbind(intermediate_output1, intermediate_output2)

Surprisingly, the correlation of the euclidean distances between the calculated 2D coordinates and the original k-dimensional distances is a whopping 0.85 (if the NN converged well).

colnames(calc_coords) <- c(
'x1'
, 'y1'
, 'x2'
, 'y2'
)

calc_coords[, calc_euclid := sqrt((x1 - x2)^2 + (y1 - y2)^2)]

cor.test(train.y, calc_coords[, calc_euclid])


text:
## anonymous one year ago what are the equations to these diagrams? (attachment) please help will medal!!

1. anonymous

2. anonymous

3. anonymous

4. welshfella

the first one is a quadratic equation with a negative coefficient of x^2. It has zeros of 2 and -8. and a vertex at (-3,6) can you find the equation from this info?

5. anonymous

For the 1st diagram $-(x+3)^2=4(y-6)$

6. anonymous

is this right? −(x+3)2=4(y−6) @welshfella

7. anonymous

the ellipse equation is $\frac{ (y-4)^2 }{ 6^2 }+\frac{ (x-3)^2 }{ 4^2 }=1$

8. anonymous

for the first one? @saseal

9. anonymous

yes the first picture

10. anonymous

11. welshfella

yes that is correct for the first graph

12. anonymous

im doing the circle now, its $(x+6)^2+(y+2)^2=5^2$

13. anonymous

what about #13 and #14? then i will figure out the rest myself thank you for all ur help @saseal

14. anonymous

$\frac{ (x-7)^2 }{ 2^2 }-\frac{ y-2)^2 }{ 2^2}=1$ for the hyperbola

15. anonymous

circle is #13 and hyperbola is #14

16. anonymous

what about the one on the top right?

17. anonymous

the oval @saseal

18. anonymous

2nd picture?

19. anonymous

yes

20. anonymous

gimme a few minutes

21. anonymous

thank you so much

22. anonymous

First oval is $\frac{ (x-7)^2 }{ 6^2 }+\frac{ y^2 }{ 3^2 }=1$

23. anonymous

if i tag you in some other ones on a different question could you help?

24. welshfella

plz dont just give answers saseal Code of Conduct asks us to Guide the user to the answers

25. anonymous

okies

26. anonymous

so I can't give ya answer now, work the last one out yourself; parabola equation for that curve looks something like this :) $(y+1)^2=4p(x-4)^2$


text:
## Sunday, 13 January 2013

### (Dis)Utility

Mart Janssen writes me that he's tried to run the examples for BCEA and pointed out a problem $-$ it all boiled down to a missing script, really, but thanks Mart (if you're reading this)!

I've now fixed the problem by updating the file Utils.R (which was responsible) to include the script for the function betaPar2 (which I use to compute the parameters of a Beta distribution under a constrain specified by the user in terms of the mode and the upper value that is reasonable to assume).

I think the very first version of the file I've put on the website was actually OK (ie it included the required function), but then I must have messed it up during one of the updates.


text:
# Oscillating spring, speed close to the equilibrium: How is this answer not 1.5? [closed]

I have this question with the answer listed as $$2.0\,\mathrm{m/s}$$.

"A $$1.25\,\mathrm{kg}$$ mass on a spring with a constant of $$12.0\,\mathrm{N/m}$$ is oscillating back and forth. Its maximum speed is $$2.5\,\mathrm{m/s}$$. What speed will it have when it is $$0.48\,\mathrm{m}$$ from equilibrium?"

But I'm struggling to understand that.

What I did was $$mv^2 = kx^2$$, which eventually translates down to $$0.48\,\sqrt{12/1.25} \simeq 1.487\,\mathrm{m/s}$$.

Is the answer just a simple mistake, or am I miscalculating this?

## closed as off-topic by John Rennie, Thomas Fritsch, David Z♦Jun 25 at 7:19

This question appears to be off-topic. The users who voted to close gave this specific reason:

• "Homework-like questions should ask about a specific physics concept and show some effort to work through the problem. We want our questions to be useful to the broader community, and to future users. See our meta site for more guidance on how to edit your question to make it better" – John Rennie, Thomas Fritsch, David Z
If this question can be reworded to fit the rules in the help center, please edit the question.

• Why do you think the kinetic energy would be equal to the elastic potential energy? Rather, they will add to a constant value. You could use $\Delta E_k=-\Delta E_e$ – M. Enns Jun 25 at 1:37
• I added MathJax, please use it in the future for typesetting math. See guide and links therein. I've also added the tag "Homework and exercises." Please use it in the future for similar questions. Also check out the meta and the guide. – MannyC Jun 25 at 4:54
• Hi Jason and welcome to the Physics SE! Please note that we don't answer homework or worked example type questions. Please see this page in the site help for more on what topics you can ask about here. – John Rennie Jun 25 at 5:03
• The idea to use the energy conservation is right, you just need to be careful about which $x$ and $v$ you use (by which I mean at which time). You need however to use additional relations between the dynamics of the position and the speed to find the correct answer. If it reassures you, I find 2 $m/s$. – David Jun 25 at 9:30


text:
# Question #79600

Apr 11, 2016

Sulphur dioxide gas reacts with hydrogen sulphide to give rise to sulphur and water, not oxygen.

#### Explanation:

A little drop of water activates the reaction between the two gases.

$S {O}_{2} \left(g\right) + {H}_{2} S \left(g\right) \to S \left(s\right) + {H}_{2} O \left(l\right)$

Despite it is a redox, the equation can be easily balanced by trials and errors.
Having two oxygen atoms in the reactants, we need four hydrogen atoms, that is two ${H}_{2} S$ molecules, and we obtain three suphur atoms together with two water molecules:

$S {O}_{2} \left(g\right) + 2 {H}_{2} S \left(g\right) \to 3 S \left(s\right) + 2 {H}_{2} O \left(l\right)$

Taking in account that sulphur is octaatomic and the least common multiple between 3 and 8 is 24, we must multiply every coefficient by eight to have the very complete equation:

$8 S {O}_{2} \left(g\right) + 16 {H}_{2} S \left(g\right) \to 3 {S}_{8} \left(s\right) + 16 {H}_{2} O \left(l\right)$


text:
Int J Performability Eng ›› 2019, Vol. 15 ›› Issue (12): 3262-3270.

### Analysis of the Features of Air Traffic Controllers' Eye Movements

Fei Lua,*, Qian Wanga, Jingjie Tenga, Yan Kangb, and Bilian Liuc

1. aCollege of Air Traffic Management, Civil Aviation University of China, Tianjin, 300300, China;
bOperation Control Department, China Cargo Airlines , Shanghai, 200335, China;
cFlight Department, Tianjin Air Cargo Co., Ltd., Tianjin, 300300, China
• Submitted on  ;  Revised on  ; Accepted on
• Contact: * E-mail address: lufei315@126.com

Abstract: This paper aims to study the eye fixation and eye movements of air traffic control (ATC) controllers while working. Based on the establishment of the experimental platform of control simulation, every behavior feature of eye movement was recorded under different difficulty levels of experiments. With the geographic coordinates of the controlled area being converted to their corresponding screen coordinates, the controlled area could be divided into six zones by means of clustering analysis. The percentage of fixation points per unit in Zones 1 and 2 is dramatically greater than that of other zones, as determined by computing the fixation point per unit within the six zones. The Markov theory was applied to compute the probability of one-step fixation movement within the six zones. The results demonstrate that the highest probability of one-step fixation movement exists in Zones 1, 2, and 5, where the airspace of departure and arrival, airway crossover points, and navigation stations exist. Therefore, an experienced controller attaches a greater amount of importance and attention to the congested airspace and areas where conflict could easily occur. This paper can be used as a reference for the training of newly-employed ATC controllers to work in a safer and more efficient manner.